Jump to content
  • Advertisement
Sign in to follow this  
DigitalSavior

Guess my number in C#

This topic is 4005 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my implementation of guess the number. I am rather new to programming so all comments and criticism is welcome. 2 things off the top of my head: I have variables for the boundaries. I was going to make the boundaries user selectable but have not done that yet. And secondly, the logic in the while (guessing) loop seems a little off due to what I named the variables and their value.

using System;

namespace GuessMyNumber
{
    class GuessMyNumber
    {
        static void Main()
        {
            bool playingGame = true;

            Console.WriteLine("Welcome to Guess My Number!  Do you think you can guess my number?");
            while (playingGame)
            {
                PlayGame();

                Console.Write("Would you like to play again? ");

                bool asking;
                string playAgain;
                asking = true;

                while (asking)
                {
                    playAgain = Console.ReadLine();
                    playAgain = playAgain.ToLower();

                    if (playAgain == "n" || playAgain == "no")
                    {
                        playingGame = false;
                        asking = false;
                    }
                    else if (playAgain == "y" || playAgain == "yes")
                    {
                        playingGame = true;
                        asking = false;
                    }
                    else
                        Console.Write("I do not understand your selection.  Please type yes or no: ");
                }
            }
        }

        static int PickRandomNumber()
        {
            int minNumber = 1;
            int maxNumber = 100;
            Random pickNumber = new Random();

            return pickNumber.Next(minNumber, maxNumber);
        }

        static void PlayGame()
        {
            int randomNumber;
            int userInput = 0;
            int numberOfTries = 0;
            bool guessing = true;
            bool gettingInput;

            randomNumber = PickRandomNumber();

            while (guessing)
            {
                gettingInput = false;

                while (gettingInput == false)
                {
                    Console.Write("Please pick a number between 1 and 100: ");
                    gettingInput = int.TryParse(Console.ReadLine(), out userInput);
                }

                if (gettingInput)
                {
                    if (userInput > 1 && userInput < 100)
                    {
                        numberOfTries += 1;

                        if (userInput == randomNumber)
                        {
                            Console.WriteLine("Good Job!\n");
                            guessing = false;
                        }
                        else if (userInput < randomNumber)
                            Console.WriteLine("Try guessing a higher number!\n");
                        else if (userInput > randomNumber)
                            Console.WriteLine("Try guessing a lower number!\n");
                    }
                }
            }

            Console.WriteLine("You guessed {0} times!\n", numberOfTries);
        }
    }
}


Share this post


Link to post
Share on other sites
Advertisement
It looks sound to me... a couple of suggestions to make it more concise, but which aren't necessary at all:

1) Move your Play Again? loop to its own function. That makes the main loop very readable:


bool playingGame = true;
while (playingGame)
{
PlayGame();
playingGame = PromptForPlayAgain();
}



2) PickRandomNumber, as it stands, doesn't need to have its own function. It only takes one line of code to do what you are doing:
randomNumber = new Random.Next(1, 100);

If you decide to add some other stuff--like selecting a range--then you may want to move it to a function to house all of the logic.

Share this post


Link to post
Share on other sites
Further to smitty1276's first point, if you move that code to a separate "bool StillWantsToPlay()" function, then you can change the while loop into a do loop and drop the boolean flag for even neater code:

do
{
PlayGame();
} while(StillWantsToPlay())


You can do a similar thing in PlayGame and drop your 'guessing' variable:

do
{
/* ... get the user to make a guess here ... */
} while(guess != answer)


and the gettingInput loop:

do {
Console.Write("Please enter a number between 1 and 100: ");
} while(!int.TryParse(Console.ReadLine(), out userInput))


You could also return immediately after printing the "Good Job" message, though that's partly a matter of style. Currently that function has what's known as the "Single Entry, Single Exit" property, in that the function always starts and exits in exactly the same place, and that's sometimes nice to have.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!