Javascript 当发现相同的数组时,如何停止运行循环?

Javascript 当发现相同的数组时,如何停止运行循环?,javascript,arrays,Javascript,Arrays,我正在做一个井字游戏。棋盘上的每个方块都有一个从0到8的索引。要检查赢家,我有一个二维数组winningPlays,其中包含所有可能的赢家组合。我有两个数组,分别包含Xs或Os播放的节目-xPlays和oPlays。对xPlays进行排序后,我想将其与winningPlays进行比较,以查看是否有任何数组匹配。如果有,我想console.log('X wins')。我似乎找不到在正确的时间执行console.log以确定获胜者的方法。下面是一段问题代码: const winningPlays =

我正在做一个井字游戏。棋盘上的每个方块都有一个从0到8的索引。要检查赢家,我有一个二维数组
winningPlays
,其中包含所有可能的赢家组合。我有两个数组,分别包含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
            }
        } 
    };
const winningPlays=[
[0,1,2],//穿过顶部
[3,4,5],//穿过中间
[6,7,8],//穿过底部
[0,3,6],//左下
[1,4,7],//中间向下
[2,5,8],//右下
[0,4,8],//从左上到右下
[2,4,6]//从右上到左下
]; //长度==8
函数checkForWinner(){
for(设i=0;i=3时执行
}
} 
};

这里是我的代码笔草稿的链接:

你有几个问题

首先,一旦发现不匹配,您就从函数返回,但是后面的
winningPlays
元素可能匹配

其次,您希望
xPlays
与其中一个
winning plays
元素完全匹配。但是X可以有更多的剧本。例如,如果
xPlays=[2,3,4,5],则应该匹配
[3,4,5]
。您真正想要测试的是,如果其中一个
winningPlays
元素的所有元素都包含在
xPlays`中,则它们不必具有相同的索引

函数checkForWinner(){
如果(xPlays.length<3){
返回;
}
for(设i=0;i
  • 遍历每个子数组
  • 检查该子阵列的所有项目是否都存在于玩家阵列中

我会像下面这样做,让事情变得更简单

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(xPlays) {
    var convertXPlays = xPlays.toString(); //if there are spaces in your array, make sure to remove it
    if (winningPlays.indexOf(convertXPlays) > -1)
    {
       console.log('win!');
    }
};

xPlays.length
检查不应该在循环中。这种检查获胜者的方法看起来并不太好,老实说。我会解析你的棋盘以查找线条。你可以编写一个函数来检查玩家是否获胜。您最好的朋友将是
for
循环。这是一个极好且优雅的解决方案!事实上,我已经意识到我最终会遇到第二个问题,但我试图把它们分开,一次解决一个。但这一举解决了这两个问题。非常感谢,@Barmar
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(xPlays) {
    var convertXPlays = xPlays.toString(); //if there are spaces in your array, make sure to remove it
    if (winningPlays.indexOf(convertXPlays) > -1)
    {
       console.log('win!');
    }
};