[英]Restarting code when user clicks message dialogue “ok” button
I am trying to spice up my code for a simple number guessing game I am making. 我正在尝试为我正在制作的一个简单的猜数游戏调整我的代码。 Once the user guesses the proper number a message dialogue box pops up telling him he is a winner.
一旦用户猜出正确的号码,就弹出一个消息对话框,告诉他他是胜利者。 That message dialogue box has one button named "ok" when I click it I am taken back to the form.
当我点击它时,该消息对话框有一个名为“ok”的按钮我被带回到表单中。
Instead I would like it to restart the code so that a new random number is generated. 相反,我希望它重新启动代码,以便生成一个新的随机数。 Is this possible or do I need to manually revert the code back to its default state within the winner code area?
这是可能的还是我需要手动将代码恢复到赢家代码区域内的默认状态?
Here is my code now: 这是我现在的代码:
private void btnEval_Click (object sender, EventArgs e)
{
// increment the counter to be displayed later
howManyClicks++;
// main decision conditions, ensures something is entered
if (txtNum.Text != "")
{
// user input number
double num = double.Parse (txtNum.Text);
if (randomNumber > num)
{
// if too low
this.BackColor = Color.Yellow;
lblMain.Text = "TOO LOW!";
lblReq.Text = "please try again";
txtNum.Clear ();
txtNum.Focus ();
}
else if (randomNumber < num)
{
// if too high
this.BackColor = Color.Red;
lblMain.Text = "TOO HIGH!";
lblReq.Text = "please try again";
txtNum.Clear ();
txtNum.Focus ();
}
else
{
// correct
this.BackColor = Color.Green;
lblMain.Text = "CORRECT!";
lblReq.Text = "well done";
MessageBox.Show ("You are right!! It took you " + howManyClicks + " guesses", "You are a WINNER!!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
txtNum.Clear ();
txtNum.Focus ();
}
}
else
{
MessageBox.Show ("You must enter a vaild number! Please try again.", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
txtNum.Clear ();
txtNum.Focus ();
}
Apparently, your "game state" ( howManyClicks
, randomNumber
, ...) is stored in instance variables of the form. 显然,您的“游戏状态”(
howManyClicks
, randomNumber
,...)存储在表单的实例变量中。 Thus, you have the following options: 因此,您有以下选择:
Extract the game state into its own class and keep only a reference to an instance of this class in your form: 将游戏状态提取到自己的类中,并在表单中仅保留对此类实例的引用:
GameState state;
When you start or restart the game, just assign a new GameState()
to state
and reset the user interface elements of the form. 当您启动或重新启动游戏时,只需指定一个
new GameState()
来state
并重置表单的用户界面元素。
Alternatively, you can close and reopen the form. 或者,您可以关闭并重新打开表单。 Your main loop could look something like this:
你的主循环看起来像这样:
while (true) {
var form = new GameForm();
var result = form.ShowDialog(); // waits until the form as been closed
if (result == DialogResult.Cancel) {
break; // The user wants to stop playing
}
}
In your game form, when the OK
button is clicked, you set Me.DialogResult
to DialogResult.OK
and then close the form. 在游戏表单中,单击“
OK
按钮时,将Me.DialogResult
设置为DialogResult.OK
,然后关闭表单。 The outer loop will automatically reopen a new, empty game form. 外循环将自动重新打开一个新的空游戏表单。
Simply make function for this code. 只需为此代码生成函数即可。
Select all the code and then right click on it, select second option: Refactor
选择所有代码,然后右键单击它,选择第二个选项:
Refactor
Select suboption: Extract a method
. 选择子选项:
Extract a method
。
You can give name for this public method. 您可以为此公共方法指定名称。
Then you can call this method from anywhere,( as you have mentioned restart the code). 然后你可以从任何地方调用这个方法(正如你提到的那样重启代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.