【问题标题】:Keeping track of points in simple console game在简单的控制台游戏中跟踪点
【发布时间】:2017-11-15 16:48:07
【问题描述】:

我很难把我的想法包裹在这件事上。每次玩家猜错时,都应该从初始余额中减去他/她的赌注。 由于它处于循环中,因此它总是从一开始就获取初始余额,每次都吐出相同的余额。 (明显地) 我尝试分配不同的变量,但似乎无法弄清楚。

我已经省略了中等难度和困难难度的方法,因为它们现在没有用,直到我弄清楚这个。

我的 Main() 只调用 setDifficulty()。没有其他的。

class Control
{
    int selectedNumber = 0;
    Random num = new Random();
    bool playAgain = true;
    int difficulty = 0;
    int bet = 0;
    int initialBalance = 20;
    int runningCredits = 0;
    int credits = 0;

    public Control()
    { }

    //sets game difficulty
    public void SetDifficulty() 
    {


        Console.Clear();
        Console.WriteLine("Please select level of difficulty between 1 - 100");
        difficulty = int.Parse(Console.ReadLine());


        if (difficulty >= 1 && difficulty <= 20)
            LetsPlayEasy();


        else if (difficulty >= 21 && difficulty <= 50)
            LetsPlayMedium();


        else if (difficulty >= 51 && difficulty <= 100)
            LetsPlayHard();


        else
        {
            SetDifficulty();
        }


    }

    //easy level method
    public void LetsPlayEasy()
    {
        //variables
        int userGuess;
        int numGuesses = 0;
        selectedNumber = num.Next(1, 101);

        Console.BackgroundColor = ConsoleColor.DarkYellow;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.White;

        Console.WriteLine("Difficulty level = EASY");

        Console.WriteLine("\nBeggining credit balance = " + initialBalance);
        Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
        bet = int.Parse(Console.ReadLine());

        do
        {
            Console.WriteLine("\nGuess a number between 1 and 100.");
            userGuess = Convert.ToInt32(Console.ReadLine());
            numGuesses++;

            UI output = new UI();
            output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);


            runningCredits = (initialBalance - bet);
            Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");

        } while (playAgain == true);
    }

class UI
{
    Random num = new Random();
    int keepGoing;

    public UI()
    { }

    //compare user's guess to selected number
    public void CompareNumbers(int userGuess, ref int selectedNumber, ref bool playAgain, ref int numGuesses)
    {
        Control difficulty = new Control();
        Admin say = new Admin();


        if (userGuess > selectedNumber)
        {
            Console.Beep(600, 300);
            Console.WriteLine("\nToo High! Guess Again!");

        }

        else if (userGuess < selectedNumber)
        {
            Console.Beep(300, 300);
            Console.WriteLine("\nToo Low! Guess Again!");

        }
        else
        {
            Console.Beep(350, 300);
            Console.Beep(380, 200);
            Console.Beep(380, 100);
            Console.Beep(500, 1100);
            Console.WriteLine("\n\nCongrats! It took you " + numGuesses + " guesses to win.");
            numGuesses = 0;
            Console.WriteLine("Press 1 to play again or 2 to quit.");
            keepGoing = int.Parse(Console.ReadLine());


            while (keepGoing != 1 && keepGoing != 2)
            {
                Console.WriteLine("\n\nPlease type either 1 or 2 only!");
                keepGoing = int.Parse(Console.ReadLine());
            }

            if (keepGoing == 2)
            {
                playAgain = false;
                say.Goodbye();
            }

            else
            {
                Console.Clear();
                difficulty.SetDifficulty();
            }

        }

    }
}

}

【问题讨论】:

    标签: c# console-application


    【解决方案1】:

    将 runningBalance 初始化为 initialBalance 并仅使用此值进行计算。如果只需要一次initialBalance,也可以不初始化runningBalance,直接将runningBalance切换为initialBalance即可。

     Console.WriteLine("\nBeggining credit balance = " + initialBalance);
     Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
     bet = int.Parse(Console.ReadLine());
     runningBalance = initialBalance
     do
     {
         Console.WriteLine("\nGuess a number between 1 and 100.");
         userGuess = Convert.ToInt32(Console.ReadLine());
         numGuesses++;
    
         UI output = new UI();
         output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);
    
    
         runningCredits -= bet;
         Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");
    
     } while (playAgain == true);
    

    【讨论】:

      【解决方案2】:
      runningCredits = (initialBalance - bet);
      

      您不会在循环中更改initialBalancebet,因此每次迭代都具有相同的runningCredits 值。

      在循环之外,这样做:

      runningCredits = initialBalance;
      

      在循环中,这样做:

      runningCredits -= bet;
      

      注意:您没有任何代码可以在循环中检查用户是否猜对或猜错(因此,用户总是输,而您总是减去赌注) .

      【讨论】:

      • 我最初对其进行了更改,但同样,它仍会重置为最初声明的内容,因此无法正常工作。
      • 比较答案的方法在另一个类的 if 语句中调用...另外,-= 有什么作用?对不起,如果你还不能说的话,这对这个很陌生。也感谢您的帮助@crashmstr
      • runniningCredits -= bet;runningCredits = runningCredits - bet; 相同。至于答案测试,您在这个级别需要它,因此返回一个布尔值或添加另一个引用参数(最好是返回值),以便您知道他们是赢还是输,以及是否从runningCredits 中添加或删除。
      • @MichaelM 说,你似乎有一个奇怪的划分,你的Control 中有用户输入,UI 中有游戏逻辑。
      猜你喜欢
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2017-08-10
      • 2014-06-11
      • 2013-12-08
      • 1970-01-01
      • 2011-08-30
      相关资源
      最近更新 更多