Created
March 28, 2019 21:05
-
-
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
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
| 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