none
How to prevent random number game from saying things at the wrong time RRS feed

  • Question

  • I'm trying to make a random number guessing game for the command prompt and it's working out okay, but whenever the game restarts after user wants to play again, the game keeps saying, "Want to play again? 'Y' or 'N': Y Wrong guess. Number is too high. Pick a lower number." or "Want to play again? 'Y' or 'N': Y Wrong guess. Number is too low. Pick a higher number. " on the same line.
    How do I prevent the game from thinking the Y or N question is a guess?

    Code:

    using System;

    using static System.Console;

    namespace Guessing Game

    {

        class Program
        {
            static void Main(string[] args)
            {
                Random RandomClass = new Random();
                int randomNumber;
                int guess;
                string yesOrNo;
                string upperYesOrNo;
                int guessAmount = 0;
                const int MIN = 1;
                const int MAX = 10;
                //random number will be >= MIN and < MAX
                randomNumber = RandomClass.Next(MIN, MAX);

                Write("Guess a number from 1 to 10: ");
                guess = int.Parse(ReadLine());

                do
                {
                    randomNumber = RandomClass.Next(MIN, MAX);
                    do
                    {
                        if (guess != randomNumber && guess < randomNumber)
                        {
                            guessAmount++;
                            WriteLine("Wrong guess. Number is too low. Pick a higher number. ");
                            Write("Guess a number from 1 to 10: ");
                            guess = int.Parse(ReadLine());
                        }
                        else if (guess != randomNumber && guess > randomNumber)
                        {
                            guessAmount++;
                            WriteLine("Wrong guess. Number is too high. Pick a lower number. ");
                            Write("Guess a number from 1 to 10: ");
                            guess = int.Parse(ReadLine());
                        }
                    } while (guess != randomNumber);

                    if (guess == randomNumber)
                    {
                        WriteLine($"Correct! The number was {randomNumber}.");
                        WriteLine($"It took you {guessAmount} guesses.");
                    }
                    Write("Want to play again? 'Y' or 'N': ");
                    yesOrNo = ReadLine();
                    upperYesOrNo = yesOrNo.ToUpper();

                    if (upperYesOrNo == "Y")
                    {
                        Write("Guess a number from 1 to 10: ");
                    }
                } while (upperYesOrNo == "Y");

                //Debug pause
                WriteLine("Press any key to continue...");
                ReadKey();
            }
        }
    }
    Tuesday, October 16, 2018 1:28 AM

Answers

  • Greetings Rainbow.

    You need to reset the guess and guessAmount.

             Write("Want to play again? 'Y' or 'N': ");
             yesOrNo = ReadLine();
             upperYesOrNo = yesOrNo.ToUpper();
    
             if (upperYesOrNo == "Y")
             {
                Write("Guess a number from 1 to 10: ");
                guess = int.Parse(ReadLine()); // Add this.
                guessAmount = 0; // And this.
             }

    • Marked as answer by Rainbow1225 Tuesday, October 16, 2018 3:27 AM
    Tuesday, October 16, 2018 1:41 AM