0

I have this two array: linesToWin and mosseX: []. A possible configuration of mosseX is: mosseX[1,3,4,7]. I want to control if there are three number in mosseX that are equal to an array of linesToWin and return true or false dipending by the searching. Someone could help me?

calculateWinner = () => {
  const linesToWin = [
    [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 result = false;
  for (let i = 0; i < linesToWin.length; i++) {
    result = this.state.mosseX.filter((mossa) =>
      mossa.includes(linesToWin[i].map((u) => u))
    );
    if (result === true) return result;
  }
  return result;
};

xdeepakv
  • 7,835
  • 2
  • 22
  • 32
Alberto Manuguerra
  • 109
  • 1
  • 1
  • 7
  • Does this answer your question? [Check if every element in one array is in a second array](https://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array) – Heretic Monkey Apr 09 '20 at 14:56

1 Answers1

0

You can simply contact array and compare.

Sample:

calculateWinner = () => {
  const linesToWin = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  function merge(data = []) {
    return data.join("-");
  }
  const mosseX = merge(this.state.mosseX);
  return linesToWin.some((line) => merge(line) === mosseX);
};

For testing:

calculateWinner = (mosseX) => {
  const linesToWin = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  function merge(data = []) {
    return data.join("-");
  }
  // For testing
  // const mosseX = merge(this.state.mosseX);
  mosseX = merge(mosseX);
  return linesToWin.some((line) => merge(line) === mosseX);
};
console.log(calculateWinner([6,7,1]))
console.log(calculateWinner([6,7,8]))
console.log(calculateWinner([1,7,8]))
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • his code was doing the same. I suggested simplifying the solution. and i think in this game order is imp. – xdeepakv Apr 09 '20 at 15:28
  • Sorry but I want the same return for this three cases. And, I need to do this control in a different moment so, I never change my mosseX array. ```console.log(calculateWinner([6,7,1])) console.log(calculateWinner([6,1,7])) console.log(calculateWinner([1,7,6])) ``` also, my mosseX array could be.... ``` mosseX=[1,4,2,7,6] ``` – Alberto Manuguerra Apr 09 '20 at 15:36
  • @AlbertoManuguerra What is the expectation? I think current solution match your description. – xdeepakv Apr 09 '20 at 15:38
  • Perfect. It function but I don't remember how I can wait for setState to finish operations before to do other things. Do you know this too? – Alberto Manuguerra Apr 09 '20 at 16:13
  • You can use callack in setState. `this.setState(pre => { const isWinner = calculateWinner(pre) this.setState({isWinner: isWinner}) }) ` – xdeepakv Apr 09 '20 at 16:16
  • I have another worning. The function that @xdeepakv give me doesn't work when in mosseX array I have element such us [2, 5,1, 8]. I searched it into the browser but I don't find the right answer. – Alberto Manuguerra Apr 11 '20 at 13:44