mark7474
mark7474

Reputation: 13

Guess the number in C# with player chances

I made a little game, but it's not work as I wanted to. It is a Guess the number game. The player has 10 chances. But there is my problem. It's increase the value of playerChance until it's reach 10. What can I use instead of while?

It's output is:

Guess the number game! Do you wanna play? y/n
y
The game started! What is your guess?
78
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!
Your number was too high!

Code:

namespace GuessTheNumberGame_v1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Guess the number game! Do you wanna play? y/n");
            if (Console.ReadKey(true).KeyChar == 'y')
            {
                Console.WriteLine("The game started! What is your guess?");
                int playerGuess = Convert.ToInt32(Console.ReadLine());

                Random r = new Random();
                int compGuess = r.Next(1, 101);

                int playerScore = 0;
                int playerChance = 0;

                bool play = true;

                do
                {
                    while (playerChance < 10)
                    {
                        if (playerGuess > compGuess)
                        {
                            ++playerChance;
                            Console.WriteLine("Your number was too high!");
                        }
                        else if (playerGuess < compGuess)
                        {
                            Console.WriteLine("Your number was too small!");
                            ++playerChance;
                        }
                        else
                        {
                            ++playerScore;
                            Console.WriteLine("You win! \nYou have: " + playerScore + " points!");
                            playerChance = 0;
                        }
                    }

                    Console.WriteLine("Do you wanna play again? y/n");
                    if (Console.ReadKey(true).KeyChar == 'n')
                        play = false;
                } while (play);     
            }

            else
            {
                Console.ReadKey();
            }
        }
    }
}

Upvotes: 0

Views: 1453

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660279

You have four problems.

The first is that you don't break out of the inner loop when you're done:

                    else
                    {
                        ++playerScore;
                        Console.WriteLine("You win! \nYou have: " + playerScore + " points!");
                        playerChance = 0;
                        break; // <-- "breaks out" of the innermost loop early
                    }

A break is an unconditional loop exit. Compare with continue, which restarts the loop and re-checks the loop condition.

The second is that the "input the user's guess" logic needs to be inside the inner loop!

The third is that your code crashes if the user inputs something other than a number. Use int.TryParse, and if the user's input is not a number, then make them try again. You'll need a loop for that too!

The fourth is that you have no "you lose" message. Can you see how to implement that?

Finally: your problem will get easier to solve if you start breaking up your program into smaller methods. Suppose we have a method:

// Asks the user to guess; returns true if the user guessed correctly.
private static bool Guess(int answer)
{
  int playerGuess = GetIntegerFromUser("What is your guess?");
  // TODO: write GetIntegerFromUser
  if (playerGuess > answer)
  {
    Console.WriteLine("Your number was too high!");
    return false;
  }
  else if (playerGuess < answer)
  {
    Console.WriteLine("Your number was too small!");
    return false;
  }
  else
  {
    Console.WriteLine("You win!");
    return true;
  }
}

Now your "main" loop gets much easier to read.

while (playerChance < 10)
{
  if (Guess(compGuess))
  {
     // TODO: Deal with winning
  }
  ...

and so on. Identify small problems, solve each with a method, and then put those solutions together.

Upvotes: 4

J. McNair
J. McNair

Reputation: 26

To answer the question, the reason it isn't working as expected is because you are only reading the number input once. You will need to get a new guess each time there is an incorrect answer.

There are a few other items that will need to be addressed:

  • What will happen when they get the correct answer before the 10th try?
  • What will the user see when they lose?
  • Will they get the correct prompts when they play another game?

Other suggestions I have for you for general development:

  • When reading a value, don't assume it will be the correct type
  • Consider creating small functions for specific tasks. This will help you in the future with testing, updating, troubleshooting, and maintenance. In this case, perhaps a a function to check the answer will simplify some of the logic.

Upvotes: 1

user4843530
user4843530

Reputation:

Change

while (playerChance < 10)

To

while (playerChance < 10 && playerGuess != compGuess)

And you have to input a new guess on each iteration, so move the

int playerGuess = Convert.ToInt32(Console.ReadLine());

To inside that loop

int playerGuess = -1;
...
while (playerChance < 10 && playerGuess != compGuess) {
    playerGuess = Convert.ToInt32(Console.ReadLine());

Upvotes: 0

Related Questions