Android Studio开发Hangman游戏:如何实现玩家获胜判断逻辑?
Hey there! Let's get that win logic working for your Hangman game. The main missing pieces are checking if the player has guessed all letters correctly, triggering the victory AlertDialog, and resetting the game state for a new round. We'll also fix a small bug with your lives remaining calculation while we're at it.
First, Fix the Lives Remaining Bug
In your button click listener, you're redefining livesRemaining as a local variable, which means the global version never gets updated. Let's fix that:
// Inside the onClick method's error check block if (strSecret.indexOf(v.getTag().toString())<0){ intError++; // Remove the local int livesRemaining = 8; line livesRemaining = 8 - intError; // Update the global variable directly textViewScore.setText(String.valueOf(livesRemaining)); imageSwitcher.setImageResource(img[intError]); }
Add Win/Lose Condition Checks
After updating the displayed guessed letters, we need to check if the player has won or lost. Add these lines right after textView.setText(strText); in the onClick method:
// Check if player has guessed all letters correctly if (strText.equals(strSecret)) { showWinDialog(); } // Check if player has used all attempts else if (intError >= 8) { showLoseDialog(); }
Create the AlertDialog Methods
Add these two methods to your GameActivity class to show victory and game-over dialogs:
private void showWinDialog() { AlertDialog.Builder winBuilder = new AlertDialog.Builder(this); winBuilder.setTitle("You Won! 🎉") .setMessage("Great job! The secret word was: " + strSecret) .setPositiveButton("Play Again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { resetGame(); } }) .setCancelable(false) // Prevent closing the dialog without choosing an option .show(); } private void showLoseDialog() { AlertDialog.Builder loseBuilder = new AlertDialog.Builder(this); loseBuilder.setTitle("Game Over 😢") .setMessage("Better luck next time! The secret word was: " + strSecret) .setPositiveButton("Try Again", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { resetGame(); } }) .setCancelable(false) .show(); }
Implement Game Reset Logic
Add this method to reset all game state variables and UI elements for a new round:
private void resetGame() { // Reset core game variables intError = 0; livesRemaining = 8; strGuess = ""; strText = ""; // Update UI elements textViewScore.setText(String.valueOf(livesRemaining)); imageSwitcher.setImageResource(img[0]); // Reset to the empty hangman image // Re-enable all letter buttons and reset their color for (Button button : btn) { button.setEnabled(true); // Replace with your original button background color if needed button.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); } // Get a new secret word and update the display getSecretWord(); }
Final Notes
- Double-check that
android.content.DialogInterfaceis imported (it looks like you already have it, but better safe than sorry). - Adjust the button background color in
resetGame()to match your app's original button style if the default doesn't fit your design.
With these changes, your game will now detect when the player wins or loses, show the appropriate dialog, and reset everything for a new game when the user chooses to play again.
内容的提问来源于stack exchange,提问作者DWing

