私が作成している単純な数字推測ゲームのコードにスパイスを加えようとしています。ユーザーが適切な数字を推測すると、メッセージ ダイアログ ボックスが表示され、勝者であることを伝えます。そのメッセージ ダイアログ ボックスには、「OK」という名前のボタンが 1 つあり、クリックするとフォームに戻ります。
代わりに、新しい乱数が生成されるようにコードを再起動したいと思います。これは可能ですか、または勝者のコード領域内でコードをデフォルトの状態に手動で戻す必要がありますか?
ここに私のコードがあります:
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 ();
}