-
-
Save isaacs/182743 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
// more lines, ever-so-slightly faster search, though. | |
function checkValidMove (xo, pos, state) { | |
// invalid move. | |
if (state[pos[0]][pos[1]] !== '') return false; | |
state[pos[0]][pos[1]] = xo; | |
// check row | |
for (var i = 0, col = 0; i < 3; i ++) if (state[pos[0]][i] === xo) col ++; | |
if (col === 3) return "a winner is you"; | |
// check col | |
for (var i = 0, row = 0; i < 3; i ++) if (state[i][pos[1]] === xo) row ++; | |
if (row === 3) return "a winner is you"; | |
// check \ | |
if (pos[0] === pos[1]) for (var i = 0, diag = 0; i < 3; i ++) if (state[i][i] === xo) diag ++; | |
if (diag === 3) return "a winner is you"; | |
// check / | |
if (pos[0] === 2-pos[1]) for (var i = 0, diag = 0; i < 3; i ++) if (state[i][2-i] === xo) diag ++; | |
if (diag === 3) return "a winner is you"; | |
return "no winner for you!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment