-
-
Save petternordholm/21fa7e9c28c0e3279df3 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 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