C sharp Hangman game + Counter

时间:2016-10-09 15:51:23

标签: c# arrays loops oop

I am trying to alter my hangman code to run correctly first, and second do the additional things I need it to do which include keeping score. So here's a description

  • You will be creating a Hangman program. In this game, the computer will have a hard coded word and the user will enter one letter at a time until he or she has guesses the correct word. A score will be tallied that will be the number of incorrect guesses the user has. So a smaller score is better.

Initially the word will be displayed as a list of special characters such as '*' or '-'. The user will input one letter at a time and each input will be compared with the word. If the guess is correct the letter will be displayed rather than the special character. Each incorrect guess will increment the score. When the user guesses the correct word the game will be over.

specifically what I was given for the introduction of loops and arrays: Modify the design and program to allow for iteration. Increase the number of guesses to 10 to solve the word. Display the word to the user with each letter as a special character such as ********. Create an array of correct letters guessed such as: char[] guessed = new char[26]; You will need counter also to keep track of how many letters are in the guessed array. You do not need to keep track of incorrectly guessed letters.

Below is my code.

//declare variables
char letter1 = 'r';
char letter2 = 'o';
char letter3 = 's';
char letter4 = 's';
char letter;
int score = 0;
int finalScore = 0;
char[] guessed = new char[26];
int index = 0;

        Console.WriteLine("Welcome to the Hangman Program");

        for (int i = 0; i < 10; i++)
        {

            Console.WriteLine("****");

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());


            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;

            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
            }
            finalScore = score;
        }

        Console.WriteLine("_____________________________________________");
        Console.WriteLine("Your score is: " + finalScore);
        Console.ReadLine();

    }
}

}

Once I run this, it puts me in an infinite loop. I just am at a loss on what I'm doing wrong here.


Update:

I was able to reduce it to run 12 times instead of 40. when i have the int < 8 in my while loop, it runs 8 times. if I set it to anything over 9, it will run 12 times. It begins the whole loop a third time. code is below

char letter1 = 'r';
char letter2 = 'o';
char letter3 = 's';
char letter4 = 's';
char letter;
int score = 0;
int finalScore = 0;
char[] guessed = new char[26];
int index = 0;


        Console.WriteLine("Welcome to the Hangman Program");

        do
        {

            Console.WriteLine("****");

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());


            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;

            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
                index++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
                index++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
                index++;
            }

            Console.WriteLine("\n\nEnter a letter please: ");
            letter = char.Parse(Console.ReadLine());
            if (letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4)
            {
                Console.WriteLine("You've guessed correctly!");
                guessed[index] = letter;
                index++;
            }
            else
            {
                Console.WriteLine("You've guessed incorrectly");
                score++;
                index++;
            }
            finalScore = score;
        }
        while ((index < 9) || (letter != letter1 && letter!= letter2 && letter != letter3 && letter != letter4));

        Console.WriteLine("_____________________________________________");
        Console.WriteLine("Your score is: " + finalScore);
        Console.ReadLine();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

You don't have any exit condition once the word is completed.

There is no infinite loop, it will just read lines four times (one for each ReadLine invocation) by ten (your for loop). After 40 inputs it will actually exit.

I would recommend sharing this code on https://codereview.stackexchange.com/ once you get it working.