-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding board class, starting to draw board state on canvas
- Loading branch information
Showing
5 changed files
with
142 additions
and
32 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// The data structure representing the board | ||
|
||
const {range,ERR,dbg} = require("./util.js"); | ||
|
||
const INITIAL_STONE_COUNT = 4; //per player cell | ||
|
||
/* | ||
the whole board is simply an array on integers. | ||
Each cell in the array represents a cell on the board. The value of board[i] is the number of stones in cell i. | ||
i = 0 => player 1 home | ||
i = CELL_COUNT/2 => player 2 home | ||
i = 1,2,.., CELL_COUNT/2 - 1 => player 1 cells (the top row) | ||
i = CELL_COUNT / 2+1, ... CELL_COUNT-1 => player 2 cells (bottom row) | ||
*/ | ||
|
||
|
||
class Board | ||
{ | ||
constructor(_cellCount) | ||
{ | ||
this.cellCount = _cellCount; | ||
this.board = []; | ||
range(1,this.cellCount).forEach(_ => this.board.push(0)) | ||
this.forAllPlayer1Cells(cellInd => this.setCellStoneCount(cellInd,INITIAL_STONE_COUNT)) | ||
this.forAllPlayer2Cells(cellInd => this.setCellStoneCount(cellInd,INITIAL_STONE_COUNT)) | ||
dbg("Board initialized") | ||
} | ||
|
||
|
||
forAllPlayer2Cells(f) | ||
{ | ||
if (f) | ||
range(this.cellCount/2+1,this.cellCount-1).forEach(f) | ||
else ERR("Invalid function for player 2 cells") | ||
} | ||
|
||
forAllPlayer1Cells(f) | ||
{ | ||
if (f) | ||
range(1,this.cellCount/2-1).forEach(f); | ||
else | ||
ERR("Invalid function for player 1 cells") | ||
} | ||
|
||
setCellStoneCount(boardCell,cnt) { this.board[boardCell] = cnt } | ||
stonesIn(boardCell) { return this.board[boardCell]} | ||
|
||
forAllCells(f) | ||
{ | ||
if (f) range(0,this.cellCount-1).forEach(boardCell => f(boardCell)) | ||
else ERR("Invalid function for all cells") | ||
} | ||
|
||
isPlayer1Cell(boardCell) { return boardCell >= 1 && boardCell <= this.cellCount/2-1; } | ||
isPlayer2Cell(boardCell) { return boardCell >= this.cellCount/2+1 && boardCell <= this.cellCount-1; } | ||
|
||
isPlayer1Home(boardCell) { return boardCell == 0; } | ||
isPlayer2Home(boardCell) { return boardCell == this.cellCount/2; } | ||
|
||
totalCellCount() { return this.cellCount; } | ||
} | ||
|
||
|
||
module.exports = { | ||
Board | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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