-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.js
87 lines (76 loc) · 2.4 KB
/
piece.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
class Piece {
constructor(color, unit) {
this.unit = unit.toUpperCase();
this.color = color;
this.frozen = false;
this.promoted = false;
this.original = false;
}
}
function isEmpty(piece) {
return piece.color == "";
}
function clearPiece(piece) {
piece.color = "";
piece.unit = "";
piece.frozen = false;
piece.promoted = false;
piece.original = false;
}
function copyPiece(dst, src) {
dst.unit = src.unit;
dst.color = src.color;
dst.frozen = src.frozen;
dst.promoted = src.promoted;
dst.original = src.original;
}
function getDetailedUnitType(file, rank, unit) {
// returns unit unless it's a bishop, when we append L or D for light or dark bishop.
if (unit == 'B') {
if ((file + rank) % 2 == 1) return "BL"; else return "BD";
} else return unit;
}
function placePieceAt(file, rank, piece) {
if (board[file][rank].unit != "") {
const detailedUnitType = getDetailedUnitType(file, rank, board[file][rank].unit);
positionData.detailedUnitCount[board[file][rank].color + detailedUnitType]--;
}
// during cage verification, it is possible for one or both kings to be missing.
if (positionData.detailedUnitCount[board[file][rank].color + 'K'] == 0) {
positionData.kingPosition[board[file][rank].color] = null;
}
if (piece.unit != "") {
const detailedUnitType = getDetailedUnitType(file, rank, piece.unit);
positionData.detailedUnitCount[piece.color + detailedUnitType]++;
}
if (piece.unit == 'K') {
positionData.kingPosition[piece.color] = new Square(file, rank);
}
copyPiece(board[file][rank], piece);
}
function emptyPieceAt(file, rank) {
placePieceAt(file, rank, new Piece("", ""));
}
function setDefaultPieceParameters(file, rank, piece) {
// set default original and frozen values sensibly
if (piece.color == "" || piece.unit == "") {
piece.unit = "";
piece.color = "";
}
if (piece.unit == 'P') {
assert(rank > 0 && rank < 7, 'Tried to add a pawn on rank ' + rank);
piece.original = true;
if (piece.color == "w") {
piece.frozen = (rank == 1);
}
if (piece.color == "b") {
piece.frozen = (rank == 6);
}
}
if (piece.unit == 'K') {
piece.original = true;
}
if (piece.frozen) {
piece.original = true;
}
}