Commit | Line | Data |
---|---|---|
92342261 BA |
1 | // (Orthodox) Chess rules are defined in ChessRules class. |
2 | // Variants generally inherit from it, and modify some parts. | |
3 | ||
e2732923 | 4 | import { ArrayFun } from "@/utils/array"; |
0c3fe8a6 | 5 | import { randInt, shuffle } from "@/utils/alea"; |
e2732923 | 6 | |
910d631b | 7 | // class "PiPo": Piece + Position |
6808d7a1 | 8 | export const PiPo = class PiPo { |
1c9f093d | 9 | // o: {piece[p], color[c], posX[x], posY[y]} |
6808d7a1 | 10 | constructor(o) { |
1c9f093d BA |
11 | this.p = o.p; |
12 | this.c = o.c; | |
13 | this.x = o.x; | |
14 | this.y = o.y; | |
15 | } | |
6808d7a1 | 16 | }; |
1d184b4c | 17 | |
6808d7a1 | 18 | export const Move = class Move { |
1c9f093d BA |
19 | // o: {appear, vanish, [start,] [end,]} |
20 | // appear,vanish = arrays of PiPo | |
21 | // start,end = coordinates to apply to trigger move visually (think castle) | |
6808d7a1 | 22 | constructor(o) { |
1c9f093d BA |
23 | this.appear = o.appear; |
24 | this.vanish = o.vanish; | |
2da551a3 BA |
25 | this.start = o.start || { x: o.vanish[0].x, y: o.vanish[0].y }; |
26 | this.end = o.end || { x: o.appear[0].x, y: o.appear[0].y }; | |
1c9f093d | 27 | } |
6808d7a1 | 28 | }; |
1d184b4c | 29 | |
2c5d7b20 BA |
30 | // NOTE: x coords = top to bottom; y = left to right |
31 | // (from white player perspective) | |
6808d7a1 | 32 | export const ChessRules = class ChessRules { |
7e8a7ea1 | 33 | |
1c9f093d BA |
34 | ////////////// |
35 | // MISC UTILS | |
36 | ||
eb2d61de BA |
37 | static get Options() { |
38 | return { | |
39 | select: [ | |
40 | { | |
41 | label: "Randomness", | |
42 | variable: "randomness", | |
43 | defaut: 2, | |
44 | options: [ | |
45 | { label: "Deterministic", value: 0 }, | |
46 | { label: "Symmetric random", value: 1 }, | |
47 | { label: "Asymmetric random", value: 2 } | |
48 | ] | |
49 | } | |
50 | ], | |
51 | check: [] | |
52 | }; | |
53 | } | |
54 | ||
55 | static AbbreviateOptions(opts) { | |
56 | return ""; | |
57 | // Randomness is a special option: (TODO?) | |
58 | //return "R" + opts.randomness; | |
59 | } | |
60 | ||
20620465 | 61 | // Some variants don't have flags: |
6808d7a1 BA |
62 | static get HasFlags() { |
63 | return true; | |
20620465 | 64 | } |
1c9f093d | 65 | |
3a2a7b5f BA |
66 | // Or castle |
67 | static get HasCastle() { | |
68 | return V.HasFlags; | |
69 | } | |
70 | ||
32f6285e BA |
71 | // Pawns specifications |
72 | static get PawnSpecs() { | |
73 | return { | |
74 | directions: { 'w': -1, 'b': 1 }, | |
472c0c4f | 75 | initShift: { w: 1, b: 1 }, |
32f6285e | 76 | twoSquares: true, |
472c0c4f | 77 | threeSquares: false, |
32f6285e BA |
78 | promotions: [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN], |
79 | canCapture: true, | |
80 | captureBackward: false, | |
81 | bidirectional: false | |
82 | }; | |
83 | } | |
84 | ||
85 | // En-passant captures need a stack of squares: | |
6808d7a1 BA |
86 | static get HasEnpassant() { |
87 | return true; | |
20620465 BA |
88 | } |
89 | ||
90 | // Some variants cannot have analyse mode | |
8477e53d | 91 | static get CanAnalyze() { |
20620465 BA |
92 | return true; |
93 | } | |
933fd1f9 BA |
94 | // Patch: issues with javascript OOP, objects can't access static fields. |
95 | get canAnalyze() { | |
96 | return V.CanAnalyze; | |
97 | } | |
20620465 BA |
98 | |
99 | // Some variants show incomplete information, | |
100 | // and thus show only a partial moves list or no list at all. | |
101 | static get ShowMoves() { | |
102 | return "all"; | |
103 | } | |
933fd1f9 BA |
104 | get showMoves() { |
105 | return V.ShowMoves; | |
106 | } | |
1c9f093d | 107 | |
00eef1ca BA |
108 | // Sometimes moves must remain hidden until game ends |
109 | static get SomeHiddenMoves() { | |
110 | return false; | |
111 | } | |
112 | get someHiddenMoves() { | |
113 | return V.SomeHiddenMoves; | |
114 | } | |
115 | ||
ad030c7d BA |
116 | // Generally true, unless the variant includes random effects |
117 | static get CorrConfirm() { | |
118 | return true; | |
119 | } | |
120 | ||
5246b49d BA |
121 | // Used for Monochrome variant (TODO: harmonize: !canFlip ==> showFirstTurn) |
122 | get showFirstTurn() { | |
123 | return false; | |
124 | } | |
125 | ||
71ef1664 BA |
126 | // Some variants always show the same orientation |
127 | static get CanFlip() { | |
128 | return true; | |
129 | } | |
130 | get canFlip() { | |
131 | return V.CanFlip; | |
132 | } | |
133 | ||
107dc1bd BA |
134 | // For (generally old) variants without checkered board |
135 | static get Monochrome() { | |
136 | return false; | |
137 | } | |
138 | ||
173f11dc | 139 | // Some games are drawn unusually (bottom right corner is black) |
157a72c8 BA |
140 | static get DarkBottomRight() { |
141 | return false; | |
142 | } | |
143 | ||
107dc1bd BA |
144 | // Some variants require lines drawing |
145 | static get Lines() { | |
146 | if (V.Monochrome) { | |
147 | let lines = []; | |
148 | // Draw all inter-squares lines | |
149 | for (let i = 0; i <= V.size.x; i++) | |
150 | lines.push([[i, 0], [i, V.size.y]]); | |
151 | for (let j = 0; j <= V.size.y; j++) | |
152 | lines.push([[0, j], [V.size.x, j]]); | |
153 | return lines; | |
154 | } | |
155 | return null; | |
156 | } | |
157 | ||
9a1e3abe BA |
158 | // In some variants, the player who repeat a position loses |
159 | static get LoseOnRepetition() { | |
d2af3400 BA |
160 | return false; |
161 | } | |
f0a812b7 BA |
162 | // And in some others (Iceage), repetitions should be ignored: |
163 | static get IgnoreRepetition() { | |
164 | return false; | |
165 | } | |
809ab1a8 BA |
166 | loseOnRepetition() { |
167 | // In some variants, result depends on the position: | |
168 | return V.LoseOnRepetition; | |
169 | } | |
d2af3400 BA |
170 | |
171 | // At some stages, some games could wait clicks only: | |
172 | onlyClick() { | |
9a1e3abe BA |
173 | return false; |
174 | } | |
175 | ||
61656127 BA |
176 | // Some variants use click infos: |
177 | doClick() { | |
178 | return null; | |
179 | } | |
180 | ||
90df90bc BA |
181 | // Some variants may need to highlight squares on hover (Hamilton, Weiqi...) |
182 | hoverHighlight() { | |
183 | return false; | |
184 | } | |
185 | ||
14edde72 BA |
186 | static get IMAGE_EXTENSION() { |
187 | // All pieces should be in the SVG format | |
188 | return ".svg"; | |
189 | } | |
190 | ||
1c9f093d | 191 | // Turn "wb" into "B" (for FEN) |
6808d7a1 BA |
192 | static board2fen(b) { |
193 | return b[0] == "w" ? b[1].toUpperCase() : b[1]; | |
1c9f093d BA |
194 | } |
195 | ||
196 | // Turn "p" into "bp" (for board) | |
6808d7a1 | 197 | static fen2board(f) { |
6cc34165 | 198 | return f.charCodeAt(0) <= 90 ? "w" + f.toLowerCase() : "b" + f; |
1c9f093d BA |
199 | } |
200 | ||
68e19a44 | 201 | // Check if FEN describes a board situation correctly |
6808d7a1 | 202 | static IsGoodFen(fen) { |
1c9f093d BA |
203 | const fenParsed = V.ParseFen(fen); |
204 | // 1) Check position | |
6808d7a1 | 205 | if (!V.IsGoodPosition(fenParsed.position)) return false; |
1c9f093d | 206 | // 2) Check turn |
6808d7a1 | 207 | if (!fenParsed.turn || !V.IsGoodTurn(fenParsed.turn)) return false; |
1c9f093d | 208 | // 3) Check moves count |
e50a8025 | 209 | if (!fenParsed.movesCount || !(parseInt(fenParsed.movesCount, 10) >= 0)) |
1c9f093d BA |
210 | return false; |
211 | // 4) Check flags | |
212 | if (V.HasFlags && (!fenParsed.flags || !V.IsGoodFlags(fenParsed.flags))) | |
213 | return false; | |
214 | // 5) Check enpassant | |
6808d7a1 BA |
215 | if ( |
216 | V.HasEnpassant && | |
217 | (!fenParsed.enpassant || !V.IsGoodEnpassant(fenParsed.enpassant)) | |
218 | ) { | |
1c9f093d BA |
219 | return false; |
220 | } | |
221 | return true; | |
222 | } | |
223 | ||
224 | // Is position part of the FEN a priori correct? | |
6808d7a1 BA |
225 | static IsGoodPosition(position) { |
226 | if (position.length == 0) return false; | |
1c9f093d | 227 | const rows = position.split("/"); |
6808d7a1 | 228 | if (rows.length != V.size.x) return false; |
6f2f9437 | 229 | let kings = { "k": 0, "K": 0 }; |
6808d7a1 | 230 | for (let row of rows) { |
1c9f093d | 231 | let sumElts = 0; |
6808d7a1 | 232 | for (let i = 0; i < row.length; i++) { |
6f2f9437 | 233 | if (['K','k'].includes(row[i])) kings[row[i]]++; |
6808d7a1 BA |
234 | if (V.PIECES.includes(row[i].toLowerCase())) sumElts++; |
235 | else { | |
e50a8025 | 236 | const num = parseInt(row[i], 10); |
173f11dc | 237 | if (isNaN(num) || num <= 0) return false; |
1c9f093d BA |
238 | sumElts += num; |
239 | } | |
240 | } | |
6808d7a1 | 241 | if (sumElts != V.size.y) return false; |
1c9f093d | 242 | } |
6f2f9437 BA |
243 | // Both kings should be on board. Exactly one per color. |
244 | if (Object.values(kings).some(v => v != 1)) return false; | |
1c9f093d BA |
245 | return true; |
246 | } | |
247 | ||
248 | // For FEN checking | |
6808d7a1 BA |
249 | static IsGoodTurn(turn) { |
250 | return ["w", "b"].includes(turn); | |
1c9f093d BA |
251 | } |
252 | ||
253 | // For FEN checking | |
6808d7a1 | 254 | static IsGoodFlags(flags) { |
3a2a7b5f BA |
255 | // NOTE: a little too permissive to work with more variants |
256 | return !!flags.match(/^[a-z]{4,4}$/); | |
1c9f093d BA |
257 | } |
258 | ||
472c0c4f | 259 | // NOTE: not with regexp to adapt to different board sizes. (TODO?) |
6808d7a1 BA |
260 | static IsGoodEnpassant(enpassant) { |
261 | if (enpassant != "-") { | |
262 | const ep = V.SquareToCoords(enpassant); | |
263 | if (isNaN(ep.x) || !V.OnBoard(ep)) return false; | |
1c9f093d BA |
264 | } |
265 | return true; | |
266 | } | |
267 | ||
268 | // 3 --> d (column number to letter) | |
6808d7a1 | 269 | static CoordToColumn(colnum) { |
1c9f093d BA |
270 | return String.fromCharCode(97 + colnum); |
271 | } | |
272 | ||
273 | // d --> 3 (column letter to number) | |
6808d7a1 | 274 | static ColumnToCoord(column) { |
1c9f093d BA |
275 | return column.charCodeAt(0) - 97; |
276 | } | |
277 | ||
278 | // a4 --> {x:3,y:0} | |
6808d7a1 | 279 | static SquareToCoords(sq) { |
1c9f093d BA |
280 | return { |
281 | // NOTE: column is always one char => max 26 columns | |
282 | // row is counted from black side => subtraction | |
e50a8025 | 283 | x: V.size.x - parseInt(sq.substr(1), 10), |
1c9f093d BA |
284 | y: sq[0].charCodeAt() - 97 |
285 | }; | |
286 | } | |
287 | ||
288 | // {x:0,y:4} --> e8 | |
6808d7a1 | 289 | static CoordsToSquare(coords) { |
1c9f093d BA |
290 | return V.CoordToColumn(coords.y) + (V.size.x - coords.x); |
291 | } | |
292 | ||
305ede7e | 293 | // Path to pieces (standard ones in pieces/ folder) |
241bf8f2 | 294 | getPpath(b) { |
305ede7e | 295 | return b; |
241bf8f2 BA |
296 | } |
297 | ||
3a2a7b5f | 298 | // Path to promotion pieces (usually the same) |
c7550017 BA |
299 | getPPpath(m) { |
300 | return this.getPpath(m.appear[0].c + m.appear[0].p); | |
3a2a7b5f BA |
301 | } |
302 | ||
1c9f093d | 303 | // Aggregates flags into one object |
6808d7a1 | 304 | aggregateFlags() { |
1c9f093d BA |
305 | return this.castleFlags; |
306 | } | |
307 | ||
308 | // Reverse operation | |
6808d7a1 | 309 | disaggregateFlags(flags) { |
1c9f093d BA |
310 | this.castleFlags = flags; |
311 | } | |
312 | ||
313 | // En-passant square, if any | |
6808d7a1 | 314 | getEpSquare(moveOrSquare) { |
4a209313 | 315 | if (!moveOrSquare) return undefined; //TODO: necessary line?! |
6808d7a1 | 316 | if (typeof moveOrSquare === "string") { |
1c9f093d | 317 | const square = moveOrSquare; |
6808d7a1 | 318 | if (square == "-") return undefined; |
1c9f093d BA |
319 | return V.SquareToCoords(square); |
320 | } | |
321 | // Argument is a move: | |
322 | const move = moveOrSquare; | |
1c5bfdf2 BA |
323 | const s = move.start, |
324 | e = move.end; | |
6808d7a1 | 325 | if ( |
1c5bfdf2 | 326 | s.y == e.y && |
0d5335de BA |
327 | Math.abs(s.x - e.x) == 2 && |
328 | // Next conditions for variants like Atomic or Rifle, Recycle... | |
329 | (move.appear.length > 0 && move.appear[0].p == V.PAWN) && | |
330 | (move.vanish.length > 0 && move.vanish[0].p == V.PAWN) | |
6808d7a1 | 331 | ) { |
1c9f093d | 332 | return { |
1c5bfdf2 BA |
333 | x: (s.x + e.x) / 2, |
334 | y: s.y | |
1c9f093d BA |
335 | }; |
336 | } | |
337 | return undefined; //default | |
338 | } | |
339 | ||
340 | // Can thing on square1 take thing on square2 | |
6808d7a1 BA |
341 | canTake([x1, y1], [x2, y2]) { |
342 | return this.getColor(x1, y1) !== this.getColor(x2, y2); | |
1c9f093d BA |
343 | } |
344 | ||
345 | // Is (x,y) on the chessboard? | |
6808d7a1 BA |
346 | static OnBoard(x, y) { |
347 | return x >= 0 && x < V.size.x && y >= 0 && y < V.size.y; | |
1c9f093d BA |
348 | } |
349 | ||
350 | // Used in interface: 'side' arg == player color | |
6808d7a1 BA |
351 | canIplay(side, [x, y]) { |
352 | return this.turn == side && this.getColor(x, y) == side; | |
1c9f093d BA |
353 | } |
354 | ||
355 | // On which squares is color under check ? (for interface) | |
af34341d BA |
356 | getCheckSquares() { |
357 | const color = this.turn; | |
b0a0468a BA |
358 | return ( |
359 | this.underCheck(color) | |
2c5d7b20 BA |
360 | // kingPos must be duplicated, because it may change: |
361 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] | |
b0a0468a BA |
362 | : [] |
363 | ); | |
1c9f093d BA |
364 | } |
365 | ||
366 | ///////////// | |
367 | // FEN UTILS | |
368 | ||
7ba4a5bc | 369 | // Setup the initial random (asymmetric) position |
eb2d61de | 370 | static GenRandInitFen(options) { |
cee75a57 BA |
371 | const randomness = parseInt(options.randomness, 10); |
372 | if (!randomness || randomness == 0) | |
7ba4a5bc | 373 | // Deterministic: |
3a2a7b5f | 374 | return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 ahah -"; |
7ba4a5bc | 375 | |
6808d7a1 | 376 | let pieces = { w: new Array(8), b: new Array(8) }; |
3a2a7b5f | 377 | let flags = ""; |
7ba4a5bc | 378 | // Shuffle pieces on first (and last rank if randomness == 2) |
6808d7a1 | 379 | for (let c of ["w", "b"]) { |
eb2d61de | 380 | if (c == 'b' && options.randomness == 1) { |
7ba4a5bc | 381 | pieces['b'] = pieces['w']; |
3a2a7b5f | 382 | flags += flags; |
7ba4a5bc BA |
383 | break; |
384 | } | |
385 | ||
1c9f093d BA |
386 | let positions = ArrayFun.range(8); |
387 | ||
388 | // Get random squares for bishops | |
656b1878 | 389 | let randIndex = 2 * randInt(4); |
1c9f093d BA |
390 | const bishop1Pos = positions[randIndex]; |
391 | // The second bishop must be on a square of different color | |
656b1878 | 392 | let randIndex_tmp = 2 * randInt(4) + 1; |
1c9f093d BA |
393 | const bishop2Pos = positions[randIndex_tmp]; |
394 | // Remove chosen squares | |
6808d7a1 BA |
395 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
396 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); | |
1c9f093d BA |
397 | |
398 | // Get random squares for knights | |
656b1878 | 399 | randIndex = randInt(6); |
1c9f093d BA |
400 | const knight1Pos = positions[randIndex]; |
401 | positions.splice(randIndex, 1); | |
656b1878 | 402 | randIndex = randInt(5); |
1c9f093d BA |
403 | const knight2Pos = positions[randIndex]; |
404 | positions.splice(randIndex, 1); | |
405 | ||
406 | // Get random square for queen | |
656b1878 | 407 | randIndex = randInt(4); |
1c9f093d BA |
408 | const queenPos = positions[randIndex]; |
409 | positions.splice(randIndex, 1); | |
410 | ||
411 | // Rooks and king positions are now fixed, | |
412 | // because of the ordering rook-king-rook | |
413 | const rook1Pos = positions[0]; | |
414 | const kingPos = positions[1]; | |
415 | const rook2Pos = positions[2]; | |
416 | ||
417 | // Finally put the shuffled pieces in the board array | |
6808d7a1 BA |
418 | pieces[c][rook1Pos] = "r"; |
419 | pieces[c][knight1Pos] = "n"; | |
420 | pieces[c][bishop1Pos] = "b"; | |
421 | pieces[c][queenPos] = "q"; | |
422 | pieces[c][kingPos] = "k"; | |
423 | pieces[c][bishop2Pos] = "b"; | |
424 | pieces[c][knight2Pos] = "n"; | |
425 | pieces[c][rook2Pos] = "r"; | |
3a2a7b5f | 426 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
1c9f093d | 427 | } |
e3e2cc44 | 428 | // Add turn + flags + enpassant |
6808d7a1 BA |
429 | return ( |
430 | pieces["b"].join("") + | |
1c9f093d BA |
431 | "/pppppppp/8/8/8/8/PPPPPPPP/" + |
432 | pieces["w"].join("").toUpperCase() + | |
3a2a7b5f | 433 | " w 0 " + flags + " -" |
e3e2cc44 | 434 | ); |
1c9f093d BA |
435 | } |
436 | ||
437 | // "Parse" FEN: just return untransformed string data | |
6808d7a1 | 438 | static ParseFen(fen) { |
1c9f093d | 439 | const fenParts = fen.split(" "); |
6808d7a1 | 440 | let res = { |
1c9f093d BA |
441 | position: fenParts[0], |
442 | turn: fenParts[1], | |
6808d7a1 | 443 | movesCount: fenParts[2] |
1c9f093d BA |
444 | }; |
445 | let nextIdx = 3; | |
6808d7a1 BA |
446 | if (V.HasFlags) Object.assign(res, { flags: fenParts[nextIdx++] }); |
447 | if (V.HasEnpassant) Object.assign(res, { enpassant: fenParts[nextIdx] }); | |
1c9f093d BA |
448 | return res; |
449 | } | |
450 | ||
451 | // Return current fen (game state) | |
6808d7a1 BA |
452 | getFen() { |
453 | return ( | |
f9c36b2d BA |
454 | this.getBaseFen() + " " + |
455 | this.getTurnFen() + " " + | |
6808d7a1 BA |
456 | this.movesCount + |
457 | (V.HasFlags ? " " + this.getFlagsFen() : "") + | |
458 | (V.HasEnpassant ? " " + this.getEnpassantFen() : "") | |
459 | ); | |
1c9f093d BA |
460 | } |
461 | ||
f9c36b2d BA |
462 | getFenForRepeat() { |
463 | // Omit movesCount, only variable allowed to differ | |
464 | return ( | |
465 | this.getBaseFen() + "_" + | |
466 | this.getTurnFen() + | |
467 | (V.HasFlags ? "_" + this.getFlagsFen() : "") + | |
468 | (V.HasEnpassant ? "_" + this.getEnpassantFen() : "") | |
469 | ); | |
470 | } | |
471 | ||
1c9f093d | 472 | // Position part of the FEN string |
6808d7a1 | 473 | getBaseFen() { |
6f2f9437 BA |
474 | const format = (count) => { |
475 | // if more than 9 consecutive free spaces, break the integer, | |
476 | // otherwise FEN parsing will fail. | |
477 | if (count <= 9) return count; | |
7c05a5f2 BA |
478 | // Most boards of size < 18: |
479 | if (count <= 18) return "9" + (count - 9); | |
480 | // Except Gomoku: | |
481 | return "99" + (count - 18); | |
6f2f9437 | 482 | }; |
1c9f093d | 483 | let position = ""; |
6808d7a1 | 484 | for (let i = 0; i < V.size.x; i++) { |
1c9f093d | 485 | let emptyCount = 0; |
6808d7a1 BA |
486 | for (let j = 0; j < V.size.y; j++) { |
487 | if (this.board[i][j] == V.EMPTY) emptyCount++; | |
488 | else { | |
489 | if (emptyCount > 0) { | |
1c9f093d | 490 | // Add empty squares in-between |
6f2f9437 | 491 | position += format(emptyCount); |
1c9f093d BA |
492 | emptyCount = 0; |
493 | } | |
494 | position += V.board2fen(this.board[i][j]); | |
495 | } | |
496 | } | |
6808d7a1 | 497 | if (emptyCount > 0) { |
1c9f093d | 498 | // "Flush remainder" |
6f2f9437 | 499 | position += format(emptyCount); |
1c9f093d | 500 | } |
6808d7a1 | 501 | if (i < V.size.x - 1) position += "/"; //separate rows |
1c9f093d BA |
502 | } |
503 | return position; | |
504 | } | |
505 | ||
6808d7a1 | 506 | getTurnFen() { |
1c9f093d BA |
507 | return this.turn; |
508 | } | |
509 | ||
510 | // Flags part of the FEN string | |
6808d7a1 | 511 | getFlagsFen() { |
1c9f093d | 512 | let flags = ""; |
3a2a7b5f BA |
513 | // Castling flags |
514 | for (let c of ["w", "b"]) | |
515 | flags += this.castleFlags[c].map(V.CoordToColumn).join(""); | |
1c9f093d BA |
516 | return flags; |
517 | } | |
518 | ||
519 | // Enpassant part of the FEN string | |
6808d7a1 | 520 | getEnpassantFen() { |
1c9f093d | 521 | const L = this.epSquares.length; |
6808d7a1 BA |
522 | if (!this.epSquares[L - 1]) return "-"; //no en-passant |
523 | return V.CoordsToSquare(this.epSquares[L - 1]); | |
1c9f093d BA |
524 | } |
525 | ||
526 | // Turn position fen into double array ["wb","wp","bk",...] | |
6808d7a1 | 527 | static GetBoard(position) { |
1c9f093d BA |
528 | const rows = position.split("/"); |
529 | let board = ArrayFun.init(V.size.x, V.size.y, ""); | |
6808d7a1 | 530 | for (let i = 0; i < rows.length; i++) { |
1c9f093d | 531 | let j = 0; |
6808d7a1 | 532 | for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) { |
1c9f093d | 533 | const character = rows[i][indexInRow]; |
e50a8025 | 534 | const num = parseInt(character, 10); |
a13cbc0f | 535 | // If num is a number, just shift j: |
6808d7a1 | 536 | if (!isNaN(num)) j += num; |
a13cbc0f | 537 | // Else: something at position i,j |
6808d7a1 | 538 | else board[i][j++] = V.fen2board(character); |
1c9f093d BA |
539 | } |
540 | } | |
541 | return board; | |
542 | } | |
543 | ||
544 | // Extract (relevant) flags from fen | |
6808d7a1 | 545 | setFlags(fenflags) { |
1c9f093d | 546 | // white a-castle, h-castle, black a-castle, h-castle |
bb688df5 | 547 | this.castleFlags = { w: [-1, -1], b: [-1, -1] }; |
3a2a7b5f BA |
548 | for (let i = 0; i < 4; i++) { |
549 | this.castleFlags[i < 2 ? "w" : "b"][i % 2] = | |
550 | V.ColumnToCoord(fenflags.charAt(i)); | |
551 | } | |
1c9f093d BA |
552 | } |
553 | ||
554 | ////////////////// | |
555 | // INITIALIZATION | |
556 | ||
37cdcbf3 | 557 | // Fen string fully describes the game state |
b627d118 BA |
558 | constructor(fen) { |
559 | if (!fen) | |
560 | // In printDiagram() fen isn't supply because only getPpath() is used | |
561 | // TODO: find a better solution! | |
562 | return; | |
1c9f093d BA |
563 | const fenParsed = V.ParseFen(fen); |
564 | this.board = V.GetBoard(fenParsed.position); | |
af34341d | 565 | this.turn = fenParsed.turn; |
e50a8025 | 566 | this.movesCount = parseInt(fenParsed.movesCount, 10); |
1c9f093d BA |
567 | this.setOtherVariables(fen); |
568 | } | |
569 | ||
3a2a7b5f | 570 | // Scan board for kings positions |
9d15c433 | 571 | // TODO: should be done from board, no need for the complete FEN |
3a2a7b5f | 572 | scanKings(fen) { |
2c5d7b20 BA |
573 | // Squares of white and black king: |
574 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; | |
1c9f093d | 575 | const fenRows = V.ParseFen(fen).position.split("/"); |
6808d7a1 | 576 | for (let i = 0; i < fenRows.length; i++) { |
1c9f093d | 577 | let k = 0; //column index on board |
6808d7a1 BA |
578 | for (let j = 0; j < fenRows[i].length; j++) { |
579 | switch (fenRows[i].charAt(j)) { | |
580 | case "k": | |
581 | this.kingPos["b"] = [i, k]; | |
1c9f093d | 582 | break; |
6808d7a1 BA |
583 | case "K": |
584 | this.kingPos["w"] = [i, k]; | |
1c9f093d | 585 | break; |
6808d7a1 | 586 | default: { |
e50a8025 | 587 | const num = parseInt(fenRows[i].charAt(j), 10); |
6808d7a1 BA |
588 | if (!isNaN(num)) k += num - 1; |
589 | } | |
1c9f093d BA |
590 | } |
591 | k++; | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | // Some additional variables from FEN (variant dependant) | |
6808d7a1 | 597 | setOtherVariables(fen) { |
1c9f093d BA |
598 | // Set flags and enpassant: |
599 | const parsedFen = V.ParseFen(fen); | |
6808d7a1 BA |
600 | if (V.HasFlags) this.setFlags(parsedFen.flags); |
601 | if (V.HasEnpassant) { | |
602 | const epSq = | |
603 | parsedFen.enpassant != "-" | |
9bd6786b | 604 | ? this.getEpSquare(parsedFen.enpassant) |
6808d7a1 BA |
605 | : undefined; |
606 | this.epSquares = [epSq]; | |
1c9f093d | 607 | } |
3a2a7b5f BA |
608 | // Search for kings positions: |
609 | this.scanKings(fen); | |
1c9f093d BA |
610 | } |
611 | ||
612 | ///////////////////// | |
613 | // GETTERS & SETTERS | |
614 | ||
6808d7a1 BA |
615 | static get size() { |
616 | return { x: 8, y: 8 }; | |
1c9f093d BA |
617 | } |
618 | ||
0ba6420d | 619 | // Color of thing on square (i,j). 'undefined' if square is empty |
6808d7a1 | 620 | getColor(i, j) { |
1c9f093d BA |
621 | return this.board[i][j].charAt(0); |
622 | } | |
623 | ||
624 | // Piece type on square (i,j). 'undefined' if square is empty | |
6808d7a1 | 625 | getPiece(i, j) { |
1c9f093d BA |
626 | return this.board[i][j].charAt(1); |
627 | } | |
628 | ||
629 | // Get opponent color | |
6808d7a1 BA |
630 | static GetOppCol(color) { |
631 | return color == "w" ? "b" : "w"; | |
1c9f093d BA |
632 | } |
633 | ||
1c9f093d | 634 | // Pieces codes (for a clearer code) |
6808d7a1 BA |
635 | static get PAWN() { |
636 | return "p"; | |
637 | } | |
638 | static get ROOK() { | |
639 | return "r"; | |
640 | } | |
641 | static get KNIGHT() { | |
642 | return "n"; | |
643 | } | |
644 | static get BISHOP() { | |
645 | return "b"; | |
646 | } | |
647 | static get QUEEN() { | |
648 | return "q"; | |
649 | } | |
650 | static get KING() { | |
651 | return "k"; | |
652 | } | |
1c9f093d BA |
653 | |
654 | // For FEN checking: | |
6808d7a1 BA |
655 | static get PIECES() { |
656 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING]; | |
1c9f093d BA |
657 | } |
658 | ||
659 | // Empty square | |
6808d7a1 BA |
660 | static get EMPTY() { |
661 | return ""; | |
662 | } | |
1c9f093d BA |
663 | |
664 | // Some pieces movements | |
6808d7a1 | 665 | static get steps() { |
1c9f093d | 666 | return { |
6808d7a1 BA |
667 | r: [ |
668 | [-1, 0], | |
669 | [1, 0], | |
670 | [0, -1], | |
671 | [0, 1] | |
672 | ], | |
673 | n: [ | |
674 | [-1, -2], | |
675 | [-1, 2], | |
676 | [1, -2], | |
677 | [1, 2], | |
678 | [-2, -1], | |
679 | [-2, 1], | |
680 | [2, -1], | |
681 | [2, 1] | |
682 | ], | |
683 | b: [ | |
684 | [-1, -1], | |
685 | [-1, 1], | |
686 | [1, -1], | |
687 | [1, 1] | |
688 | ] | |
1c9f093d BA |
689 | }; |
690 | } | |
691 | ||
692 | //////////////////// | |
693 | // MOVES GENERATION | |
694 | ||
0ba6420d | 695 | // All possible moves from selected square |
173f11dc BA |
696 | getPotentialMovesFrom(sq) { |
697 | switch (this.getPiece(sq[0], sq[1])) { | |
698 | case V.PAWN: return this.getPotentialPawnMoves(sq); | |
699 | case V.ROOK: return this.getPotentialRookMoves(sq); | |
700 | case V.KNIGHT: return this.getPotentialKnightMoves(sq); | |
701 | case V.BISHOP: return this.getPotentialBishopMoves(sq); | |
702 | case V.QUEEN: return this.getPotentialQueenMoves(sq); | |
703 | case V.KING: return this.getPotentialKingMoves(sq); | |
1c9f093d | 704 | } |
2a0672a9 | 705 | return []; //never reached (but some variants may use it: Bario...) |
1c9f093d BA |
706 | } |
707 | ||
708 | // Build a regular move from its initial and destination squares. | |
709 | // tr: transformation | |
6808d7a1 | 710 | getBasicMove([sx, sy], [ex, ey], tr) { |
1c58eb76 | 711 | const initColor = this.getColor(sx, sy); |
7e8a7ea1 | 712 | const initPiece = this.board[sx][sy].charAt(1); |
1c9f093d BA |
713 | let mv = new Move({ |
714 | appear: [ | |
715 | new PiPo({ | |
716 | x: ex, | |
717 | y: ey, | |
173f11dc BA |
718 | c: !!tr ? tr.c : initColor, |
719 | p: !!tr ? tr.p : initPiece | |
1c9f093d BA |
720 | }) |
721 | ], | |
722 | vanish: [ | |
723 | new PiPo({ | |
724 | x: sx, | |
725 | y: sy, | |
1c58eb76 BA |
726 | c: initColor, |
727 | p: initPiece | |
1c9f093d BA |
728 | }) |
729 | ] | |
730 | }); | |
731 | ||
732 | // The opponent piece disappears if we take it | |
6808d7a1 | 733 | if (this.board[ex][ey] != V.EMPTY) { |
1c9f093d BA |
734 | mv.vanish.push( |
735 | new PiPo({ | |
736 | x: ex, | |
737 | y: ey, | |
6808d7a1 | 738 | c: this.getColor(ex, ey), |
7e8a7ea1 | 739 | p: this.board[ex][ey].charAt(1) |
1c9f093d BA |
740 | }) |
741 | ); | |
742 | } | |
1c5bfdf2 | 743 | |
1c9f093d BA |
744 | return mv; |
745 | } | |
746 | ||
747 | // Generic method to find possible moves of non-pawn pieces: | |
748 | // "sliding or jumping" | |
6808d7a1 | 749 | getSlideNJumpMoves([x, y], steps, oneStep) { |
1c9f093d | 750 | let moves = []; |
6808d7a1 | 751 | outerLoop: for (let step of steps) { |
1c9f093d BA |
752 | let i = x + step[0]; |
753 | let j = y + step[1]; | |
6808d7a1 BA |
754 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
755 | moves.push(this.getBasicMove([x, y], [i, j])); | |
3208c667 | 756 | if (!!oneStep) continue outerLoop; |
1c9f093d BA |
757 | i += step[0]; |
758 | j += step[1]; | |
759 | } | |
6808d7a1 BA |
760 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j])) |
761 | moves.push(this.getBasicMove([x, y], [i, j])); | |
1c9f093d BA |
762 | } |
763 | return moves; | |
764 | } | |
765 | ||
32f6285e BA |
766 | // Special case of en-passant captures: treated separately |
767 | getEnpassantCaptures([x, y], shiftX) { | |
768 | const Lep = this.epSquares.length; | |
769 | const epSquare = this.epSquares[Lep - 1]; //always at least one element | |
770 | let enpassantMove = null; | |
771 | if ( | |
772 | !!epSquare && | |
773 | epSquare.x == x + shiftX && | |
774 | Math.abs(epSquare.y - y) == 1 | |
775 | ) { | |
776 | enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); | |
777 | enpassantMove.vanish.push({ | |
778 | x: x, | |
779 | y: epSquare.y, | |
7e8a7ea1 | 780 | p: this.board[x][epSquare.y].charAt(1), |
32f6285e BA |
781 | c: this.getColor(x, epSquare.y) |
782 | }); | |
783 | } | |
784 | return !!enpassantMove ? [enpassantMove] : []; | |
785 | } | |
786 | ||
1c58eb76 BA |
787 | // Consider all potential promotions: |
788 | addPawnMoves([x1, y1], [x2, y2], moves, promotions) { | |
789 | let finalPieces = [V.PAWN]; | |
af34341d | 790 | const color = this.turn; //this.getColor(x1, y1); |
1c58eb76 BA |
791 | const lastRank = (color == "w" ? 0 : V.size.x - 1); |
792 | if (x2 == lastRank) { | |
793 | // promotions arg: special override for Hiddenqueen variant | |
794 | if (!!promotions) finalPieces = promotions; | |
15d69043 | 795 | else if (!!V.PawnSpecs.promotions) finalPieces = V.PawnSpecs.promotions; |
1c58eb76 | 796 | } |
1c58eb76 | 797 | for (let piece of finalPieces) { |
3b98a861 | 798 | const tr = (piece != V.PAWN ? { c: color, p: piece } : null); |
1c58eb76 BA |
799 | moves.push(this.getBasicMove([x1, y1], [x2, y2], tr)); |
800 | } | |
801 | } | |
802 | ||
1c9f093d | 803 | // What are the pawn moves from square x,y ? |
32f6285e | 804 | getPotentialPawnMoves([x, y], promotions) { |
af34341d | 805 | const color = this.turn; //this.getColor(x, y); |
6808d7a1 | 806 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
32f6285e | 807 | const pawnShiftX = V.PawnSpecs.directions[color]; |
1c58eb76 | 808 | const firstRank = (color == "w" ? sizeX - 1 : 0); |
0b8bd121 | 809 | const forward = (color == 'w' ? -1 : 1); |
32f6285e BA |
810 | |
811 | // Pawn movements in shiftX direction: | |
812 | const getPawnMoves = (shiftX) => { | |
813 | let moves = []; | |
814 | // NOTE: next condition is generally true (no pawn on last rank) | |
815 | if (x + shiftX >= 0 && x + shiftX < sizeX) { | |
816 | if (this.board[x + shiftX][y] == V.EMPTY) { | |
0b8bd121 | 817 | // One square forward (or backward) |
1c58eb76 | 818 | this.addPawnMoves([x, y], [x + shiftX, y], moves, promotions); |
32f6285e BA |
819 | // Next condition because pawns on 1st rank can generally jump |
820 | if ( | |
821 | V.PawnSpecs.twoSquares && | |
472c0c4f BA |
822 | ( |
823 | (color == 'w' && x >= V.size.x - 1 - V.PawnSpecs.initShift['w']) | |
824 | || | |
825 | (color == 'b' && x <= V.PawnSpecs.initShift['b']) | |
826 | ) | |
32f6285e | 827 | ) { |
0b8bd121 BA |
828 | if ( |
829 | shiftX == forward && | |
830 | this.board[x + 2 * shiftX][y] == V.EMPTY | |
831 | ) { | |
472c0c4f BA |
832 | // Two squares jump |
833 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); | |
834 | if ( | |
835 | V.PawnSpecs.threeSquares && | |
836 | this.board[x + 3 * shiftX][y] == V.EMPTY | |
837 | ) { | |
838 | // Three squares jump | |
839 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); | |
840 | } | |
841 | } | |
32f6285e BA |
842 | } |
843 | } | |
844 | // Captures | |
845 | if (V.PawnSpecs.canCapture) { | |
846 | for (let shiftY of [-1, 1]) { | |
15d69043 | 847 | if (y + shiftY >= 0 && y + shiftY < sizeY) { |
32f6285e BA |
848 | if ( |
849 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
850 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
851 | ) { | |
1c58eb76 BA |
852 | this.addPawnMoves( |
853 | [x, y], [x + shiftX, y + shiftY], | |
854 | moves, promotions | |
855 | ); | |
32f6285e BA |
856 | } |
857 | if ( | |
0b8bd121 | 858 | V.PawnSpecs.captureBackward && shiftX == forward && |
32f6285e BA |
859 | x - shiftX >= 0 && x - shiftX < V.size.x && |
860 | this.board[x - shiftX][y + shiftY] != V.EMPTY && | |
861 | this.canTake([x, y], [x - shiftX, y + shiftY]) | |
862 | ) { | |
1c58eb76 | 863 | this.addPawnMoves( |
0b8bd121 | 864 | [x, y], [x - shiftX, y + shiftY], |
1c58eb76 BA |
865 | moves, promotions |
866 | ); | |
32f6285e BA |
867 | } |
868 | } | |
1c9f093d BA |
869 | } |
870 | } | |
871 | } | |
32f6285e | 872 | return moves; |
1c9f093d BA |
873 | } |
874 | ||
32f6285e BA |
875 | let pMoves = getPawnMoves(pawnShiftX); |
876 | if (V.PawnSpecs.bidirectional) | |
877 | pMoves = pMoves.concat(getPawnMoves(-pawnShiftX)); | |
878 | ||
6808d7a1 | 879 | if (V.HasEnpassant) { |
32f6285e BA |
880 | // NOTE: backward en-passant captures are not considered |
881 | // because no rules define them (for now). | |
882 | Array.prototype.push.apply( | |
883 | pMoves, | |
884 | this.getEnpassantCaptures([x, y], pawnShiftX) | |
885 | ); | |
1c9f093d | 886 | } |
294fe29f | 887 | |
32f6285e | 888 | return pMoves; |
1c9f093d BA |
889 | } |
890 | ||
891 | // What are the rook moves from square x,y ? | |
6808d7a1 | 892 | getPotentialRookMoves(sq) { |
1c9f093d BA |
893 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]); |
894 | } | |
895 | ||
896 | // What are the knight moves from square x,y ? | |
6808d7a1 | 897 | getPotentialKnightMoves(sq) { |
1c9f093d BA |
898 | return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"); |
899 | } | |
900 | ||
901 | // What are the bishop moves from square x,y ? | |
6808d7a1 | 902 | getPotentialBishopMoves(sq) { |
1c9f093d BA |
903 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]); |
904 | } | |
905 | ||
906 | // What are the queen moves from square x,y ? | |
6808d7a1 BA |
907 | getPotentialQueenMoves(sq) { |
908 | return this.getSlideNJumpMoves( | |
909 | sq, | |
910 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]) | |
911 | ); | |
1c9f093d BA |
912 | } |
913 | ||
914 | // What are the king moves from square x,y ? | |
6808d7a1 | 915 | getPotentialKingMoves(sq) { |
1c9f093d | 916 | // Initialize with normal moves |
c583ef1c | 917 | let moves = this.getSlideNJumpMoves( |
6808d7a1 BA |
918 | sq, |
919 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
920 | "oneStep" | |
921 | ); | |
7e8a7ea1 BA |
922 | if (V.HasCastle && this.castleFlags[this.turn].some(v => v < V.size.y)) |
923 | moves = moves.concat(this.getCastleMoves(sq)); | |
c583ef1c | 924 | return moves; |
1c9f093d BA |
925 | } |
926 | ||
a6836242 | 927 | // "castleInCheck" arg to let some variants castle under check |
7e8a7ea1 | 928 | getCastleMoves([x, y], finalSquares, castleInCheck, castleWith) { |
6808d7a1 | 929 | const c = this.getColor(x, y); |
1c9f093d BA |
930 | |
931 | // Castling ? | |
932 | const oppCol = V.GetOppCol(c); | |
933 | let moves = []; | |
9bd6786b | 934 | // King, then rook: |
7e8a7ea1 BA |
935 | finalSquares = finalSquares || [ [2, 3], [V.size.y - 2, V.size.y - 3] ]; |
936 | const castlingKing = this.board[x][y].charAt(1); | |
6808d7a1 BA |
937 | castlingCheck: for ( |
938 | let castleSide = 0; | |
939 | castleSide < 2; | |
940 | castleSide++ //large, then small | |
941 | ) { | |
3a2a7b5f | 942 | if (this.castleFlags[c][castleSide] >= V.size.y) continue; |
3f22c2c3 | 943 | // If this code is reached, rook and king are on initial position |
1c9f093d | 944 | |
2c5d7b20 | 945 | // NOTE: in some variants this is not a rook |
32f6285e | 946 | const rookPos = this.castleFlags[c][castleSide]; |
7e8a7ea1 | 947 | const castlingPiece = this.board[x][rookPos].charAt(1); |
85a1dcba BA |
948 | if ( |
949 | this.board[x][rookPos] == V.EMPTY || | |
950 | this.getColor(x, rookPos) != c || | |
7e8a7ea1 | 951 | (!!castleWith && !castleWith.includes(castlingPiece)) |
85a1dcba | 952 | ) { |
61656127 | 953 | // Rook is not here, or changed color (see Benedict) |
32f6285e | 954 | continue; |
85a1dcba | 955 | } |
32f6285e | 956 | |
2beba6db BA |
957 | // Nothing on the path of the king ? (and no checks) |
958 | const finDist = finalSquares[castleSide][0] - y; | |
959 | let step = finDist / Math.max(1, Math.abs(finDist)); | |
059f0aa2 | 960 | let i = y; |
6808d7a1 BA |
961 | do { |
962 | if ( | |
7e8a7ea1 BA |
963 | (!castleInCheck && this.isAttacked([x, i], oppCol)) || |
964 | ( | |
965 | this.board[x][i] != V.EMPTY && | |
6808d7a1 | 966 | // NOTE: next check is enough, because of chessboard constraints |
7e8a7ea1 BA |
967 | (this.getColor(x, i) != c || ![y, rookPos].includes(i)) |
968 | ) | |
6808d7a1 | 969 | ) { |
1c9f093d BA |
970 | continue castlingCheck; |
971 | } | |
2beba6db | 972 | i += step; |
6808d7a1 | 973 | } while (i != finalSquares[castleSide][0]); |
1c9f093d BA |
974 | |
975 | // Nothing on the path to the rook? | |
6808d7a1 | 976 | step = castleSide == 0 ? -1 : 1; |
3a2a7b5f | 977 | for (i = y + step; i != rookPos; i += step) { |
6808d7a1 | 978 | if (this.board[x][i] != V.EMPTY) continue castlingCheck; |
1c9f093d | 979 | } |
1c9f093d BA |
980 | |
981 | // Nothing on final squares, except maybe king and castling rook? | |
6808d7a1 BA |
982 | for (i = 0; i < 2; i++) { |
983 | if ( | |
5e1bc651 | 984 | finalSquares[castleSide][i] != rookPos && |
6808d7a1 | 985 | this.board[x][finalSquares[castleSide][i]] != V.EMPTY && |
5e1bc651 | 986 | ( |
7e8a7ea1 | 987 | finalSquares[castleSide][i] != y || |
5e1bc651 BA |
988 | this.getColor(x, finalSquares[castleSide][i]) != c |
989 | ) | |
6808d7a1 | 990 | ) { |
1c9f093d BA |
991 | continue castlingCheck; |
992 | } | |
993 | } | |
994 | ||
995 | // If this code is reached, castle is valid | |
6808d7a1 BA |
996 | moves.push( |
997 | new Move({ | |
998 | appear: [ | |
2c5d7b20 BA |
999 | new PiPo({ |
1000 | x: x, | |
1001 | y: finalSquares[castleSide][0], | |
7e8a7ea1 | 1002 | p: castlingKing, |
2c5d7b20 BA |
1003 | c: c |
1004 | }), | |
1005 | new PiPo({ | |
1006 | x: x, | |
1007 | y: finalSquares[castleSide][1], | |
1008 | p: castlingPiece, | |
1009 | c: c | |
1010 | }) | |
6808d7a1 BA |
1011 | ], |
1012 | vanish: [ | |
7e8a7ea1 | 1013 | // King might be initially disguised (Titan...) |
a19caec0 | 1014 | new PiPo({ x: x, y: y, p: castlingKing, c: c }), |
a6836242 | 1015 | new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c }) |
6808d7a1 BA |
1016 | ], |
1017 | end: | |
1018 | Math.abs(y - rookPos) <= 2 | |
1019 | ? { x: x, y: rookPos } | |
1020 | : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) } | |
1021 | }) | |
1022 | ); | |
1c9f093d BA |
1023 | } |
1024 | ||
1025 | return moves; | |
1026 | } | |
1027 | ||
1028 | //////////////////// | |
1029 | // MOVES VALIDATION | |
1030 | ||
1031 | // For the interface: possible moves for the current turn from square sq | |
6808d7a1 BA |
1032 | getPossibleMovesFrom(sq) { |
1033 | return this.filterValid(this.getPotentialMovesFrom(sq)); | |
1c9f093d BA |
1034 | } |
1035 | ||
1036 | // TODO: promotions (into R,B,N,Q) should be filtered only once | |
6808d7a1 BA |
1037 | filterValid(moves) { |
1038 | if (moves.length == 0) return []; | |
1c9f093d BA |
1039 | const color = this.turn; |
1040 | return moves.filter(m => { | |
1041 | this.play(m); | |
1042 | const res = !this.underCheck(color); | |
1043 | this.undo(m); | |
1044 | return res; | |
1045 | }); | |
1046 | } | |
1047 | ||
5e1bc651 | 1048 | getAllPotentialMoves() { |
1c9f093d | 1049 | const color = this.turn; |
1c9f093d | 1050 | let potentialMoves = []; |
6808d7a1 BA |
1051 | for (let i = 0; i < V.size.x; i++) { |
1052 | for (let j = 0; j < V.size.y; j++) { | |
156986e6 | 1053 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { |
6808d7a1 BA |
1054 | Array.prototype.push.apply( |
1055 | potentialMoves, | |
1056 | this.getPotentialMovesFrom([i, j]) | |
1057 | ); | |
1c9f093d BA |
1058 | } |
1059 | } | |
1060 | } | |
5e1bc651 BA |
1061 | return potentialMoves; |
1062 | } | |
1063 | ||
1064 | // Search for all valid moves considering current turn | |
1065 | // (for engine and game end) | |
1066 | getAllValidMoves() { | |
1067 | return this.filterValid(this.getAllPotentialMoves()); | |
1c9f093d BA |
1068 | } |
1069 | ||
1070 | // Stop at the first move found | |
2c5d7b20 | 1071 | // TODO: not really, it explores all moves from a square (one is enough). |
cdab5663 BA |
1072 | // Possible fix: add extra arg "oneMove" to getPotentialMovesFrom, |
1073 | // and then return only boolean true at first move found | |
1074 | // (in all getPotentialXXXMoves() ... for all variants ...) | |
6808d7a1 | 1075 | atLeastOneMove() { |
1c9f093d | 1076 | const color = this.turn; |
6808d7a1 BA |
1077 | for (let i = 0; i < V.size.x; i++) { |
1078 | for (let j = 0; j < V.size.y; j++) { | |
665eed90 | 1079 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { |
6808d7a1 BA |
1080 | const moves = this.getPotentialMovesFrom([i, j]); |
1081 | if (moves.length > 0) { | |
107dc1bd | 1082 | for (let k = 0; k < moves.length; k++) |
6808d7a1 | 1083 | if (this.filterValid([moves[k]]).length > 0) return true; |
1c9f093d BA |
1084 | } |
1085 | } | |
1086 | } | |
1087 | } | |
1088 | return false; | |
1089 | } | |
1090 | ||
68e19a44 BA |
1091 | // Check if pieces of given color are attacking (king) on square x,y |
1092 | isAttacked(sq, color) { | |
6808d7a1 | 1093 | return ( |
68e19a44 BA |
1094 | this.isAttackedByPawn(sq, color) || |
1095 | this.isAttackedByRook(sq, color) || | |
1096 | this.isAttackedByKnight(sq, color) || | |
1097 | this.isAttackedByBishop(sq, color) || | |
1098 | this.isAttackedByQueen(sq, color) || | |
1099 | this.isAttackedByKing(sq, color) | |
6808d7a1 | 1100 | ); |
1c9f093d BA |
1101 | } |
1102 | ||
d1be8046 | 1103 | // Generic method for non-pawn pieces ("sliding or jumping"): |
68e19a44 BA |
1104 | // is x,y attacked by a piece of given color ? |
1105 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { | |
d1be8046 BA |
1106 | for (let step of steps) { |
1107 | let rx = x + step[0], | |
1108 | ry = y + step[1]; | |
1109 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { | |
1110 | rx += step[0]; | |
1111 | ry += step[1]; | |
1112 | } | |
1113 | if ( | |
1114 | V.OnBoard(rx, ry) && | |
3cf54395 | 1115 | this.board[rx][ry] != V.EMPTY && |
68e19a44 | 1116 | this.getPiece(rx, ry) == piece && |
da9e846e | 1117 | this.getColor(rx, ry) == color |
d1be8046 BA |
1118 | ) { |
1119 | return true; | |
1120 | } | |
1121 | } | |
1122 | return false; | |
1123 | } | |
1124 | ||
68e19a44 | 1125 | // Is square x,y attacked by 'color' pawns ? |
107dc1bd | 1126 | isAttackedByPawn(sq, color) { |
68e19a44 | 1127 | const pawnShift = (color == "w" ? 1 : -1); |
107dc1bd BA |
1128 | return this.isAttackedBySlideNJump( |
1129 | sq, | |
1130 | color, | |
1131 | V.PAWN, | |
1132 | [[pawnShift, 1], [pawnShift, -1]], | |
1133 | "oneStep" | |
1134 | ); | |
1c9f093d BA |
1135 | } |
1136 | ||
68e19a44 BA |
1137 | // Is square x,y attacked by 'color' rooks ? |
1138 | isAttackedByRook(sq, color) { | |
1139 | return this.isAttackedBySlideNJump(sq, color, V.ROOK, V.steps[V.ROOK]); | |
1c9f093d BA |
1140 | } |
1141 | ||
68e19a44 BA |
1142 | // Is square x,y attacked by 'color' knights ? |
1143 | isAttackedByKnight(sq, color) { | |
6808d7a1 BA |
1144 | return this.isAttackedBySlideNJump( |
1145 | sq, | |
68e19a44 | 1146 | color, |
6808d7a1 BA |
1147 | V.KNIGHT, |
1148 | V.steps[V.KNIGHT], | |
1149 | "oneStep" | |
1150 | ); | |
1c9f093d BA |
1151 | } |
1152 | ||
68e19a44 BA |
1153 | // Is square x,y attacked by 'color' bishops ? |
1154 | isAttackedByBishop(sq, color) { | |
1155 | return this.isAttackedBySlideNJump(sq, color, V.BISHOP, V.steps[V.BISHOP]); | |
1c9f093d BA |
1156 | } |
1157 | ||
68e19a44 BA |
1158 | // Is square x,y attacked by 'color' queens ? |
1159 | isAttackedByQueen(sq, color) { | |
6808d7a1 BA |
1160 | return this.isAttackedBySlideNJump( |
1161 | sq, | |
68e19a44 | 1162 | color, |
6808d7a1 BA |
1163 | V.QUEEN, |
1164 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]) | |
1165 | ); | |
1c9f093d BA |
1166 | } |
1167 | ||
68e19a44 BA |
1168 | // Is square x,y attacked by 'color' king(s) ? |
1169 | isAttackedByKing(sq, color) { | |
6808d7a1 BA |
1170 | return this.isAttackedBySlideNJump( |
1171 | sq, | |
68e19a44 | 1172 | color, |
6808d7a1 BA |
1173 | V.KING, |
1174 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
1175 | "oneStep" | |
1176 | ); | |
1c9f093d BA |
1177 | } |
1178 | ||
1c9f093d | 1179 | // Is color under check after his move ? |
6808d7a1 | 1180 | underCheck(color) { |
1c58eb76 | 1181 | return this.isAttacked(this.kingPos[color], V.GetOppCol(color)); |
1c9f093d BA |
1182 | } |
1183 | ||
1184 | ///////////////// | |
1185 | // MOVES PLAYING | |
1186 | ||
1187 | // Apply a move on board | |
6808d7a1 BA |
1188 | static PlayOnBoard(board, move) { |
1189 | for (let psq of move.vanish) board[psq.x][psq.y] = V.EMPTY; | |
1190 | for (let psq of move.appear) board[psq.x][psq.y] = psq.c + psq.p; | |
1c9f093d BA |
1191 | } |
1192 | // Un-apply the played move | |
6808d7a1 BA |
1193 | static UndoOnBoard(board, move) { |
1194 | for (let psq of move.appear) board[psq.x][psq.y] = V.EMPTY; | |
1195 | for (let psq of move.vanish) board[psq.x][psq.y] = psq.c + psq.p; | |
1c9f093d BA |
1196 | } |
1197 | ||
3a2a7b5f BA |
1198 | prePlay() {} |
1199 | ||
1200 | play(move) { | |
1201 | // DEBUG: | |
1202 | // if (!this.states) this.states = []; | |
1c58eb76 | 1203 | // const stateFen = this.getFen() + JSON.stringify(this.kingPos); |
3a2a7b5f BA |
1204 | // this.states.push(stateFen); |
1205 | ||
1206 | this.prePlay(move); | |
2c5d7b20 BA |
1207 | // Save flags (for undo) |
1208 | if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags()); | |
3a2a7b5f BA |
1209 | if (V.HasEnpassant) this.epSquares.push(this.getEpSquare(move)); |
1210 | V.PlayOnBoard(this.board, move); | |
1211 | this.turn = V.GetOppCol(this.turn); | |
1212 | this.movesCount++; | |
1213 | this.postPlay(move); | |
1214 | } | |
1215 | ||
a9e1202b | 1216 | updateCastleFlags(move, piece, color) { |
4258b58c | 1217 | // TODO: check flags. If already off, no need to always re-evaluate |
a9e1202b | 1218 | const c = color || V.GetOppCol(this.turn); |
1c58eb76 BA |
1219 | const firstRank = (c == "w" ? V.size.x - 1 : 0); |
1220 | // Update castling flags if rooks are moved | |
c7550017 | 1221 | const oppCol = this.turn; |
1c58eb76 | 1222 | const oppFirstRank = V.size.x - 1 - firstRank; |
bb688df5 BA |
1223 | if (piece == V.KING && move.appear.length > 0) |
1224 | this.castleFlags[c] = [V.size.y, V.size.y]; | |
1225 | else if ( | |
1c58eb76 BA |
1226 | move.start.x == firstRank && //our rook moves? |
1227 | this.castleFlags[c].includes(move.start.y) | |
1228 | ) { | |
1229 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
1230 | this.castleFlags[c][flagIdx] = V.size.y; | |
305ede7e BA |
1231 | } |
1232 | // NOTE: not "else if" because a rook could take an opposing rook | |
1233 | if ( | |
1c58eb76 BA |
1234 | move.end.x == oppFirstRank && //we took opponent rook? |
1235 | this.castleFlags[oppCol].includes(move.end.y) | |
1236 | ) { | |
1237 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
1238 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
1239 | } | |
1240 | } | |
1241 | ||
1c9f093d | 1242 | // After move is played, update variables + flags |
3a2a7b5f BA |
1243 | postPlay(move) { |
1244 | const c = V.GetOppCol(this.turn); | |
1c9f093d | 1245 | let piece = undefined; |
3a2a7b5f | 1246 | if (move.vanish.length >= 1) |
1c9f093d BA |
1247 | // Usual case, something is moved |
1248 | piece = move.vanish[0].p; | |
3a2a7b5f | 1249 | else |
1c9f093d BA |
1250 | // Crazyhouse-like variants |
1251 | piece = move.appear[0].p; | |
1c9f093d BA |
1252 | |
1253 | // Update king position + flags | |
964eda04 BA |
1254 | if (piece == V.KING && move.appear.length > 0) |
1255 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; | |
bb688df5 | 1256 | if (V.HasCastle) this.updateCastleFlags(move, piece); |
1c9f093d BA |
1257 | } |
1258 | ||
3a2a7b5f | 1259 | preUndo() {} |
1c9f093d | 1260 | |
6808d7a1 | 1261 | undo(move) { |
3a2a7b5f | 1262 | this.preUndo(move); |
6808d7a1 BA |
1263 | if (V.HasEnpassant) this.epSquares.pop(); |
1264 | if (V.HasFlags) this.disaggregateFlags(JSON.parse(move.flags)); | |
1c9f093d BA |
1265 | V.UndoOnBoard(this.board, move); |
1266 | this.turn = V.GetOppCol(this.turn); | |
1267 | this.movesCount--; | |
3a2a7b5f | 1268 | this.postUndo(move); |
1c9f093d BA |
1269 | |
1270 | // DEBUG: | |
1c58eb76 | 1271 | // const stateFen = this.getFen() + JSON.stringify(this.kingPos); |
9bd6786b BA |
1272 | // if (stateFen != this.states[this.states.length-1]) debugger; |
1273 | // this.states.pop(); | |
1c9f093d BA |
1274 | } |
1275 | ||
3a2a7b5f BA |
1276 | // After move is undo-ed *and flags resetted*, un-update other variables |
1277 | // TODO: more symmetry, by storing flags increment in move (?!) | |
1278 | postUndo(move) { | |
1279 | // (Potentially) Reset king position | |
1280 | const c = this.getColor(move.start.x, move.start.y); | |
1281 | if (this.getPiece(move.start.x, move.start.y) == V.KING) | |
1282 | this.kingPos[c] = [move.start.x, move.start.y]; | |
1283 | } | |
1284 | ||
1c9f093d BA |
1285 | /////////////// |
1286 | // END OF GAME | |
1287 | ||
1288 | // What is the score ? (Interesting if game is over) | |
6808d7a1 | 1289 | getCurrentScore() { |
bb688df5 | 1290 | if (this.atLeastOneMove()) return "*"; |
1c9f093d BA |
1291 | // Game over |
1292 | const color = this.turn; | |
1293 | // No valid move: stalemate or checkmate? | |
bb688df5 | 1294 | if (!this.underCheck(color)) return "1/2"; |
1c9f093d | 1295 | // OK, checkmate |
68e19a44 | 1296 | return (color == "w" ? "0-1" : "1-0"); |
1c9f093d BA |
1297 | } |
1298 | ||
1299 | /////////////// | |
1300 | // ENGINE PLAY | |
1301 | ||
1302 | // Pieces values | |
6808d7a1 | 1303 | static get VALUES() { |
1c9f093d | 1304 | return { |
6808d7a1 BA |
1305 | p: 1, |
1306 | r: 5, | |
1307 | n: 3, | |
1308 | b: 3, | |
1309 | q: 9, | |
1310 | k: 1000 | |
1c9f093d BA |
1311 | }; |
1312 | } | |
1313 | ||
1314 | // "Checkmate" (unreachable eval) | |
6808d7a1 BA |
1315 | static get INFINITY() { |
1316 | return 9999; | |
1317 | } | |
1c9f093d BA |
1318 | |
1319 | // At this value or above, the game is over | |
6808d7a1 BA |
1320 | static get THRESHOLD_MATE() { |
1321 | return V.INFINITY; | |
1322 | } | |
1c9f093d | 1323 | |
2c5d7b20 | 1324 | // Search depth: 1,2 for e.g. higher branching factor, 4 for smaller |
6808d7a1 BA |
1325 | static get SEARCH_DEPTH() { |
1326 | return 3; | |
1327 | } | |
1c9f093d | 1328 | |
af34341d BA |
1329 | // 'movesList' arg for some variants to provide a custom list |
1330 | getComputerMove(movesList) { | |
1c9f093d BA |
1331 | const maxeval = V.INFINITY; |
1332 | const color = this.turn; | |
af34341d | 1333 | let moves1 = movesList || this.getAllValidMoves(); |
c322a844 | 1334 | |
6808d7a1 | 1335 | if (moves1.length == 0) |
e71161fb | 1336 | // TODO: this situation should not happen |
41cb9b94 | 1337 | return null; |
1c9f093d | 1338 | |
b83a675a | 1339 | // Rank moves using a min-max at depth 2 (if search_depth >= 2!) |
6808d7a1 | 1340 | for (let i = 0; i < moves1.length; i++) { |
afbf3ca7 BA |
1341 | this.play(moves1[i]); |
1342 | const score1 = this.getCurrentScore(); | |
1343 | if (score1 != "*") { | |
1344 | moves1[i].eval = | |
1345 | score1 == "1/2" | |
1346 | ? 0 | |
1347 | : (score1 == "1-0" ? 1 : -1) * maxeval; | |
1348 | } | |
1349 | if (V.SEARCH_DEPTH == 1 || score1 != "*") { | |
1350 | if (!moves1[i].eval) moves1[i].eval = this.evalPosition(); | |
1351 | this.undo(moves1[i]); | |
b83a675a BA |
1352 | continue; |
1353 | } | |
1c9f093d | 1354 | // Initial self evaluation is very low: "I'm checkmated" |
6808d7a1 | 1355 | moves1[i].eval = (color == "w" ? -1 : 1) * maxeval; |
afbf3ca7 BA |
1356 | // Initial enemy evaluation is very low too, for him |
1357 | let eval2 = (color == "w" ? 1 : -1) * maxeval; | |
1358 | // Second half-move: | |
1359 | let moves2 = this.getAllValidMoves(); | |
1360 | for (let j = 0; j < moves2.length; j++) { | |
1361 | this.play(moves2[j]); | |
1362 | const score2 = this.getCurrentScore(); | |
1363 | let evalPos = 0; //1/2 value | |
1364 | switch (score2) { | |
1365 | case "*": | |
1366 | evalPos = this.evalPosition(); | |
1367 | break; | |
1368 | case "1-0": | |
1369 | evalPos = maxeval; | |
1370 | break; | |
1371 | case "0-1": | |
1372 | evalPos = -maxeval; | |
1373 | break; | |
1c9f093d | 1374 | } |
afbf3ca7 BA |
1375 | if ( |
1376 | (color == "w" && evalPos < eval2) || | |
1377 | (color == "b" && evalPos > eval2) | |
1378 | ) { | |
1379 | eval2 = evalPos; | |
1380 | } | |
1381 | this.undo(moves2[j]); | |
1382 | } | |
6808d7a1 BA |
1383 | if ( |
1384 | (color == "w" && eval2 > moves1[i].eval) || | |
1385 | (color == "b" && eval2 < moves1[i].eval) | |
1386 | ) { | |
1c9f093d BA |
1387 | moves1[i].eval = eval2; |
1388 | } | |
1389 | this.undo(moves1[i]); | |
1390 | } | |
6808d7a1 BA |
1391 | moves1.sort((a, b) => { |
1392 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); | |
1393 | }); | |
a97bdbda | 1394 | // console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; })); |
1c9f093d | 1395 | |
1c9f093d | 1396 | // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...) |
6808d7a1 | 1397 | if (V.SEARCH_DEPTH >= 3 && Math.abs(moves1[0].eval) < V.THRESHOLD_MATE) { |
6808d7a1 | 1398 | for (let i = 0; i < moves1.length; i++) { |
1c9f093d BA |
1399 | this.play(moves1[i]); |
1400 | // 0.1 * oldEval : heuristic to avoid some bad moves (not all...) | |
6808d7a1 BA |
1401 | moves1[i].eval = |
1402 | 0.1 * moves1[i].eval + | |
1403 | this.alphabeta(V.SEARCH_DEPTH - 1, -maxeval, maxeval); | |
1c9f093d BA |
1404 | this.undo(moves1[i]); |
1405 | } | |
6808d7a1 BA |
1406 | moves1.sort((a, b) => { |
1407 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); | |
1408 | }); | |
b83a675a | 1409 | } |
1c9f093d | 1410 | |
b83a675a | 1411 | let candidates = [0]; |
d54f6261 BA |
1412 | for (let i = 1; i < moves1.length && moves1[i].eval == moves1[0].eval; i++) |
1413 | candidates.push(i); | |
656b1878 | 1414 | return moves1[candidates[randInt(candidates.length)]]; |
1c9f093d BA |
1415 | } |
1416 | ||
6808d7a1 | 1417 | alphabeta(depth, alpha, beta) { |
1c9f093d BA |
1418 | const maxeval = V.INFINITY; |
1419 | const color = this.turn; | |
1420 | const score = this.getCurrentScore(); | |
1421 | if (score != "*") | |
6808d7a1 BA |
1422 | return score == "1/2" ? 0 : (score == "1-0" ? 1 : -1) * maxeval; |
1423 | if (depth == 0) return this.evalPosition(); | |
a97bdbda | 1424 | const moves = this.getAllValidMoves(); |
6808d7a1 BA |
1425 | let v = color == "w" ? -maxeval : maxeval; |
1426 | if (color == "w") { | |
1427 | for (let i = 0; i < moves.length; i++) { | |
1c9f093d | 1428 | this.play(moves[i]); |
6808d7a1 | 1429 | v = Math.max(v, this.alphabeta(depth - 1, alpha, beta)); |
1c9f093d BA |
1430 | this.undo(moves[i]); |
1431 | alpha = Math.max(alpha, v); | |
6808d7a1 | 1432 | if (alpha >= beta) break; //beta cutoff |
1c9f093d | 1433 | } |
1c5bfdf2 | 1434 | } |
6808d7a1 | 1435 | else { |
1c5bfdf2 | 1436 | // color=="b" |
6808d7a1 | 1437 | for (let i = 0; i < moves.length; i++) { |
1c9f093d | 1438 | this.play(moves[i]); |
6808d7a1 | 1439 | v = Math.min(v, this.alphabeta(depth - 1, alpha, beta)); |
1c9f093d BA |
1440 | this.undo(moves[i]); |
1441 | beta = Math.min(beta, v); | |
6808d7a1 | 1442 | if (alpha >= beta) break; //alpha cutoff |
1c9f093d BA |
1443 | } |
1444 | } | |
1445 | return v; | |
1446 | } | |
1447 | ||
6808d7a1 | 1448 | evalPosition() { |
1c9f093d BA |
1449 | let evaluation = 0; |
1450 | // Just count material for now | |
6808d7a1 BA |
1451 | for (let i = 0; i < V.size.x; i++) { |
1452 | for (let j = 0; j < V.size.y; j++) { | |
1453 | if (this.board[i][j] != V.EMPTY) { | |
1454 | const sign = this.getColor(i, j) == "w" ? 1 : -1; | |
1455 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; | |
1c9f093d BA |
1456 | } |
1457 | } | |
1458 | } | |
1459 | return evaluation; | |
1460 | } | |
1461 | ||
1462 | ///////////////////////// | |
1463 | // MOVES + GAME NOTATION | |
1464 | ///////////////////////// | |
1465 | ||
1466 | // Context: just before move is played, turn hasn't changed | |
1467 | // TODO: un-ambiguous notation (switch on piece type, check directions...) | |
6808d7a1 BA |
1468 | getNotation(move) { |
1469 | if (move.appear.length == 2 && move.appear[0].p == V.KING) | |
1cd3e362 | 1470 | // Castle |
6808d7a1 | 1471 | return move.end.y < move.start.y ? "0-0-0" : "0-0"; |
1c9f093d BA |
1472 | |
1473 | // Translate final square | |
1474 | const finalSquare = V.CoordsToSquare(move.end); | |
1475 | ||
1476 | const piece = this.getPiece(move.start.x, move.start.y); | |
6808d7a1 | 1477 | if (piece == V.PAWN) { |
1c9f093d BA |
1478 | // Pawn move |
1479 | let notation = ""; | |
6808d7a1 | 1480 | if (move.vanish.length > move.appear.length) { |
1c9f093d BA |
1481 | // Capture |
1482 | const startColumn = V.CoordToColumn(move.start.y); | |
1483 | notation = startColumn + "x" + finalSquare; | |
78d64531 | 1484 | } |
6808d7a1 BA |
1485 | else notation = finalSquare; |
1486 | if (move.appear.length > 0 && move.appear[0].p != V.PAWN) | |
78d64531 | 1487 | // Promotion |
1c9f093d BA |
1488 | notation += "=" + move.appear[0].p.toUpperCase(); |
1489 | return notation; | |
1490 | } | |
6808d7a1 BA |
1491 | // Piece movement |
1492 | return ( | |
1493 | piece.toUpperCase() + | |
1494 | (move.vanish.length > move.appear.length ? "x" : "") + | |
1495 | finalSquare | |
1496 | ); | |
1497 | } | |
2c5d7b20 BA |
1498 | |
1499 | static GetUnambiguousNotation(move) { | |
1500 | // Machine-readable format with all the informations about the move | |
1501 | return ( | |
1502 | (!!move.start && V.OnBoard(move.start.x, move.start.y) | |
1503 | ? V.CoordsToSquare(move.start) | |
1504 | : "-" | |
1505 | ) + "." + | |
1506 | (!!move.end && V.OnBoard(move.end.x, move.end.y) | |
1507 | ? V.CoordsToSquare(move.end) | |
1508 | : "-" | |
1509 | ) + " " + | |
1510 | (!!move.appear && move.appear.length > 0 | |
1511 | ? move.appear.map(a => | |
1512 | a.c + a.p + V.CoordsToSquare({ x: a.x, y: a.y })).join(".") | |
1513 | : "-" | |
1514 | ) + "/" + | |
1515 | (!!move.vanish && move.vanish.length > 0 | |
1516 | ? move.vanish.map(a => | |
1517 | a.c + a.p + V.CoordsToSquare({ x: a.x, y: a.y })).join(".") | |
1518 | : "-" | |
1519 | ) | |
1520 | ); | |
1521 | } | |
7e8a7ea1 | 1522 | |
6808d7a1 | 1523 | }; |