-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
185 lines (142 loc) · 4.41 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var STATE = require('./states.js');
var Board = function Board(board) {
var self = this;
self._initialState = board;
// board need to be 2d array, gaps should be defined as immovable spaces
self.boardWidth = board[0].length;
self.boardHeight = board.length;
// snapshot wont work until the above
self._currentState = self.snapshot(board);
self._snapshotState = self.snapshot();
console.log("width: " + self.boardWidth + " height: " + self.boardHeight);
};
Board.prototype.snapshot = function(state) {
var self = this;
if(!state)
state = self._currentState;
var copy = new Array(state.length);
for(var i = 0; i < state.length; i++) {
var row = new Array(state[0].length);
for(var j = 0; j < state[0].length; j++) {
row[j] = state[i][j];
}
copy[i] = row;
}
return copy;
}
Board.prototype.printBoard = function(pieces) {
var self = this;
var buf = "\t";
for(var i = 0; i < self.boardWidth; i++) {
buf += " " + i + ":\t";
}
console.log(buf);
for(var i = self._currentState.length; i > 0; i--) {
var row = self._currentState[i-1];
var buf = "\t" + (i-1) +":\t";
var y = i-1;
for(var j = 0; j < row.length; j++) {
// check pieces array, if exists flag visually
var sep = "|";
var x = j;
if(pieces && pieces.length > 0) {
for(var k = 0; k < pieces.length; k++) {
if(pieces[k].x == x && pieces[k].y == y) {
sep = "*";
}
}
}
buf += " " + sep + row[j] + sep + "\t";
}
console.log(buf);
}
};
Board.prototype.makeMove = function(player, piece, to) {
var self = this;
var currentPiece = self.getPiece(to);
var fromPiece = self.getPiece(piece);
self._currentState[to.y][to.x] = player.id;//self._currentState[from.y][from.x]; // 'move' piece
self._currentState[piece.y][piece.x] = (fromPiece == STATE.START) ? STATE.START : STATE.DEFAULT;
return currentPiece; // return the bumped piece
};
Board.prototype.setPiece = function(position, y, val) {
var self = this;
var x = position;
var y = y;
var val = val;
if( typeof(position) == "object") {
x = position.x;
val = y;
y = position.y;
}
var old = self._currentState[y][x];
self._currentState[y][x] = val;
self._snapshot = self.snapshot();
return old;
}
Board.prototype.getPiece = function (position, y) {
var self = this;
var x = position;
var y = y;
if( typeof(position) == "object") {
x = position.x;
y = position.y;
}
return self._currentState[y][x];
};
Board.prototype.availableMoves = function(player, piece, distance) {
var self = this;
var x = piece.x;
var y = piece.y;
var walkQueue = [];
var terminalMoves = [];
var bitboard = [];
for(var i = 0; i < self.boardHeight; i++) {
var row = new Array(self.boardWidth);
bitboard.push(row);
}
walkQueue.push({x: x, y: y, step: 0}); // start
// bfs search map
while(walkQueue.length > 0) {
var task = walkQueue.shift();
// bounds check
if(task.x < 0 || task.y < 0 || task.x >= self.boardWidth || task.y >= self.boardHeight) {
continue;
}
var length = Math.abs(task.x - piece.x) + Math.abs(task.y - piece.y);
if(length > distance)
continue;
if(bitboard[task.y][task.x] == 1) {
continue;
}
bitboard[task.y][task.x] = 1;
// verify that the piece can move 'through' this spot, if step == distance, this is a landing
var destination = self.getPiece(task.x,task.y);
if(destination == STATE.IMPASS || (destination == STATE.BLOCKER && task.step != distance)) {
continue;
}
if(task.step == distance) { // out of moves, and we can land here
if(destination != STATE.IMPASS && destination != STATE.START && destination != player.id)
terminalMoves.push(task);
}
walkQueue.push({ x: task.x, y: task.y-1, step: task.step+1});
walkQueue.push({ x: task.x, y: task.y+1, step: task.step+1});
walkQueue.push({ x: task.x-1, y: task.y, step: task.step+1});
walkQueue.push({ x: task.x+1, y: task.y, step: task.step+1});
}
return terminalMoves;
};
Board.prototype.getStartPositions = function() {
var self = this;
var starts = [];
// just walk the board and return all start position markers
for(var i = 0; i < self.boardHeight; i++) {
for(var j = 0; j < self.boardWidth; j++) {
if(self.getPiece(j,i) == STATE.START) {
starts.push({x:j,y:i});
}
}
}
return starts;
};
module.exports = Board;