Last active
August 17, 2018 12:17
-
-
Save dustinheestand/68a0d28533d76d0f644a1b6baf7084af 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
class Cell { | |
constructor(pos) { | |
this.pos = pos; | |
this.neighbors = []; | |
this.explored = false; | |
this.color = ''; | |
} | |
set(color) { | |
if (this.color !== '') throw new Error(`There's a piece there!`); | |
this.color = color; | |
const oppColor = color === 'black' ? 'white' : 'black'; | |
Cell.queue.push(this); | |
this.libertyCheck(color); | |
Cell.tearDown(); | |
this.neighbors | |
.filter(c => c.color === oppColor && !c.explored) | |
.forEach(c => { | |
Cell.queue.push(c); | |
c.libertyCheck(color); | |
}); | |
Cell.tearDown(); | |
} | |
libertyCheck(whoseTurn) { | |
if (this.neighbors.findIndex(c => !c.color) < 0) { | |
const neighborsToSearch = this.neighbors.filter( | |
c => !c.explored && c.color === this.color | |
); | |
neighborsToSearch.forEach(c => { | |
c.explored = true; | |
}); | |
if (neighborsToSearch[0]) Cell.queue.push(...neighborsToSearch); | |
Cell.qIndex++; | |
if (Cell.qIndex >= Cell.queue.length) { | |
this.remove(whoseTurn); | |
} else { | |
Cell.queue[Cell.qIndex].libertyCheck(whoseTurn); | |
} | |
} | |
} | |
remove(whoseTurn) { | |
if (this.color === whoseTurn) { | |
Cell.queue[0].color = ''; | |
Cell.tearDown(); | |
throw new Error("That's suicide!"); | |
} else { | |
Cell.queue.forEach(c => { | |
c.color = ''; | |
}); | |
} | |
} | |
static tearDown() { | |
Cell.queue.forEach(c => { | |
c.explored = false; | |
}); | |
Cell.queue = []; | |
Cell.qIndex = 0; | |
} | |
} | |
Cell.queue = []; | |
Cell.qIndex = 0; | |
class Go { | |
constructor(w, h) { | |
this.w = w; | |
this.h = h || w; | |
if (w > 25 || h > 25) throw new Error('Too big a board!'); | |
this.blacksTurn = true; | |
this.board = this.makeBoard(); | |
} | |
makeBoard() { | |
const board = []; | |
for (let i = 0; i < this.h; i++) { | |
const row = []; | |
board.push(row); | |
for (let j = 0; j < this.w; j++) { | |
row.push(new Cell(this.reverseParse(i, j))); | |
} | |
} | |
for (let i = 0; i < this.h; i++) { | |
for (let j = 0; j < this.w; j++) { | |
board[i][j].neighbors = [ | |
(board[i - 1] || [])[j], | |
(board[i + 1] || [])[j], | |
board[i][j + 1], | |
board[i][j - 1] | |
].filter(a => a); | |
} | |
} | |
return board; | |
} | |
renderCell(color) { | |
switch (color) { | |
case 'white': | |
return 'o'; | |
case 'black': | |
return 'x'; | |
default: | |
return '.'; | |
} | |
} | |
parse(pos) { | |
const row = parseInt(pos.match(/\d+/)[0], 10); | |
const col = pos[pos.length - 1]; | |
const y = this.h - row; | |
let x = col.charCodeAt(0) - 65; | |
if (x > 8) x--; | |
return [y, x]; | |
} | |
reverseParse(y, x) { | |
const number = (this.h - y).toString(); | |
const letter = String.fromCharCode(x > 8 ? x + 66 : x + 65); | |
return number + letter; | |
} | |
getPosition(str) { | |
let y, x; | |
[y, x] = this.parse(str); | |
return this.renderCell(this.boardData[y][x].color); | |
} | |
currentColor() { | |
return this.blacksTurn ? 'black' : 'white'; | |
} | |
oppColor() { | |
return this.blacksTurn ? 'white' : 'black'; | |
} | |
move(pos) { | |
//console.log(pos, this.blacksTurn ? 'black' : 'white'); | |
let y, x; | |
[y, x] = this.parse(pos); | |
const color = this.blacksTurn ? 'black' : 'white'; | |
try { | |
this.board[y][x].set(color); | |
this.blacksTurn = !this.blacksTurn; | |
} catch (err) { | |
throw err; | |
} | |
} | |
render() { | |
const boardArray = []; | |
this.board.forEach(row => { | |
const drawRow = []; | |
boardArray.push(drawRow); | |
row.forEach(cell => { | |
if (cell.color === 'white') drawRow.push('o'); | |
else if (cell.color === 'black') drawRow.push('x'); | |
else drawRow.push('.'); | |
}); | |
}); | |
console.log(boardArray); | |
} | |
} | |
const go = new Go(5); | |
go.move('3C'); | |
go.move('3B'); | |
go.move('1A'); | |
go.move('3D'); | |
go.move('2A'); | |
go.move('4C'); | |
go.move('3A'); | |
go.move('2C'); | |
go.move('5E'); | |
go.move('4A'); | |
go.move('4E'); | |
go.move('2B'); | |
go.move('3E'); | |
go.move('1B'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment