-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.js
77 lines (70 loc) · 2.59 KB
/
Board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Board {
constructor(game, state) {
if (!state) {
this.game = game;
this.state = initialState;
initializeState(this);
}
else if (state.length == 8 && state.every(col => { return col.length === Board.prototype.SIZE })) {
this.state = state;
this.game.whitesAlive = whitesAlive;
this.game.blacksAlive = blacksAlive;
}
else throw "state array length (and its inside objects) needs to equal board's size";
}
get boardState() { return this.boardState; }
set boardState(newState) {
if (newState.length == 8 && newState.every(col => {
return col.length === Board.prototype.SIZE
}))
this.state = newState;
else throw 'board length is incorrect';
}
isOccupied(location) {
return this.tileStatus(location) != 'e';
}
tileStatus(location) {
if (location.row >= 0 && location.row < this.SIZE && location.col >= 0 && location.col < this.SIZE)
return this.state[location.row][location.col];
else return false;
}
successfulMovment(moveFrom, moveTo) {
this.tileStatus(moveTo) = this.tileStatus(moveFrom);
this.tileStatus(moveFrom) = 'e';
}
pieceCaptured(location) {
let pieceToRemove = this.tileStatus(location);
let aliveList = pieceToRemove.color ? this.game.whitesAlive : this.game.blacksAlive;
let index = aliveList.indexOf(pieceToRemove);
aliveList.splice(index, 1);
this.state[location.row][location.col] = 'e';
}
}
Object.defineProperty(Board.prototype, 'SIZE', {
value: 8,
writable: false,
configurable: false,
enumerable: true
});
const initialState = new Array();
//top left is white empty tile, blacks on top
function initializeState(board) {
for (let row = 0; row < Board.prototype.SIZE; row++) {
initialState.push(new Array());
var tile;
for (let col = 0; col < Board.prototype.SIZE; col++) {
var tileIndex = row + col;
if (row < 3 && (tileIndex % 2 != 0)) {
tile = new Piece(board, new Location(row, col), false);
board.game.blacksAlive.push(tile);
}
else if (row > 4 && (tileIndex % 2 != 0)) {
tile = new Piece(board, new Location(row, col), true);
board.game.whitesAlive.push(tile);
}
else
tile = 'e';
initialState[row].push(tile);
}
}
}