-
Notifications
You must be signed in to change notification settings - Fork 2
/
board.js
48 lines (42 loc) · 1.16 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
var fs = require('fs');
module.exports = {
pieces: {}
, create: function () {
this.buildBackRow("1", "W");
this.buildPawnRow("2", "W");
for (var col = 3; col <= 6; col ++) {
this.buildBlankRow(col)
}
this.buildPawnRow("7", "B");
this.buildBackRow("8", "B");
this.save();
return this.pieces;
}
, buildBackRow: function (row, color) {
for (var col = 0; col < 8; col ++) {
this.pieces[this.col_labels[col] + row] = color + this.row_pieces[col];
}
}
, buildPawnRow: function (row, color) {
for (var col = 0; col < 8; col ++) {
this.pieces[this.col_labels[col] + row] = color + "P";
}
}
, buildBlankRow: function (row) {
for (var col = 0; col < 8; col ++) {
this.pieces[this.col_labels[col] + row] = "";
}
}
, col_labels: ["a", "b", "c", "d", "e", "f", "g", "h"]
, row_pieces: ["R", "N", "B", "Q", "K", "B", "N", "R"]
, save: function () {
fs.writeFileSync("board.hash", JSON.stringify(this.pieces));
}
, move: function (from, to) {
piece = this.pieces[from]
this.pieces[from] = "";
removed = this.pieces[to]
this.pieces[to] = piece;
return removed;
}
};