第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

找到相同的數(shù)組時(shí)如何停止運(yùn)行循環(huán)?

找到相同的數(shù)組時(shí)如何停止運(yùn)行循環(huán)?

LEATH 2022-07-08 15:59:39
我正在做一個(gè)井字游戲。winningPlays棋盤上的每個(gè)方塊都有一個(gè)從 0 到 8 的索引。為了檢查獲勝者,我有一個(gè)包含所有潛在獲勝組合的二維數(shù)組。我有兩個(gè)數(shù)組,分別包含 Xs 或 Os -xPlays和的播放oPlays。排序后xPlays,我想比較它以winningPlays查看是否有任何數(shù)組匹配。如果他們這樣做,我想console.log('X wins')。我似乎找不到在正確的時(shí)間執(zhí)行 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
查看完整描述

3 回答

?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

你有幾個(gè)問題。


首先,一旦發(fā)現(xiàn)不匹配,就從函數(shù)返回,但后面的元素winningPlays可能匹配。


其次,您期望xPlays完全匹配其中一個(gè)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() {

  if (xPlays.length < 3) {

    return;

  }

  for (let i = 0; i < winningPlays.length; i++) {

    let win = true;

    for (let j = 0; j < winningPlays[i].length; j++) {

      if (!xPlays.includes(winningPlays[i][j])) {

        win = false;

        break;

      }

    }

    if (win) {

      console.log('win');

      break;

    }

  }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-08
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

遍歷每個(gè)子數(shù)組

檢查該子數(shù)組的所有項(xiàng)目是否都存在于玩家數(shù)組中

function checkWinner() {

    return winningPlays.some(list => {

        return list.every(item => {

            return xPlays.includes(item);

        });

    });

}


查看完整回答
反對(duì) 回復(fù) 2022-07-08
?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

我會(huì)通過執(zhí)行以下操作使其更簡單


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!');

    }

};


查看完整回答
反對(duì) 回復(fù) 2022-07-08
  • 3 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)