Skip to content

Instantly share code, notes, and snippets.

@petternordholm
Forked from d-git/board.js
Last active August 29, 2015 14:22
Show Gist options
  • Save petternordholm/21fa7e9c28c0e3279df3 to your computer and use it in GitHub Desktop.
Save petternordholm/21fa7e9c28c0e3279df3 to your computer and use it in GitHub Desktop.
function Board() {
this.isWinner = isWinner;
this.placePiece = placePiece;
var GOAL = 5, winnerIs, values = {'white': {}, 'black': {}};
function placePiece(col, row, player) {
values[player][col + '-' + row] = 1;
}
function isWinner(x, y, player) {
var dir = [[1,0],[0,1],[1,1],[1,-1]];
for(var d in dir) {
if(getCount(player, x, y, dir[d][0], dir[d][1]) + getCount(player, x, y, -dir[d][0], -dir[d][1], false) + 1 >= GOAL) return true;
}
return false;
}
function getCount(player, x, y, dx, dy) {
return values[player][(x + dx) + '-' + (y + dy)] ? 1 + getCount(player, x+dx, y+dy, dx, dy) : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment