如何为JavaScript石头剪刀布游戏的不同难度设置不同的可操作次数?
Hey there! Let's get your difficulty system working and put an end to that infinite game loop—you're already really close, just a few small fixes needed.
First, Fix the Difficulty Selection Bug
Looking at your selectDifficulty function, the main issue is that you're not using the difficulty value being passed from the buttons! Right now, you define difficultyRating as an array (["easy", "normal", "hard"]) and compare that to strings like "easy"—which will never match. That's why your counter always defaults to the normal difficulty's 5 moves, no matter which button you click.
Here's the corrected function, plus we'll make movesCounter a global variable so it persists across game rounds:
// Global variable to track remaining moves (persists across rounds) let movesCounter = 0; function selectDifficulty(selectedDifficulty) { // Use an object to map difficulties to move counts (cleaner than if/else chains) const difficultyMoves = { easy: 7, normal: 5, hard: 3 }; // Set the counter based on the selected difficulty, default to normal if something goes wrong movesCounter = difficultyMoves[selectedDifficulty] || difficultyMoves.normal; // Update the displayed move count document.getElementById("moves-counter").textContent = movesCounter; // Optional: Reset game state when switching difficulty (scores, results, etc.) resetGame(); } // Example reset function (adjust based on your existing game code) function resetGame() { // Reset player/computer scores, clear result messages, etc. }
Next, Stop the Infinite Game: Track Moves Per Round
Now that we have a working move counter, we need to decrement it every time the player makes a choice and end the game when it hits 0. Let's assume you have a function like playerMakesChoice(choice) that runs when the player picks rock/paper/scissors—add this logic to it:
function playerMakesChoice(playerPick) { // Your existing game logic goes here: // - Generate computer's choice // - Compare choices to determine winner // - Update scores and show results // Decrement the move counter after each round movesCounter--; document.getElementById("moves-counter").textContent = movesCounter; // Check if we're out of moves if (movesCounter <= 0) { endGame(); } } function endGame() { // Show final game results (customize this to match your game's style) alert(`Game over! Final Score: Player ${playerScore} vs Computer ${computerScore}`); // Optional: Reset to default difficulty or let the player pick a new one selectDifficulty("normal"); }
Quick Note on Your HTML
Your button setup is already correct—they're passing the right difficulty values to the function. Just make sure your rock/paper/scissors buttons call the playerMakesChoice function we added above when clicked.
Why This Works
- The corrected
selectDifficultyuses the actual value from the button to set the move count - A global
movesCounterkeeps track of remaining plays across all rounds - After every player choice, we reduce the counter and check if it's time to end the game
That should get your difficulty levels functioning properly and stop the infinite game loop. Let me know if you need help tweaking this to fit your existing game code!
内容的提问来源于stack exchange,提问作者AriRix1410

