我正在做一个井字游戏。winningPlays棋盘上的每个方块都有一个从 0 到 8 的索引。为了检查获胜者,我有一个包含所有潜在获胜组合的二维数组。我有两个数组,分别包含 Xs 或 Os -xPlays和的播放oPlays。排序后xPlays,我想比较它以winningPlays查看是否有任何数组匹配。如果他们这样做,我想console.log('X wins')。我似乎找不到在正确的时间执行 console.log 以确定获胜者的方法。这是一段问题代码:
const winningPlays = [
[0,1,2], //across top
[3,4,5], //across middle
[6,7,8], //across bottom
[0,3,6], //left down
[1,4,7], //middle down
[2,5,8], //right down
[0,4,8], //top left to bottom right
[2,4,6] // top right to bottom left
]; //length == 8
function checkForWinner() {
for(let i = 0; i < winningPlays.length; i++){
for(let j = 0; j < winningPlays[i].length; j++){
if (xPlays.length < 3) {
return;
} else if (winningPlays[i][j] !== xPlays[j]) {
console.log(winningPlays[i][j])
console.log(xPlays[j])
return;
}
console.log('win') // executes every time that xPlays.length >= 3
}
}
};
这是我的 codepen 草稿的链接:https ://codepen.io/CDLWebDev/pen/gOawjvE
慕容森
慕码人8056858
汪汪一只猫
相关分类