Skip to content

Instantly share code, notes, and snippets.

@R167
Created February 27, 2017 22:08
Show Gist options
  • Save R167/27bc95c3e2bb1095feee20542e635a8e to your computer and use it in GitHub Desktop.
Save R167/27bc95c3e2bb1095feee20542e635a8e to your computer and use it in GitHub Desktop.
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