Visual C# Hangman 按钮单击事件

本文关键字:单击 事件 按钮 Hangman Visual | 更新日期: 2023-09-27 18:33:51

我正在尝试用Visual C#创建一个Hangman游戏,但是我在字母按钮单击事件时遇到问题。

这是它应该如何工作的:玩家点击一个字母按钮(A-Z)。如果该按钮的文本位于他应该猜测的单词 (guessWord) 中,则会显示该字母,并且 correctGuess 计数器将递增。如果这封信不在猜字中,他剩下的机会就会减少,刽子手就会抽出一块。当正确猜测>=猜测词的长度时,玩家赢得比赛,当他没有更多机会时,他输掉了比赛(他从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();
        }
    }
}

Visual C# Hangman 按钮单击事件

你的问题在这里:

//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而不是gameOverMsg2gameOverWinMsg而不是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());