Last active
July 7, 2020 16:38
-
-
Save bcamargogui/5d87489e51756964a9925484f99999d4 to your computer and use it in GitHub Desktop.
This file contains 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
enum SquarePossibilities { | |
X = 'X', | |
O = 'O', | |
} | |
type SquareType = SquarePossibilities | null; | |
/** | |
* Return winner from tic tac toe game | |
* @argument squares: SquareType[] | |
* @returns SquareType | |
*/ | |
function getWinnerFromTicTacToe(squares: SquareType[]): SquareType { | |
const winCombinations = [ | |
[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 (const combination of winCombinations) { | |
const [a, b, c] = combination; | |
if ( | |
squares[a] | |
&& squares[a] === squares[b] | |
&& squares[a] === squares[c] | |
) { | |
return squares[a]; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment