Last active
October 1, 2019 10:54
-
-
Save szmeku/452b47e0cd688a1097a116d783e8e43e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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