Skip to content

Instantly share code, notes, and snippets.

@sunmockyang
Created March 28, 2019 21:05
Show Gist options
  • Select an option

  • Save sunmockyang/897b396728bc780b2702787825d71450 to your computer and use it in GitHub Desktop.

Select an option

Save sunmockyang/897b396728bc780b2702787825d71450 to your computer and use it in GitHub Desktop.
A function that will check if a 2d array contains a tic tac toe win
var map = [
[1, 1, 1],
[-1, -1, -1],
[0, 0, 0]
];
function checkStatus(team){
// Check vertical wins
for (var i = 0; i < 3; i++) {
if (map[i][0] == team && map[i][1] == team && map[i][2] == team) {
return true;
}
}
// Check horizontal wins
for (var i = 0; i < 3; i++) {
if (map[0][i] == team && map[1][i] == team && map[2][i] == team) {
return true;
}
}
// Check diagonal
if (map[0][0] == team && map[1][1] == team && map[2][2] == team) {
return true;
}
if (map[2][0] == team && map[1][1] == team && map[0][2] == team) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment