-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.js
247 lines (218 loc) · 6.61 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* global board, Dom, Game, board */
/* eslint-disable no-unused-vars */
class Piece {
constructor(squareName, colour, symbol, name) {
this.square = board.sq(squareName);
this.square.piece = this;
this.row = this.square.row;
this.column = this.square.column;
this.name = name;
this.colour = colour;
this.opponentColour = colour === 'white' ? 'black' : 'white';
this.symbol = symbol;
this.square.render();
this.hasMoved = false;
}
availableSquares() {
return board.filter(square => this.squareIsAvailable(square));
}
attemptMoveTo(square) {
if (Game.isInCheck(this.colour) && !Game.pieceMovesPreventingCheck(this).includes(square)) {
Dom.message('This move does not prevent check');
return false;
}
const moveResultsInCheck = Game
.simulateMove(this, square, () => Game.isInCheck(this.colour));
if (this.squareIsAvailable(square) && moveResultsInCheck) {
Dom.message('You cannot move into check');
return false;
}
if (this.squareIsAvailable(square)) {
Dom.message('Move ' + this.symbol + ' to ' + square.name);
if (square.hasOpponentOf(this)) {
this.takes(square.piece);
}
this.moveTo(square);
this.hasMoved = true;
return true;
}
return false;
}
moveTo(square) {
this.square.clear();
square.setPiece(this);
}
canMove() {
return !!this.availableSquares().length;
}
takes(piece) {
Dom.message(`${this.colour}
<span class="symbol">${this.symbol}</span>
takes ${piece.colour}
<span class="symbol">${piece.symbol}</span>!`);
}
isThreatenedBy() {
return board
.opponentsOf(this)
.filter(piece => piece.availableSquares().includes(this.square));
}
isThreatened() {
return this.isThreatenedBy().length;
}
isThreateningSquares() {
return this.availableSquares();
}
}
class King extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♚', 'K');
}
squareIsAvailable(square) {
const castlingDestinations = board
.piecesByColour(this.colour)
.filter(piece => piece instanceof Rook && this.canCastle(piece))
.map(piece => this.castlingDestinations(piece).king);
const neighbouringSquares = board
.filter(sq => sq.isNeighbourOf(square));
return castlingDestinations.includes(square)
|| (square.isNeighbourOf(this.square)
&& !neighbouringSquares.find(sq => sq.hasOpponentKing(this))
&& !this.square.routeIsBlockedTo(square)
&& !square.isThreatenedBy(this.opponentColour).length);
}
hasOpponentKing(square) {
return square.piece && square.piece instanceof King
&& square.piece.colour === this.opponentColour;
}
rookToCastleWithToReach(square) {
return board
.piecesByColour(this.colour)
.filter(piece => piece instanceof Rook && this.canCastle(piece))
.find(piece => this.castlingDestinations(piece).king === square);
}
castle(rook) {
if (this.canCastle(rook)) {
const destinations = this.castlingDestinations(rook);
this.moveTo(destinations.king);
rook.moveTo(destinations.rook);
Dom.message(this.colour + ' castles');
return true;
}
}
castlingDestinations(rook) {
if (rook.column === 8) {
// King's side
return {
king: board.squareAt(this.row, 7),
rook: board.squareAt(this.row, 6)
};
} else if (rook.column === 1) {
// Queen's side
return {
king: board.squareAt(this.row, 3),
rook: board.squareAt(this.row, 4)
};
}
}
attemptMoveTo(square) {
const rookToCastleWith = this.rookToCastleWithToReach(square);
if (rookToCastleWith) {
// Square is available via castling
return this.castle(rookToCastleWith);
} else {
// Square is available normally
return super.attemptMoveTo(square);
}
}
canCastle(rook) {
// Is the rook of the right colour?
if (this.colour !== rook.colour) return false;
// Has either piece moved?
if (this.hasMoved || rook.hasMoved) return false;
// Are any of the intervening squares threatened?
const opponentColour = this.opponentColour;
if (this.square.routeTo(rook.square).some(square =>
square.isThreatenedBy(opponentColour).length)) return false;
// Are any of the intervening squares occupied?
if (this.square.squaresTo(rook.square).filter(square =>
square.piece !== rook).some(square => square.piece)) return false;
return true;
}
}
class Queen extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♛', 'Q');
}
squareIsAvailable(square) {
return (
square.isDiagonalFrom(this.square) || square.isStraightLineFrom(this.square)
) && !this.square.routeIsBlockedTo(square);
}
}
class Bishop extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♝', 'B');
}
squareIsAvailable(square) {
return (
square.isDiagonalFrom(this.square)
) && !this.square.routeIsBlockedTo(square);
}
}
class Knight extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♞', 'N');
}
squareIsAvailable(square) {
return square.isKnightsMoveFrom(this.square) && !square.hasTeamMateOf(this);
}
}
class Rook extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♜', 'R');
}
squareIsAvailable(square) {
return (
square.isStraightLineFrom(this.square)
) && !this.square.routeIsBlockedTo(square);
}
}
class Pawn extends Piece {
constructor(squareName, colour) {
super(squareName, colour, '♟', 'p');
}
squareIsAvailable(square) {
const here = this.square;
let isAvailable = this.squareIsForwardOf(square);
if (this.hasMoved) {
isAvailable = isAvailable
&& square.isNeighbourOf(here);
} else {
isAvailable = isAvailable
&& here.routeTo(square).length <= 2;
}
isAvailable = isAvailable
&& (
(square.isStraightLineFrom(here) && !square.piece)
|| (this.isThreatening(square) && square.hasOpponentOf(this))
);
return isAvailable && !here.routeIsBlockedTo(square);
}
squareIsForwardOf(square) {
switch(this.colour) {
case 'black':
return square.isAbove(this.square);
case 'white':
return square.isBelow(this.square);
}
}
isThreatening(square) {
return this.squareIsForwardOf(square)
&& square.isDiagonalFrom(this.square)
&& square.isNeighbourOf(this.square);
}
isThreateningSquares() {
// TODO: Finish this. Which squares is this pawn threatening?
return board.filter(square => this.isThreatening(square));
}
}