为什么我的价值不重置后游戏结束 [英] why is my value not resetting after game ends

查看:199
本文介绍了为什么我的价值不重置后游戏结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是一个猜测游戏,用户猜测一个数字在1-100之间。他们有10次尝试,每次猜测后告诉他们太低或太高等。

My code is for a guessing game, user guesses a number between 1-100. They have 10 tries, after each guess it tells them if its too low or too high, etc.

我有一个问题,我似乎无法修复是他们赢了后告诉他们他们赢了,以及他们赢了多少猜测(使用猜测变量)。我的问题是,如果他们赢了,他们再玩,猜测不重置。所以,如果他们在第一场比赛中尝试5次,在第二场比赛中尝试8次,在第二场比赛中,他们会告诉他们他们赢了13次猜测(5次从第一次+ 8次)。

A problem I'm having that I can't seem to fix is that after they won it tells them they won and how many guesses it took them to win (using guesses variable). My problem is that if they win, and they play again, the guesses doesn't reset. So if it takes them 5 tries on the first game, and 8 tries on the second game, in the second game it will tell them they won in 13 guesses (5 from first + 8 from second).

我知道为什么会发生,但我尝试的一切似乎并没有解决它。我假设如果我只是做同样的地方, guesses = 0; 我做 attemptsLeft = 10; 会工作,因为 attemptsLeft 工作正常,但然后它只是总是说他们赢了,它花了他们0猜测。我也尝试在 if 里面设置 guesses = 0;

I know why it's happening, but everything I try doesn't seem to fix it. I assumed if i just did something like guesses = 0; in the same spot I do attemptsLeft = 10; that it would work since the attemptsLeft is working fine, but then it just always says they won and it took them 0 guesses. I also tried setting guesses = 0; inside the if statements for when they win/lose but it does the same thing and says they won in 0 guesses.

namespace NumberGuessingGame
{
    public class GuessingGame
    {

        int myGuess = 0;
        int guessesLeft = 10;
        public int gamesPlayed = 0;
        public int gamesWon = 0;
        public int gamesLost = 0;
        public int attemptsLeft = 10;
        public int guesses = 0;

        Random rand;
        int number = 0;

        public GuessingGame()
        {           
            attemptsLeft = 10;
            rand = new Random();
            number = rand.Next(1, 100);            
        }
        public void ResetGame()
        {            
            attemptsLeft = 10;
            number = rand.Next(1, 100);
            guessesLeft = 10;            
        }                
        public int CheckGuess(int newGuess)
        {
            myGuess = newGuess;

            if (guessesLeft < 1)
            {               
                gamesLost++;
                gamesPlayed++;
                ResetGame();                
                return 2;                
            }
            else if (myGuess > number)
            {
                attemptsLeft--;
                guesses++;
                guessesLeft--;
                return 1;
            }
            else if (myGuess < number)
            {                               
                attemptsLeft--;
                guesses++;
                guessesLeft--;
                return -1;
            }
            else
            {               
                guesses++;
                gamesWon++;
                gamesPlayed++;
                ResetGame();                
                return 0;
            }
        }
    }
}



for form class



Code for form class

namespace NumberGuessingGame
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();           
        }        

        GuessingGame myGuess = new GuessingGame();

        private void btnCheck_Click(object sender, EventArgs e)
        {
            int inputGuess = Convert.ToInt32(txtGuess.Text);
            int result = myGuess.CheckGuess(inputGuess);                                           

            if(result == 2)
            {
                lblLosses.Text = "Losses: " + myGuess.gamesLost;
                lblPlayed.Text = "Games Played: " + myGuess.gamesPlayed;
                lblWins.Text = "Wins: " + myGuess.gamesWon;                                       
                MessageBox.Show("You ran out of guesses and lost!");                
            }
            else if(result == 1)
            {               
                MessageBox.Show("Your guess was too high, try again!" + "\n You have " + myGuess.attemptsLeft + " guesses left");
            }
            else if(result == -1)
            {
                MessageBox.Show("Your guess was too low, try again!" + "\n You have " + myGuess.attemptsLeft + " guesses left");
            }
            else if(result == 0)
            {                
                MessageBox.Show("You won!" + "\n It took you " + myGuess.guesses + " guesses");                
                lblLosses.Text = "Losses: " + myGuess.gamesLost;
                lblPlayed.Text = "Games Played: " + myGuess.gamesPlayed;
                lblWins.Text = "Wins: " + myGuess.gamesWon;    
            }                       
        }


推荐答案

I猜猜我很困惑为什么你增加猜测和尝试的数量。你不能仅仅使用逻辑来完成游戏的流程,而不是跟踪2个事情,这本质上意味着相同的事情?当你重新玩游戏,看看每个变量等于什么,你正确地重置每个变量和调用该函数?祝你好运。

I guess I'm kind of confused why you increment both number of guesses and attempts left. Can't you just use the logic from either to accomplish the flow of the game instead of keeping track of 2 things that essentially mean the same thing? When you re-play the game see what each variable equates too, are you properly resetting each variable and calling that function? Good luck.

这篇关于为什么我的价值不重置后游戏结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆