-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
312 lines (287 loc) · 9.97 KB
/
Copy pathengine.js
File metadata and controls
312 lines (287 loc) · 9.97 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const indexForCoords = (x, y) => {
return y * 8 + x;
}
const relativeIndex = (index, dx, dy) => {
const coords = coordsForIndex(index);
const x = coords.x + dx;
const y = coords.y + dy;
if ((x < 0 || x > 7) || (y < 0 || y > 7)) {
return null;
}
return indexForCoords(x, y);
}
const movesForPiece = (state, piece) => {
const { pieces } = state;
const pArray = [...new Array(64)].map(() => null);
for (const p of pieces) {
if (!p.dead) {
pArray[p.index] = p;
}
}
const color = piece.type[0];
const type = piece.type[1];
const moves = [];
const r = (x, y) => relativeIndex(piece.index, x, y);
const add = (i, moves, mode) => {
if (pArray[i]) {
if (pArray[i].type[0] !== color && (mode === 'both' || mode === 'take')) {
moves.push(i);
}
return false;
} else if (mode === 'both' || mode === 'move') {
moves.push(i);
return true;
}
}
const directional = (dx = 1, dy = 1, limit = 7, mode = 'both') => {
const moves = [];
for (let i = 1; i <= limit; i++) {
const ix = r(i * dx, i * dy);
if (ix === null || !add(ix, moves, mode)) {
break;
}
}
return moves;
}
if (type === 'p') { // pawn
const limit = piece.moveCount === 0 ? 2 : 1;
const dir = color === 'w' ? -1 : 1;
moves.push(...directional(0, dir, limit, 'move'));
moves.push(...directional(1, dir, 1, 'take'));
moves.push(...directional(-1, dir, 1, 'take'));
// en passant
const {y} = coordsForIndex(piece.index);
const dy = color === 'w' ? -1 : 1;
const row = color === 'w' ? 3 : 4;
if (y === row) {
const p1 = pArray[r(1, 0)];
const p2 = pArray[r(-1, 0)];
const valid = (p) => p && p.type[1] === 'p' && p.type[0] !== color && p.moveCount === 1 && state.lastPid === getPid(state, p);
if (valid(p1)) {
moves.push(...directional(1, dy, 1, 'move'));
}
if (valid(p2)) {
moves.push(...directional(-1, dy, 1, 'move'));
}
}
} else if (type === 'r') { // rook
moves.push(...directional(1, 0));
moves.push(...directional(-1, 0));
moves.push(...directional(0, 1));
moves.push(...directional(0, -1));
} else if (type === 'b') { // bishop
moves.push(...directional(-1, -1));
moves.push(...directional(1, 1));
moves.push(...directional(1, -1));
moves.push(...directional(-1, 1));
} else if (type === 'q') { // queen
moves.push(...directional(1, 0));
moves.push(...directional(-1, 0));
moves.push(...directional(0, 1));
moves.push(...directional(0, -1));
moves.push(...directional(-1, -1));
moves.push(...directional(1, 1));
moves.push(...directional(1, -1));
moves.push(...directional(-1, 1));
} else if (type === 'n') { // knight
moves.push(...directional(-1, -2, 1));
moves.push(...directional(1, -2, 1));
moves.push(...directional(2, -1, 1));
moves.push(...directional(2, 1, 1));
moves.push(...directional(-1, 2, 1));
moves.push(...directional(1, 2, 1));
moves.push(...directional(-2, -1, 1));
moves.push(...directional(-2, 1, 1));
} else if (type === 'k') { // king
moves.push(...directional(1, 0, 1));
moves.push(...directional(-1, 0, 1));
moves.push(...directional(0, 1, 1));
moves.push(...directional(0, -1, 1));
moves.push(...directional(-1, -1, 1));
moves.push(...directional(1, 1, 1));
moves.push(...directional(1, -1, 1));
moves.push(...directional(-1, 1, 1));
if (piece.moveCount === 0) { // castling
const clearRight = !pArray[r(1, 0)] && !pArray[r(2, 0)];
if (clearRight && pArray[r(3, 0)] && pArray[r(3, 0)].moveCount === 0) {
moves.push(...directional(2, 0, 2));
}
const clearLeft = !pArray[r(-1, 0)] && !pArray[r(-2, 0)] && !pArray[r(-3, 0)];
if (clearLeft && pArray[r(-4, 0)] && pArray[r(-4, 0)].moveCount === 0) {
moves.push(...directional(-2, 0, 2));
}
}
}
return moves;
};
const getPid = (state, piece) => state.pieces.indexOf(piece);
export const validMovesForColor = (state, color) => {
const moves = [];
const pieces = alivePieces(state).filter((p) => p.type[0] === color);
for (const piece of pieces) {
const pid = getPid(state, piece);
moves.push({pid, piece, moves: validMoves(state, pid)});
}
return moves;
}
const initialState = () => ({
lastPid: null,
pieces: [
{type: 'br', index: 0, dead: false, moveCount: 0},
{type: 'bn', index: 1, dead: false, moveCount: 0},
{type: 'bb', index: 2, dead: false, moveCount: 0},
{type: 'bq', index: 3, dead: false, moveCount: 0},
{type: 'bk', index: 4, dead: false, moveCount: 0},
{type: 'bb', index: 5, dead: false, moveCount: 0},
{type: 'bn', index: 6, dead: false, moveCount: 0},
{type: 'br', index: 7, dead: false, moveCount: 0},
{type: 'bp', index: 8, dead: false, moveCount: 0},
{type: 'bp', index: 9, dead: false, moveCount: 0},
{type: 'bp', index: 10, dead: false, moveCount: 0},
{type: 'bp', index: 11, dead: false, moveCount: 0},
{type: 'bp', index: 12, dead: false, moveCount: 0},
{type: 'bp', index: 13, dead: false, moveCount: 0},
{type: 'bp', index: 14, dead: false, moveCount: 0},
{type: 'bp', index: 15, dead: false, moveCount: 0},
{type: 'wr', index: 56, dead: false, moveCount: 0},
{type: 'wn', index: 57, dead: false, moveCount: 0},
{type: 'wb', index: 58, dead: false, moveCount: 0},
{type: 'wq', index: 59, dead: false, moveCount: 0},
{type: 'wk', index: 60, dead: false, moveCount: 0},
{type: 'wb', index: 61, dead: false, moveCount: 0},
{type: 'wn', index: 62, dead: false, moveCount: 0},
{type: 'wr', index: 63, dead: false, moveCount: 0},
{type: 'wp', index: 48, dead: false, moveCount: 0},
{type: 'wp', index: 49, dead: false, moveCount: 0},
{type: 'wp', index: 50, dead: false, moveCount: 0},
{type: 'wp', index: 51, dead: false, moveCount: 0},
{type: 'wp', index: 52, dead: false, moveCount: 0},
{type: 'wp', index: 53, dead: false, moveCount: 0},
{type: 'wp', index: 54, dead: false, moveCount: 0},
{type: 'wp', index: 55, dead: false, moveCount: 0}
]
});
const cloneState = (state) => {
const pieces = state.pieces.map((p) => ({...p}));
return { lastPid: state.lastPid, pieces };
}
// ----
export const alivePieces = (state) => state.pieces.filter((p) => !p.dead);
export const coordsForIndex = (index) => {
const y = Math.floor(index / 8);
const x = Math.floor(index % 8);
if ((x < 0 || x > 7) || (y < 0 || y > 7)) {
return null;
}
return {x, y}
}
export const validMoves = (state, pid) => {
const piece = state.pieces[pid];
const moves = movesForPiece(state, piece);
const invalid = [];
for (const move of moves) {
const tmpState = makeMove(state, pid, move);
const king = alivePieces(tmpState).find((pp) => pp.type === `${piece.type[0]}k`);
const opponentPieces = alivePieces(tmpState).filter((p) => p.type[0] !== piece.type[0]);
const opponentMoves = opponentPieces.map((op) => movesForPiece(tmpState, op)).flat();
if (opponentMoves.includes(king.index)) {
invalid.push(move);
}
}
return moves.filter((m) => !invalid.includes(m));
};
export const makeMove = (state, pid, toIndex, callback) => {
const newState = cloneState(state);
const pieces = alivePieces(newState);
const piece = newState.pieces[pid];
const target = pieces.find((p) => p.index === toIndex);
if (target) { // kill
target.dead = true;
} else if (piece.type[1] === 'p') {
const coords = coordsForIndex(piece.index);
const newCoords = coordsForIndex(toIndex);
if (newCoords.x !== coords.x) { // en passant
const offset = newCoords.y < coords.y ? 8 : -8;
const t = pieces.find((p) => p.index === toIndex + offset);
if (t && t.type[0] !== piece.type[0] && t.type[1] === 'p') {
t.dead = true;
}
}
}
if (piece.type[1] === 'k') { // castling
const delta = toIndex - piece.index;
if (delta === 2) {
const rook = pieces.find((p) => p.index === piece.index + 3);
rook.index = piece.index + 1;
rook.moveCount += 1;
} else if (delta === -2) {
const rook = pieces.find((p) => p.index === piece.index - 4);
rook.index = piece.index - 1;
rook.moveCount += 1;
}
}
piece.index = toIndex;
newState.lastPid = pid;
piece.moveCount++;
const willPromote = () => {
if (piece.type[1] !== 'p') {
return false;
}
const {y} = coordsForIndex(piece.index);
if ((piece.type[0] === 'w' && y === 0) || (piece.type[0] === 'b' && y === 7)) {
return true;
}
return false;
}
if (willPromote()) {
if (callback) {
return new Promise(async (resolve) => {
const newType = await callback(newState);
piece.type = `${piece.type[0]}${newType}`;
resolve(newState);
});
} else {
piece.type = `${piece.type[0]}q`;
}
}
return newState;
};
export const gameStatus = (state) => {
const ap = alivePieces(state);
const statusForColor = (color) => {
const oposite = color === 'w' ? 'b' : 'w';
const moves = validMovesForColor(state, color);
const threats = validMovesForColor(state, oposite);
const movesList = moves.map((m) => m.moves).flat();
const threatsList = threats.map((m) => m.moves).flat();
const king = ap.find((p) => p.type === `${color}k`);
const check = !!(king && threatsList.includes(king.index));
const checkmate = check && movesList.length === 0;
const stalemate = !check && movesList.length === 0;
return { checkmate, check, stalemate }
}
if (ap.length === 2 && ap.every((p) => p.type[1] === 'k')) {
return 'd';
}
const white = statusForColor('w');
if (white.checkmate) {
return 'wcm';
} else if (white.check) {
return 'wc';
} else if (white.stalemate) {
return 'sm';
}
const black = statusForColor('b');
if (black.checkmate) {
return 'bcm';
} else if (black.check) {
return 'bc';
} else if (black.stalemate) {
return 'sm';
}
return null;
}
export const init = () => {
const state = initialState();
return state;
};