[英]Visual C# Hangman Button-click event
我試圖在Visual C#中創建一個Hangman游戲,但是遇到字母按鈕單擊事件時遇到了麻煩。
它的工作方式如下:玩家單擊字母按鈕(AZ)。 如果該按鈕的文本在他應該猜的單詞(guessWord)中,則會顯示字母,並增加正確的Guess計數器。 如果這封信中沒有猜測字詞,那么他離開的機會就會減少,並且會抽出一名子手。 當正確的猜詞> =猜詞的長度時,玩家將贏得游戲,而當他沒有更多機會時,他將輸掉游戲(他從8開始)。
我不知道當前代碼有什么問題。 有時,當我運行它時,只需單擊一個字母按鈕,我就會得到“您迷路了”的框。 有時,當猜出正確的字母時,程序會顯示字母,然后顯示“您丟失了”框。
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();
}
}
}
您的問題在這里:
//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
。
您在這里遇到問題:
//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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.