user3475011
user3475011

Reputation: 21

Number Guessing Game: difficulty setting

This is how I am generating my random number, but I would like to be able to have the user select the difficulty: easy = 1-10, intermediate = 1-50, hard = 1-100. I was wondering how this could be done. Any help would be greatly appreciated.

{
    class HiLow
    {
        private int number;

        public int Number
        {
            get
            {
                return number;
            }
        }

        public HiLow(int taker)
        {
            number = new Random().Next(taker);
        }
    }
}

{
    class HiLowUI
    {
        public void Welcome()
        {
            Console.WriteLine("\nWelcome to the HighLow Game!" +
                "\n\nInstructions: \nTry to guess the number between the selected difficulty range." + 
                "\nIf you guess the right number you win!");

            Console.WriteLine("\nDifficulty: \n[1] (1-10) Easy \n[2] (1-50) Intermediate \n[3] (1-100) Hard");

        }
        public void Play()
        {
            Welcome();

            while (true)
            {
                HiLow hi = null;
                int number = hi.Number;
                int guess;
                int attempts = 0;

                int difficulty = PromptForInt("Select difficulty");
                if (difficulty == 1)
                {
                   hi = new HiLow(10);
                }
                else if (difficulty == 2)
                {
                    hi = new HiLow(50);
                }
                else if (difficulty == 3)
                {
                    hi = new HiLow(100);
                }

                for (guess = PromptForInt("\nEnter your guess! "); guess != number; guess = PromptForInt("\nEnter your guess! "))
                {
                    if (guess < number)
                    {
                        Console.WriteLine("Higher");
                        attempts++;
                    }
                    else if (guess > number)
                    {
                        Console.WriteLine("Lower");
                        attempts++;
                    }
                }
                Console.WriteLine("\nIt took you {0} attempts, but you've won!\n{1} was the correct number!\n", attempts, number);
                Console.WriteLine("Would you like to play again? (y/n)");

                if (Console.ReadLine() != "y") break;
            }
        }
        private int PromptForInt(string prompt)
        {
            int value;
            Console.Write(prompt);
            int.TryParse(Console.ReadLine(), out value);
            return value;
        }
    }
}

Upvotes: 0

Views: 1390

Answers (2)

OnlineCop
OnlineCop

Reputation: 4069

In your constructor:

public HiLow(int upperLimit)
{
    number = new Random().Next(upperLimit);
}

Within your Play() method, you would instantiate the HiLow class as one of:

HiLow hi = new HiLow(10);  // easy
HiLow hi = new HiLow(50);  // medium
HiLow hi = new HiLow(100); // hard

Then simply prompt the user for their desired level of difficulty before you enter the Play() method, and pass in whatever value they chose to the HiLow() constructor.

Personally, I would pass in the difficulty to the Play() method, and use a switch() or if() statement to determine which one to call:

public void Play(int difficulty)
{
    while (true)
    {
        HiLow hi = null;
        if (difficulty == 0)      // Easy
        {
            hi = new HiLow(10);
        }
        else if (difficulty == 1) // Medium
        {
            hi = new HiLow(50);
        }
        else if (difficulty == 2) // Hard
        {
            hi = new HiLow(100);
        }
        else if (difficulty == 3) // Much harder
        {
            hi = new HiLow(5000);
        }
        else if (difficulty == 4) // Mwa-ha-ha
        {
            hi = new HiLow(99999999);
        }

        // ...
    }
}

Upvotes: 0

user1932079
user1932079

Reputation:

When you have the user's setting, add an option to the constructor of HiLow for the maximum number. Use that number for generating the random number.

Upvotes: 1

Related Questions