当前位置: 首页 > 知识库问答 >
问题:

简单游戏机游戏中的点数跟踪

夹谷成仁
2023-03-14

我很难把我的心思放在这件事上。每当玩家猜错时,它应该从最初的余额中减去他/她的赌注。由于它是在一个循环中,它总是从一开始就取初始平衡,每次都吐出相同的平衡。(很明显)我试过分配不同的变量,但似乎无法找到答案。

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

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

        }

    }
}

共有1个答案

乌翔
2023-03-14

将runningBalance初始化为initialBalance并仅将此值用于计算。如果只需要initialBalance一次,也可以通过简单地将runningBalance切换到initialBalance而不初始化runningBalance来完成。

 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);
 类似资料:
  • 我正在尝试用libgdx开发一个简单的蛇游戏。我的问题是,每次我想要繁殖一些苹果(纹理,20px宽X 20px高),它总是与蛇的身体重叠。我试图避免这种情况,但它在比赛中不断发生。 snake由多个部分组成-每个部分都是一个20px宽X 20px高的纹理(屏幕宽度是480px宽X 800px高) 以下是我迄今为止所做的尝试: 代码很容易解释。每时每刻,屏幕上都有3个不同的苹果。这段代码试图抽奖x-

  • 我一直在尝试制作一个简单的游戏,计算机生成一个随机数,你试着猜它。它还存储了你“尝试”的猜测量。 但是,当我运行该程序时,它只是打印:“让我们玩游戏。我会想到一个数字 1-100。试着猜猜。 这是我的代码: 我不明白为什么这不起作用,有人能解释一下为什么不起作用吗?

  • 简单的推箱子游戏,纯Objective-C代码完成。边界检测,碰撞检测,全部底层代码完成。游戏规则是控制超级玛丽,将炸弹推到怪物的方格里面。

  • 本文向大家介绍python简单猜数游戏实例,包括了python简单猜数游戏实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python简单猜数游戏。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的Python程序设计有所帮助。

  • 本文向大家介绍python实现的简单猜数字游戏,包括了python实现的简单猜数字游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python实现的简单猜数字游戏。分享给大家供大家参考。具体如下: 给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好。 希望本文所述对大家的Python程序设计有所帮助。