JavaScript井字棋游戏胜利判定函数实现技术求助
Hey there! Let's get that win detection working smoothly for your Tic Tac Toe game. The core idea here is straightforward: we need to check if any of the winning combinations is a full subset of the player's moves array. Here's a clean, reliable way to implement it:
The checkWin Function Implementation
The every() array method is perfect for this scenario—it lets us verify that every element in a winning combo exists in the player's moves. Here's the code:
function checkWin(winArr, movesArr) { // Loop through each possible winning combination for (const winCombo of winArr) { // Check if all three numbers in the combo are present in the player's moves const hasWon = winCombo.every(number => movesArr.includes(number)); if (hasWon) { // We found a winning combo! Return true to signal a win return true; } } // No winning combo matched return false; }
How This Works:
for...ofiterates over each 3-number winning set in yourwinArr.winCombo.every(...)ensures all three elements of the combo are present in the player'smovesArr—no partial counts, only full matches count as a win.- We return
trueimmediately as soon as a winning combo is found (no need to check other combos once a win is confirmed).
Optional Optimization: Use a Set for Faster Lookups
While the difference is negligible for Tic Tac Toe (max 9 moves), using a Set instead of an array for lookups is more efficient (O(1) vs O(n) time). Here's the optimized version:
function checkWin(winArr, movesArr) { const movesSet = new Set(movesArr); // Convert moves array to a Set for faster lookups for (const winCombo of winArr) { const hasWon = winCombo.every(number => movesSet.has(number)); if (hasWon) { return true; } } return false; }
Fixing the currentMove Function Bug
I spotted a small issue in your existing currentMove function: you're checking both players' moves every time, even after only one player has taken a turn. Let's adjust that to only check the current player's moves right after they play:
function currentMove(square){ var squareNum = square.data('squarenumber'); square.off(); if(game.player){ square.html('x'); game.p1Moves.push(squareNum); game.player = false; // Check if Player 1 won after their move const p1Won = checkWin(game.win, game.p1Moves); if (p1Won) { game.p1Points++; alert("Player 1 wins!"); // Add your game reset logic here } } else{ square.html('0'); game.p2Moves.push(squareNum); game.player = true; // Check if Player 2 won after their move const p2Won = checkWin(game.win, game.p2Moves); if (p2Won) { game.p2Points++; alert("Player 2 wins!"); // Add your game reset logic here } } }
This way, we only check the player who just made a move—logical, and avoids unnecessary checks.
Why Your Previous Attempts Might Have Failed
If you tried nested loops before, you might have missed verifying that all three elements of a winning combo are present in the moves array. The every() method handles this automatically, ensuring no partial matches slip through—exactly what we need for a valid win.
内容的提问来源于stack exchange,提问作者JamesVitaly




