javascript 有没有办法把一个数组的值和另一个数组的值匹配起来?

5jvtdoz2  于 2023-02-07  发布在  Java
关注(0)|答案(3)|浏览(159)

我正在尝试创建一个winCondition,其中的activePlayer是player1或player2,与任何保存特定值的数组匹配。
我设置了我的板,每个按钮都有自己的data-number,从0到8。
当activePlayer单击其中一个按钮时,它们会将该data-number推入自己的数组

const player1 = Players("Player 1", 0, "X", []);
 const player2 = Players("Player 2", 0, "O", []);

然后,我想检查这些玩家数组值是否与任何获胜组合(markersToHit)匹配。markersToHit是一个包含多个数组的数组,所有这些数组都是井字游戏中获胜的所有可能方法。从上到下,从左到右或从右到左,以及对角线。

const checkWinAndTie = () => {
    const winCheck = () => {
      const markersToHit = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
      ]

      //I want to check if the activePlayer PlayerHits(which holds an array), matches
      //with any of the values inside markersToHit
      //if they match, I want to  display the activePlayer has won the round and add a score to them

      for (let i = 0; i < markersToHit.length; i++) {
        console.log(markersToHit[i]);
        if (markersToHit[i] === getActivePlayer().playerHits) {
          console.log(`${getActivePlayer().name} wins!`);
        }
      }
      console.log(`${getActivePlayer().name} :${getActivePlayer().playerHits}`);
    }

    winCheck();
  }

我试着做一个if语句来匹配winningCombination中的值和数组中的activePlayer值。我希望它能输出哪个玩家赢了这一轮,但我只是停留在这一刻。老实说,已经五天了。
你知道我该怎么做吗?
如果你还不清楚的话,这是代码,控制台显示了我所说的播放器点击率:https://codepen.io/jimmyjimenez2400/pen/ZEjvrPq

pgky5nke

pgky5nke1#

代码中的所有注解:

// Copied from your code, the winning combinations
const markersToHit = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6],
];

// Call this function with an array holding the player's current choices
function isItAWinner(choices) {
  let winner = false;
  // Loop through all winning combinations
  markersToHit.forEach(winningCombination => {
  
    // If a winning combination has already been found, skip
    if(winner) return;
    
    // matchers contains the intersection of the player's choices and the current winning combination
    let matches = winningCombination.filter(singleMarker => {
      return choices.includes(singleMarker)
    });
    
    // If all three necessary choices of this winning combination are matched, this player wins
    if (matches.length === 3) {
      console.log("Winning combination: " + matches);
      winner = true;
    }
  });
  return winner;
}

// An example: player's choices (spoiler: they win)
const playersChoices = [0, 3, 8, 6];
console.log(isItAWinner(playersChoices));
unftdfkk

unftdfkk2#

const checkWinAndTie = () => {
    const winCheck = () => {
      const markersToHit = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
      ]

      //I want to check if the activePlayer PlayerHits(which holds an array), matches
      //with any of the values inside markersToHit
      //if they match, I want to  display the activePlayer has won the round and add a score to them

      for (let i = 0; i < markersToHit.length; i++) {
        console.log(markersToHit[i]);
-       if (markersToHit[i] === getActivePlayer().playerHits) {
+       if (getActivePlayer().playerHits.every((val, j) => val === markersToHit[i][j]))
          console.log(`${getActivePlayer().name} wins!`);
        }
      }
      console.log(`${getActivePlayer().name} :${getActivePlayer().playerHits}`);
    }

    winCheck();
  }
wtlkbnrh

wtlkbnrh3#

您正在检查这两个数组是否是同一个对象,但您希望检查数组的元素是否包含在播放器的值中。

for (let i = 0; i < markersToHit.length; i++) {
        const hasRow = markersToHit[i].every(f => getActivePlayer().playerHits.includes(""+f));
        if (hasRow) {
          console.log(`${getActivePlayer().name} wins!`);
        }
      }

请注意,player数组和marks数组中的值属于不同的类型(string与int),因此您必须修改其中一个,或者与==而不是===进行比较。

相关问题