数字猜测游戏:设置难度

时间:2014-04-06 18:30:48

标签: c# if-statement

这就是我生成随机数的方法,但我希望能够让用户选择难度:easy = 1-10,middle = 1-50,hard = 1-100。我想知道如何做到这一点。任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:1)

当您拥有用户的设置时,请为HiLow的构造函数添加一个选项以获取最大数量。使用该数字生成随机数。

答案 1 :(得分:0)

在你的构造函数中:

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

Play()方法中,您可以将HiLow类实例化为以下之一:

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

然后在输入Play()方法之前,只需提示用户所需的难度级别,并将他们选择的任何值传递给HiLow()构造函数。

就个人而言,我会将难度传递给Play()方法,并使用switch()if()语句来确定要调用的方法:

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

        // ...
    }
}