none
Hangman RRS feed

  • Question

  • I need help. I'm a new programmer and i cannot get this hangman program to run. Any tips?

    Here's the Program: 

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {

                string[] words = new string[18];

                words[0] = "hat";
                words[1] = "swag";
                words[2] = "dog";
                words[3] = "paan";
                words[4] = "buzz";
                words[5] = "shirt";
                words[6] = "shoes";
                words[7] = "tennis";
                words[8] = "ignore";
                words[9] = "football";
                words[10] = "tailor";
                words[11] = "nugget";
                words[12] = "sholay";
                words[13] = "epidemic";
                words[14] = "erupting";
                words[15] = "enormous";
                words[16] = "hydroencephalus";
                words[17] = "biryani";

                Random random = new Random();
                var number = random.Next(0, 17);
                string randomWord = words[number];
                char[] guess = new char[randomWord.Length];

                for (int p = 0; p < randomWord.Length; p++)
                    guess[p] = '~';

                while (true)
                {
                    int a = 5;
                Turn:
                    Console.Write("Enter a letter");
                    char playerguess = char.Parse(Console.ReadLine());
                    for (int d = 0; d < randomWord.Length; d++)
                    {
                        
                        if (playerguess == randomWord[d])
                        {
                            guess[d] = playerguess;
                        }
                        if (playerguess != randomWord[d])
                        {
                            while (a < 6)
                            {
                                Console.WriteLine("Nice try, you have {0} lives", life(a));
                                goto Turn;
                            }
                        }
                        
                    }

                    Console.WriteLine(guess);
                }
            }
                      static double life(int a)
                        {
                            return (a - 1);
                        }
                      
                
            
        }
    }

    Tuesday, June 11, 2013 12:37 PM

Answers

  • Actually there are a few things you should change: 

    1.) In your main function a is always the same so you always report "you have 4 lives left", so add 'a--;' into the while loop.

    2.) This change will however allow to go to negative lives, so add an if statement into the loop as well. E.g.: 

    if (a == 0)

    goto End;  //Define an End somewhere

    3.) Also printout the guees word somewhere, when you guees a char right.

    4.) Oh and move your while loop out of the for loop, because now it only check the first letter of the word.



    • Edited by MadMikeP84 Tuesday, June 11, 2013 1:00 PM
    • Proposed as answer by Lil Aswang Tuesday, June 11, 2013 1:21 PM
    • Marked as answer by Jason Dot Wang Friday, June 21, 2013 8:11 AM
    Tuesday, June 11, 2013 12:56 PM

All replies

  • can you please explain what error or problem you have?

    Tuesday, June 11, 2013 12:53 PM
  • Actually there are a few things you should change: 

    1.) In your main function a is always the same so you always report "you have 4 lives left", so add 'a--;' into the while loop.

    2.) This change will however allow to go to negative lives, so add an if statement into the loop as well. E.g.: 

    if (a == 0)

    goto End;  //Define an End somewhere

    3.) Also printout the guees word somewhere, when you guees a char right.

    4.) Oh and move your while loop out of the for loop, because now it only check the first letter of the word.



    • Edited by MadMikeP84 Tuesday, June 11, 2013 1:00 PM
    • Proposed as answer by Lil Aswang Tuesday, June 11, 2013 1:21 PM
    • Marked as answer by Jason Dot Wang Friday, June 21, 2013 8:11 AM
    Tuesday, June 11, 2013 12:56 PM