给定一个嵌套的获胜条件数组,它们是索引,比较另一个固定长度的数组,如果给定符号与这些索引匹配,则 return 为真

Given an nested Array of winning conditions which are indexes compare another fixed length array and return true if given symbol matches those index

 let testArray = Array(9).fill("") 
 
  const winConditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];

  let xValue = "X";
  let oValue = "O";

testArray 在一次单击一个按钮时填充“X”和“O”。所以最终结果应该是例如。如果“X”位于 testArray 索引 0、1、2 return true

function checkWin(value, array) {
    return winConditions.some((cond) =>
        cond.every((index) => array[index] == value));
}

console.log(checkWin(xValue, testArray));
console.log(checkWin(oValue, testArray));

some return 如果任何值为真,则为真, every return 如果所有值都为真,则为真。

因此,如果值与所有数组值匹配,我们将检查每个条件,如果满足这些条件中的任何一个,则 return 为真。