找到相同的数组时如何停止运行循环?

我正在做一个井字游戏。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


LEATH
浏览 130回答 3
3回答

慕容森

你有几个问题。首先,一旦发现不匹配,就从函数返回,但后面的元素winningPlays可能匹配。其次,您期望xPlays完全匹配其中一个winningPlays元素。但是 X 可以有额外的玩法。例如,如果xPlays = [2, 3, 4, 5], that should match[3, 4, 5] . What you really want to test is if all the elements of one of thewinningPlays elements are included inxPlays`,它们不必具有相同的索引。function checkForWinner() {&nbsp; if (xPlays.length < 3) {&nbsp; &nbsp; return;&nbsp; }&nbsp; for (let i = 0; i < winningPlays.length; i++) {&nbsp; &nbsp; let win = true;&nbsp; &nbsp; for (let j = 0; j < winningPlays[i].length; j++) {&nbsp; &nbsp; &nbsp; if (!xPlays.includes(winningPlays[i][j])) {&nbsp; &nbsp; &nbsp; &nbsp; win = false;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (win) {&nbsp; &nbsp; &nbsp; console.log('win');&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; }}

慕码人8056858

遍历每个子数组检查该子数组的所有项目是否都存在于玩家数组中function checkWinner() {&nbsp; &nbsp; return winningPlays.some(list => {&nbsp; &nbsp; &nbsp; &nbsp; return list.every(item => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return xPlays.includes(item);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });}

汪汪一只猫

我会通过执行以下操作使其更简单const winningPlays = [&nbsp; &nbsp; "0,1,2", //across top&nbsp; &nbsp; "3,4,5", //across middle&nbsp; &nbsp; "6,7,8", //across bottom&nbsp; &nbsp; "0,3,6", //left down&nbsp; &nbsp; "1,4,7", //middle down&nbsp; &nbsp; "2,5,8", //right down&nbsp; &nbsp; "0,4,8", //top left to bottom right&nbsp; &nbsp; "2,4,6" // top right to bottom left]; //length == 8function checkForWinner(xPlays) {&nbsp; &nbsp; var convertXPlays = xPlays.toString(); //if there are spaces in your array, make sure to remove it&nbsp; &nbsp; if (winningPlays.indexOf(convertXPlays) > -1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;console.log('win!');&nbsp; &nbsp; }};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript