Skip to content

Instantly share code, notes, and snippets.

@bcamargogui
Last active July 7, 2020 16:38
Show Gist options
  • Save bcamargogui/5d87489e51756964a9925484f99999d4 to your computer and use it in GitHub Desktop.
Save bcamargogui/5d87489e51756964a9925484f99999d4 to your computer and use it in GitHub Desktop.
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