locked
Hangman Help C# RRS feed

  • Question

  • Hey,

    I'm just learning how to code and have decided to try to make a c# hangman game, with the help of internet resources. I have one issue with the source code, so basically when the user is typing their letters it just doesn't display the first letter of the word. Any help would be appreciated. 

               Thanks in advance.


    if (uservalue == "3")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                     string[] arrayofwords = { "Soccer", "Tennis", "Basketball", "Badminton", "Baseball", "Hockey" };                         // my word bank

                    Random ran = new Random();   // created variable to randomize word bank
                    var number = ran.Next(0, 6);
                    string randomword = arrayofwords[number];
                    char[] guess = new char[randomword.Length];
                    Console.WriteLine("You chose sports");





                    for (int p = 0; p < randomword.Length; p++) // the squiggly lines will be as long as the random word itself 


                        guess[p] = '~';      // this is for the squiglly lines you usually see on hangman games

                        while (true)
                        {
                            Console.Write("Please enter your guess: ");
                            char playerGuess = char.Parse(Console.ReadLine());
                            for (int j = 0; j < randomword.Length; j++)
                            {

                                if (playerGuess == randomword[j])

                                    guess[j] = playerGuess;







                            }

                            Console.WriteLine(guess);




                        }

    }



                                                                                                                                                                
    Tuesday, June 11, 2013 1:11 PM

Answers

  • I presume it is because the first letter is uppercase and all the others are lowercase.  To fix this, change the comparison and perform the comparison in lowercase.

    if( char.ToLower( playerGuess ) == char.ToLower( randomword[j]) )

    Then, rather than filling in the character with the guess, fill it in with the original letter.  That will preserve the case of the original word:

    guess[j] = randomword[j];

    Alternatively, you can rework your game so that you play entirely in uppercase rather than storing mixed-case answers.  This is actually quite typical in word games.  Crosswords, Boggle, Scrabble, etc. all use uppercase letters only.

    • Marked as answer by Hyrax Tuesday, June 11, 2013 4:57 PM
    Tuesday, June 11, 2013 1:44 PM

All replies

  • I presume it is because the first letter is uppercase and all the others are lowercase.  To fix this, change the comparison and perform the comparison in lowercase.

    if( char.ToLower( playerGuess ) == char.ToLower( randomword[j]) )

    Then, rather than filling in the character with the guess, fill it in with the original letter.  That will preserve the case of the original word:

    guess[j] = randomword[j];

    Alternatively, you can rework your game so that you play entirely in uppercase rather than storing mixed-case answers.  This is actually quite typical in word games.  Crosswords, Boggle, Scrabble, etc. all use uppercase letters only.

    • Marked as answer by Hyrax Tuesday, June 11, 2013 4:57 PM
    Tuesday, June 11, 2013 1:44 PM
  • Makes sense, thanks again!
    Tuesday, June 11, 2013 4:58 PM