我试图在Visual C#中创建一个Hangman游戏,但我遇到了按钮单击事件的问题。
以下是它应该如何工作:玩家点击一个字母按钮(A-Z)。如果该按钮的文本在他应该猜测的单词中(guessWord),则显示该字母并且正确的Guess计数器递增。如果这封信不在guessWord中,那么他剩下的机会数量会减少,而且还会抽出一块刽子手。当correctGuess> = guessWord的长度时,玩家赢得游戏,并且当他没有更多机会时他失去了游戏(他从8开始)。
我不知道当前代码的问题是什么。有时当我运行它时,我会得到"你输了#34;单击一个字母按钮后的框。有时,当猜到正确的字母时,程序会显示该字母,但随后会显示"您丢失了#34;框。
void LetterBtn_Click(object sender, EventArgs e)
{
//Event hook-up
Button b = (Button)sender;
char letterClicked = b.Text.ToCharArray()[0];
//Disable button after it's been clicked
b.Enabled = false;
string gameOverTitle1 = "Congrats!";
string gameOverTitle2 = "Sorry!";
string gameOverMsg1 = "You won!";
string gameOverMsg2 = "You lost!";
char[] guessWordChars = guessWord.ToCharArray();
//If the character is in the word
if ((guessWord = guessWord.ToUpper()).Contains(letterClicked))
{
for (int i = 0; i < guessWord.Length; i++)
{
if (guessWordChars[i] == letterClicked)
{
//Reveal the character
lblguessWord[i].Font = new Font("Bauhaus 93", 28, FontStyle.Regular);
lblguessWord[i].Text = letterClicked.ToString();
lblguessWord[i].ForeColor = Color.Fuchsia;
//Increment counter
correctGuess += 1;
}
}
//Winning condition
if (correctGuess >= guessWordChars.Length)
{
return;
}
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
}
else
{
//Incorrect guess
if (chances > 0)
{
chances -= 1;
}
//Player lost the game
else
{
//Reveal the word
for (int k = 0; k < guessWordChars.Length; k++)
{
lblguessWord[k].Text = guessWordChars[k].ToString();
}
//Tell the player he lost the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle1, gameOverMsg1);
frmGameOverBoxInstance.ShowDialog();
}
}
}
答案 0 :(得分:2)
你的问题在这里:
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
您正在混合自己的标签。 gameOverMsg2
包含您的You Lost!
字符串。改为将其改为:
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle1, gameOverMsg1);
frmGameOverBoxInstance.ShowDialog();
您的else
子句中存在同样的问题:
//Tell the player he lost the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle1, gameOverMsg1);
frmGameOverBoxInstance.ShowDialog();
将其更改为:
//Tell the player he lost the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
您应该为变量名称提供更具描述性的标题以避免这种情况,例如gameOverLostMsg
而不是gameOverMsg2
或gameOverWinMsg
而不是gameOverMsg1
。
答案 1 :(得分:1)
你有问题:
//Winning condition
if (correctGuess >= guessWordChars.Length)
{
return;
}
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
如果用户已正确猜出所有字母,则此代码返回,如果他/她正确猜测但尚未获胜,则会向用户提供获胜消息。您可能想要执行以下操作:
//Winning condition
if (correctGuess >= guessWordChars.Length)
{
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
}
除此之外,在开头添加一些调试代码以确保正确初始化变量,如下所示:
Debug.WriteLine("correctGuess: " + correctGuess.ToString());
Debug.WriteLine("chances: " + chances.ToString());