Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


DigitalSavior

Member Since 02 Jul 2005
Offline Last Active Apr 25 2014 05:54 PM

Topics I've Started

Guess my number in C#

01 August 2007 - 03:55 AM

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);
        }
    }
}



First Project

23 July 2007 - 10:12 AM

Just wanted to know when the first project was gonna happen. I know JWalsh is busy and that we changed texts. Thank You!

PARTNERS