SHARE
TWEET

Untitled

a guest Oct 18th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculateWinner(squares) {
  2.     const lines = [
  3.         [0, 1, 2],
  4.         [3, 4, 5],
  5.         [6, 7, 8],
  6.         [0, 3, 6],
  7.         [1, 4, 7],
  8.         [2, 5, 8],
  9.         [0, 4, 8],
  10.         [2, 4, 6],
  11.     ];
  12.     for (let i = 0; i < lines.length; i++) {
  13.         const [a, b, c] = lines[i];
  14.         if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
  15.             return squares[a];
  16.         }
  17.     }
  18.     return null;
  19. }
  20.  
  21. const calculateWinner = (squares) => {
  22.     const lines = [
  23.         [0, 1, 2],
  24.         [3, 4, 5],
  25.         [6, 7, 8],
  26.         [0, 3, 6],
  27.         [1, 4, 7],
  28.         [2, 5, 8],
  29.         [0, 4, 8],
  30.         [2, 4, 6],
  31.     ];
  32.  
  33.     const allXsOrOs = _.anyPass([
  34.         _.all(_.equals('X')),
  35.         _.all(_.equals('O'))
  36.     ]);
  37.  
  38.     const pickLine = _.props(_.__, squares);
  39.  
  40.     return _.pipe(
  41.         _.find(_.pipe(pickLine, allXsOrOs)),
  42.         _.cond([
  43.             [_.isNil, _.identity],
  44.             [_.T, _.pipe(_.head, _.nth(_.__, squares))]
  45.         ])
  46.     )(lines);
  47. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top