Skip to content

Instantly share code, notes, and snippets.

@szmeku
Last active October 1, 2019 10:54
Show Gist options
  • Save szmeku/452b47e0cd688a1097a116d783e8e43e to your computer and use it in GitHub Desktop.
Save szmeku/452b47e0cd688a1097a116d783e8e43e to your computer and use it in GitHub Desktop.
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
const calculateWinner = (squares) => {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const allXsOrOs = _.anyPass([
_.all(_.equals('X')),
_.all(_.equals('O'))
]);
const pickLine = _.props(_.__, squares);
return _.pipe(
_.find(_.pipe(pickLine, allXsOrOs)),
_.cond([
[_.isNil, _.identity],
[_.T, _.pipe(_.head, _.nth(_.__, squares))]
])
)(lines);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment