| 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 |
1
3
1
1
1
4662
4662
4662
4662
4662
4662
4662
4662
4662
4662
4662
4662
4662
4662
2435
1
1
2435
19480
155840
12863
75027
288612
288612
288612
288612
2057261
2057261
2057261
2057261
75026
1
605446
605446
605446
605446
131959
131959
131959
131959
2435
2435
19480
155840
155840
60957
1
4662
4662
4662
4662
37296
37296
37296
37296
298368
37296
4662
| "use strict";
Object.defineProperties(exports, {
Board: {get: function() {
return Board;
}},
__esModule: {value: true}
});
var $__0 = require('./brands'),
KING = $__0.KING,
QUEEN = $__0.QUEEN,
KNIGHT = $__0.KNIGHT,
BISHOP = $__0.BISHOP,
ROOK = $__0.ROOK,
PAWN = $__0.PAWN;
var Point = require('./point').Point;
var Board = function Board() {
var files = arguments[0] !== (void 0) ? arguments[0] : 8;
var ranks = arguments[1] !== (void 0) ? arguments[1] : 8;
var boardArr = arguments[2];
this.files = files;
this.ranks = ranks;
this.storage = createStorage(files, ranks);
this.pieces = new Set();
$traceurRuntime.setProperty(this.pieces, KING, new Set());
$traceurRuntime.setProperty(this.pieces, QUEEN, new Set());
$traceurRuntime.setProperty(this.pieces, KNIGHT, new Set());
$traceurRuntime.setProperty(this.pieces, BISHOP, new Set());
$traceurRuntime.setProperty(this.pieces, ROOK, new Set());
$traceurRuntime.setProperty(this.pieces, PAWN, new Set());
if (boardArr != null) {
this.decorate(boardArr);
}
};
var $Board = Board;
($traceurRuntime.createClass)(Board, {
map: function(fn) {
return new $Board(this.ranks, this.files, this.storage.map((function(rank, i) {
return rank.map((function(piece, j) {
return fn(piece, new Point(j, i));
}));
})));
},
getPieces: function(brand) {
return brand == null ? this.pieces : this.pieces[$traceurRuntime.toProperty(brand)];
},
getPieceCoords: function(piece) {
for (var i = 0,
iLen = this.storage.length; i < iLen; i++) {
try {
throw undefined;
} catch (rank) {
{
rank = this.storage[$traceurRuntime.toProperty(i)];
for (var j = 0,
jLen = rank.length; j < jLen; j++) {
try {
throw undefined;
} catch (p) {
{
p = rank[$traceurRuntime.toProperty(j)];
if (p && p === piece) {
return new Point(j, i);
}
}
}
}
}
}
}
return null;
},
getPieceByCoords: function($__4) {
var $__5 = $traceurRuntime.assertObject($__4),
x = $__5.x,
y = $__5.y;
var rotated = arguments[1] !== (void 0) ? arguments[1] : false;
var rank = this.storage[$traceurRuntime.toProperty(rotated ? this.ranks - y - 1 : y)];
return rank == null ? null : rank[$traceurRuntime.toProperty(rotated ? this.files - x - 1 : x)];
},
placePiece: function(piece, $__4) {
var $__5 = $traceurRuntime.assertObject($__4),
file = $__5.x,
rank = $__5.y;
$traceurRuntime.setProperty(this.storage[$traceurRuntime.toProperty(rank)], file, piece);
this.pieces.add(piece);
this.pieces[$traceurRuntime.toProperty(piece.brand)].add(piece);
},
decorate: function(board) {
var $__2 = this;
board.forEach((function(rank, i) {
rank.forEach((function(file, j) {
var piece = board[$traceurRuntime.toProperty(i)][$traceurRuntime.toProperty(j)];
if (piece != null) {
$__2.placePiece(piece, new Point(j, i));
}
}));
}));
}
}, {});
function createStorage() {
var ranks = arguments[0] !== (void 0) ? arguments[0] : 8;
var files = arguments[1] !== (void 0) ? arguments[1] : 8;
var board = [];
for (var i = 0; i < ranks; i++) {
try {
throw undefined;
} catch (rank) {
{
rank = [];
for (var j = 0; j < files; j++) {
rank.push(null);
}
board.push(rank);
}
}
}
return board;
}
|