简体   繁体   中英

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.

Here's how it's supposed to work: a player clicks on a letter button (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. 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).

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! 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:

//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 .

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM