[英]Visual C# Hangman Button-click event
I'm trying to create a Hangman game in Visual C#, but I'm having trouble with the letter button-click events. 我试图在Visual C#中创建一个Hangman游戏,但是遇到字母按钮单击事件时遇到了麻烦。
Here's how it's supposed to work: a player clicks on a letter button (AZ). 它的工作方式如下:玩家单击字母按钮(AZ)。 If the text of that button is in the word he's supposed to guess (guessWord), the letter is revealed and the correctGuess counter is incremented. 如果该按钮的文本在他应该猜的单词(guessWord)中,则会显示字母,并增加正确的Guess计数器。 If the letter isn't in guessWord, the number of chances he has left decreases and a piece of the hangman is drawn. 如果这封信中没有猜测字词,那么他离开的机会就会减少,并且会抽出一名子手。 The player wins the game when the correctGuess >= the length of the guessWord, and he loses the game when he has no more chances (he starts with 8). 当正确的猜词> =猜词的长度时,玩家将赢得游戏,而当他没有更多机会时,他将输掉游戏(他从8开始)。
I don't know what the problem is with my current code. 我不知道当前代码有什么问题。 Sometimes when I run it, I will get the "You lost" box after clicking just one letter button. 有时,当我运行它时,只需单击一个字母按钮,我就会得到“您迷路了”的框。 Sometimes, when a correct letter is guessed, the program reveals the letter but then displays the "You lost" box. 有时,当猜出正确的字母时,程序会显示字母,然后显示“您丢失了”框。
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();
}
}
}
Your problem is here: 您的问题在这里:
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
You are mixing up your own labels. 您正在混淆自己的标签。 gameOverMsg2
contains your You Lost!
gameOverMsg2
包含您的You Lost!
string. 串。 Change it to this instead: 改为:
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle1, gameOverMsg1);
frmGameOverBoxInstance.ShowDialog();
You have the same problem here, in your else
clause: 您在else
子句中有相同的问题:
//Tell the player he lost the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle1, gameOverMsg1);
frmGameOverBoxInstance.ShowDialog();
Change it to this: 更改为此:
//Tell the player he lost the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
You should give your variable names more descriptive titles to avoid this, such as gameOverLostMsg
instead of gameOverMsg2
or gameOverWinMsg
instead of gameOverMsg1
. 您应该给变量名更多描述性的标题,以避免出现这种情况,例如gameOverLostMsg
代替gameOverMsg2
或者gameOverWinMsg
代替gameOverMsg1
。
You have a problem here: 您在这里遇到问题:
//Winning condition
if (correctGuess >= guessWordChars.Length)
{
return;
}
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
This code returns if the user has guessed all the letters correctly, and gives the user the winning message if s/he guess correctly but has not yet won. 此代码返回用户是否猜中所有的字母,并为用户提供获胜的消息,如果他/她猜对,但还没有取得胜利。 You probably want to do the following instead: 您可能想要执行以下操作:
//Winning condition
if (correctGuess >= guessWordChars.Length)
{
//Tell the player he won the game
frmGameOverBox frmGameOverBoxInstance = new frmGameOverBox(gameOverTitle2, gameOverMsg2);
frmGameOverBoxInstance.ShowDialog();
}
Beyond that, add some debugging code at the beginning to ensure your variables are correctly initialized, like so: 除此之外,在开头添加一些调试代码,以确保正确初始化变量,如下所示:
Debug.WriteLine("correctGuess: " + correctGuess.ToString());
Debug.WriteLine("chances: " + chances.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.