Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Hi, I'm almost finished with this Hangman game but some problems still remain.

c# help game hangman

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 41wp2

41wp2

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 May 2015 - 10:25 AM

Hi, I'm almost finished with this Hangman game but some problems still remain.
1. How many errors you get to have when you guess the word. I want the player to choose. Right now there are two .
2. That you will guess the letters and not the word.
3. When you guess the word it comes up "_ " for each letter so you know how long the word is .


Can anyone help?

 

 

 

 
 

    class Program
    {
        static List<string> ordbank = new List<string>();
        static bool Spela = false;
        static bool Meny = true;
        static int Fel = 1;
        static int FelGissningar = 0;
        static int AntalOrd = 5; 
        static void Main(string[] args)
        {
            Console.WriteLine("\nHänga gubbe!" +
                              "\n\n1) Lägg till ord" +
                              "\n2) Lista till alla ord" +
                              "\n3) Spela" +
                              "\n4) Avsluta");
            do
            {
                Console.Write("\nVälj 1 - 4: ");
                string menuInput = Console.ReadLine();
                switch (menuInput.ToLower())
                {
                    case "1":
                        Console.WriteLine("\nLägga till fem nya ord:\n ");
                        for (int i = 1; i <= AntalOrd; i++)
                        {
                            Console.WriteLine("Lägg till ord " + i + ": ");
                            string wordInput = Console.ReadLine();
                            ordbank.Add(wordInput);
                        }
                        ordbank.Sort();

                        break;

                    case "2":
                        Console.WriteLine("Lista över alla ord");
                        if (ordbank.Count > 0)
                        {

                            foreach (string ord in ordbank)
                            {
                                Console.WriteLine(ord);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Du har inte lagt till några ord");
                        }
                        break;
                    case "3":
                        if (ordbank.Count == 0)
                        {
                            Console.WriteLine("\nDu måste lägga till fem ord innan du kan spela");
                            break;
                        }

                        Console.Write("Välj hur många fel: " + Fel );
                        Console.WriteLine("\nGissa vilka ord som finns");
                        Spela = true;
                        FelGissningar = 0;

                        do
                        {
                            Console.Write("Gissa ord: ");
                            string guessedWord = Console.ReadLine();
                            if (ordbank.Contains(guessedWord))
                            {
                                 
                                ordbank.Remove(guessedWord);


                                 
                                if (ordbank.Count == 0)
                                {
                                    Console.WriteLine("Du vann");
                                    Spela = false;
                                }
                                else
                                {
                                    Console.WriteLine("Det ordet fanns");
                                }

                            }
                            else
                            {
                                FelGissningar++;
                                if (FelGissningar == Fel)
                                {
                                    Spela = false;
                                    Console.WriteLine("Du förlorade");
                                    ordbank.Clear();
                                }
                                else
                                {
                                    Console.WriteLine("Du gissade fel, du har " + (Fel - FelGissningar) + "försköt till");
                                }
                            }

                        } while (Spela);
                        break;
                    case "4":
                        Console.WriteLine();
                        Meny = false;
                        break;             
                }
            } while (Meny == true);

        }


    }
}
        
    

 

 


#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 29 May 2015 - 03:25 PM

#1:  Just do a simple console.ReadLine() and then do an int.TryParse to get the number of missed guesses.  

Create a variable to hold the missed guesses.  For every missed guess, increment the number, and check against the number of missed guesses that the user picked.

 

#2:  You can have a list of words, that are either hard coded, read from a file, or database, etc.  Get the source string from a random number between 0 and the number of items in the list.  Then have the user guess a letter. Then do an index on that guess against the source string.

 

#3: Have another variable that holds the current status of the word.  Create the string by finding the length of the source string, and simply filling the string with that number of "_" characters.  Check against the index found in #2, and replace that character with the actual character.  

 

For example:  The word is "world". The user guesses "o", the index of "o" in "world" is 1.  Then simply change that character in the display string to the actual string.  This would have to be in a loop until all the characters of that letter are shown in the display string.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download