Created
February 27, 2017 22:08
-
-
Save R167/27bc95c3e2bb1095feee20542e635a8e 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
function diagnolWinner() { | |
var color; | |
for (var i = 0; i <= board.length - 4; i++) { | |
if ((color = genericDiagnol(i, 1)) !== 0) { | |
return color; | |
} | |
} | |
for (var i = board.length - 1; i >= board.length - 4; i--) { | |
if ((color = genericDiagnol(i, -1)) !== 0) { | |
return color; | |
} | |
} | |
return 0; | |
} | |
/** | |
* Keeps code DRY | |
* | |
* @param i column to look in | |
* @param direction offset type thing | |
* @return winners within the | |
*/ | |
function genericDiagnol(i, direction) { | |
for (var j = 0; j <= board[0].length - 4; j++) { | |
var current = board[i][j]; | |
if (current !== 0) { | |
var success = true; | |
for (int k = 1; k < 4; k++) { | |
if (board[i + k * direction][j + k] !== current) { | |
success = false; | |
break; | |
} | |
} | |
if (success) { | |
return current; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment