Commit | Line | Data |
---|---|---|
173f11dc BA |
1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
2 | import { randInt } from "@/utils/alea"; | |
3 | ||
4 | export class PacosakoRules extends ChessRules { | |
5 | ||
6 | static get IMAGE_EXTENSION() { | |
7 | return ".png"; | |
8 | } | |
9 | ||
10 | // Unions (left = white if upperCase, black otherwise) | |
11 | static get UNIONS() { | |
12 | return { | |
13 | a: ['p', 'p'], | |
14 | c: ['p', 'r'], | |
15 | d: ['p', 'n'], | |
16 | e: ['p', 'b'], | |
17 | f: ['p', 'q'], | |
18 | g: ['p', 'k'], | |
19 | h: ['r', 'r'], | |
20 | i: ['r', 'n'], | |
21 | j: ['r', 'b'], | |
22 | l: ['r', 'q'], | |
23 | m: ['r', 'k'], | |
24 | o: ['n', 'n'], | |
25 | s: ['n', 'b'], | |
26 | t: ['n', 'q'], | |
27 | u: ['n', 'k'], | |
28 | v: ['b', 'b'], | |
29 | w: ['b', 'q'], | |
30 | x: ['b', 'k'], | |
31 | y: ['q', 'q'], | |
32 | z: ['q', 'k'] | |
33 | }; | |
34 | } | |
35 | ||
36 | static IsGoodPosition(position) { | |
37 | if (position.length == 0) return false; | |
38 | const rows = position.split("/"); | |
39 | if (rows.length != V.size.x) return false; | |
40 | let kingSymb = ['k', 'g', 'm', 'u', 'x']; | |
41 | let kings = { 'k': 0, 'K': 0 }; | |
42 | for (let row of rows) { | |
43 | let sumElts = 0; | |
44 | for (let i = 0; i < row.length; i++) { | |
45 | const lowR = row[i].toLowerCase | |
46 | if (!!(row[i].toLowerCase().match(/[a-z]/))) { | |
47 | sumElts++; | |
48 | if (kingSymb.includes(row[i])) kings['k']++; | |
49 | else if (kingSymb.some(s => row[i] == s.toUpperCase())) kings['K']++; | |
50 | } | |
51 | else { | |
52 | const num = parseInt(row[i], 10); | |
53 | if (isNaN(num) || num <= 0) return false; | |
54 | sumElts += num; | |
55 | } | |
56 | } | |
57 | if (sumElts != V.size.y) return false; | |
58 | } | |
59 | // Both kings should be on board. Exactly one per color. | |
60 | if (Object.values(kings).some(v => v != 1)) return false; | |
61 | return true; | |
62 | } | |
63 | ||
64 | getPpath(b) { | |
65 | return "Pacosako/" + b; | |
66 | } | |
67 | ||
4a209313 | 68 | getPPpath(m) { |
173f11dc BA |
69 | if (ChessRules.PIECES.includes(m.appear[0].p)) return super.getPPpath(m); |
70 | // For an union, show only relevant piece: | |
71 | // The color must be deduced from the move: reaching final rank of who? | |
4a209313 BA |
72 | const color = (m.appear[0].x == 0 ? 'w' : 'b'); |
73 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); | |
74 | return "Pacosako/" + color + up[color]; | |
173f11dc BA |
75 | } |
76 | ||
77 | canTake([x1, y1], [x2, y2]) { | |
4a209313 BA |
78 | const p1 = this.board[x1][y1].charAt(1); |
79 | if (!(ChessRules.PIECES.includes(p1))) return false; | |
80 | const p2 = this.board[x2][y2].charAt(1); | |
81 | if (!(ChessRules.PIECES.includes(p2))) return true; | |
82 | const c1 = this.board[x1][y1].charAt(0); | |
83 | const c2 = this.board[x2][y2].charAt(0); | |
84 | return (c1 != c2); | |
173f11dc BA |
85 | } |
86 | ||
87 | canIplay(side, [x, y]) { | |
4a209313 BA |
88 | return ( |
89 | this.turn == side && | |
90 | ( | |
91 | !(ChessRules.PIECES.includes(this.board[x][y].charAt(1))) || | |
92 | this.board[x][y].charAt(0) == side | |
93 | ) | |
94 | ); | |
173f11dc BA |
95 | } |
96 | ||
97 | scanKings(fen) { | |
98 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; | |
99 | const fenRows = V.ParseFen(fen).position.split("/"); | |
100 | const startRow = { 'w': V.size.x - 1, 'b': 0 }; | |
101 | const kingSymb = ['k', 'g', 'm', 'u', 'x']; | |
102 | for (let i = 0; i < fenRows.length; i++) { | |
103 | let k = 0; | |
104 | for (let j = 0; j < fenRows[i].length; j++) { | |
105 | const c = fenRows[i].charAt(j); | |
106 | if (kingSymb.includes(c)) | |
107 | this.kingPos["b"] = [i, k]; | |
108 | else if (kingSymb.some(s => c == s.toUpperCase())) | |
109 | this.kingPos["w"] = [i, k]; | |
110 | else { | |
111 | const num = parseInt(fenRows[i].charAt(j), 10); | |
112 | if (!isNaN(num)) k += num - 1; | |
113 | } | |
114 | k++; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | setOtherVariables(fen) { | |
120 | super.setOtherVariables(fen); | |
121 | // Stack of "last move" only for intermediate chaining | |
122 | this.lastMoveEnd = [null]; | |
4a209313 BA |
123 | // Local stack of non-capturing union moves: |
124 | this.umoves = []; | |
125 | const umove = V.ParseFen(fen).umove; | |
126 | if (umove == "-") this.umoves.push(null); | |
127 | else { | |
128 | this.umoves.push({ | |
129 | start: ChessRules.SquareToCoords(umove.substr(0, 2)), | |
130 | end: ChessRules.SquareToCoords(umove.substr(2)) | |
131 | }); | |
132 | } | |
133 | } | |
134 | ||
135 | static IsGoodFen(fen) { | |
136 | if (!ChessRules.IsGoodFen(fen)) return false; | |
137 | const fenParts = fen.split(" "); | |
138 | if (fenParts.length != 6) return false; | |
139 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) | |
140 | return false; | |
141 | return true; | |
142 | } | |
143 | ||
144 | getUmove(move) { | |
145 | if ( | |
146 | move.vanish.length == 1 && | |
147 | !(ChessRules.PIECES.includes(move.appear[0].p)) | |
148 | ) { | |
149 | // An union moving | |
150 | return { start: move.start, end: move.end }; | |
151 | } | |
152 | return null; | |
153 | } | |
154 | ||
155 | static ParseFen(fen) { | |
156 | const fenParts = fen.split(" "); | |
157 | return Object.assign( | |
158 | ChessRules.ParseFen(fen), | |
159 | { umove: fenParts[5] } | |
160 | ); | |
161 | } | |
162 | ||
163 | static GenRandInitFen(randomness) { | |
164 | // Add empty umove | |
165 | return ChessRules.GenRandInitFen(randomness) + " -"; | |
166 | } | |
167 | ||
168 | getUmoveFen() { | |
169 | const L = this.umoves.length; | |
170 | return ( | |
171 | !this.umoves[L - 1] | |
172 | ? "-" | |
173 | : ChessRules.CoordsToSquare(this.umoves[L - 1].start) + | |
174 | ChessRules.CoordsToSquare(this.umoves[L - 1].end) | |
175 | ); | |
176 | } | |
177 | ||
178 | getFen() { | |
179 | return super.getFen() + " " + this.getUmoveFen(); | |
180 | } | |
181 | ||
182 | getFenForRepeat() { | |
183 | return super.getFenForRepeat() + "_" + this.getUmoveFen(); | |
173f11dc BA |
184 | } |
185 | ||
186 | getColor(i, j) { | |
187 | const p = this.board[i][j].charAt(1); | |
188 | if (ChessRules.PIECES.includes(p)) return super.getColor(i, j); | |
4a209313 | 189 | return this.turn; //union: I can use it, so it's "my" color... |
173f11dc BA |
190 | } |
191 | ||
192 | getPiece(i, j, color) { | |
193 | const p = this.board[i][j].charAt(1); | |
173f11dc BA |
194 | if (ChessRules.PIECES.includes(p)) return p; |
195 | const c = this.board[i][j].charAt(0); | |
196 | // NOTE: this.turn == HACK, but should work... | |
197 | color = color || this.turn; | |
198 | return V.UNIONS[p][c == color ? 0 : 1]; | |
199 | } | |
200 | ||
201 | getUnionPieces(color, code) { | |
202 | const pieces = V.UNIONS[code]; | |
203 | return { | |
204 | w: pieces[color == 'w' ? 0 : 1], | |
205 | b: pieces[color == 'b' ? 0 : 1] | |
206 | }; | |
207 | } | |
208 | ||
4a209313 | 209 | // p1: white piece, p2: black piece |
173f11dc BA |
210 | getUnionCode(p1, p2) { |
211 | let uIdx = ( | |
212 | Object.values(V.UNIONS).findIndex(v => v[0] == p1 && v[1] == p2) | |
213 | ); | |
214 | const c = (uIdx >= 0 ? 'w' : 'b'); | |
215 | if (uIdx == -1) { | |
216 | uIdx = ( | |
217 | Object.values(V.UNIONS).findIndex(v => v[0] == p2 && v[1] == p1) | |
218 | ); | |
219 | } | |
220 | return { c: c, p: Object.keys(V.UNIONS)[uIdx] }; | |
221 | } | |
222 | ||
223 | getBasicMove([sx, sy], [ex, ey], tr) { | |
4a209313 BA |
224 | const L = this.lastMoveEnd.length; |
225 | const lm = this.lastMoveEnd[L-1]; | |
226 | const piece = (!!lm ? lm.p : null); | |
227 | const initColor = (!!piece ? this.turn : this.board[sx][sy].charAt(0)); | |
228 | const initPiece = (piece || this.board[sx][sy].charAt(1)); | |
229 | const c = this.turn; | |
230 | const oppCol = V.GetOppCol(c); | |
231 | if (!!tr && !(ChessRules.PIECES.includes(initPiece))) { | |
232 | // Transformation computed without taking union into account | |
233 | const up = this.getUnionPieces(initColor, initPiece); | |
234 | let args = [tr.p, up[oppCol]]; | |
235 | if (c == 'b') args = args.reverse(); | |
236 | const cp = this.getUnionCode(args[0], args[1]); | |
237 | tr.c = cp.c; | |
238 | tr.p = cp.p; | |
239 | } | |
173f11dc BA |
240 | // 4 cases : moving |
241 | // - union to free square (other cases are illegal: return null) | |
242 | // - normal piece to free square, | |
243 | // to enemy normal piece, or | |
244 | // to union (releasing our piece) | |
245 | let mv = new Move({ | |
4a209313 BA |
246 | start: { x: sx, y: sy }, |
247 | end: { x: ex, y: ey }, | |
248 | vanish: [] | |
249 | }); | |
250 | if (!piece) { | |
251 | mv.vanish = [ | |
173f11dc BA |
252 | new PiPo({ |
253 | x: sx, | |
254 | y: sy, | |
255 | c: initColor, | |
256 | p: initPiece | |
257 | }) | |
4a209313 BA |
258 | ]; |
259 | } | |
173f11dc BA |
260 | // Treat free square cases first: |
261 | if (this.board[ex][ey] == V.EMPTY) { | |
262 | mv.appear = [ | |
263 | new PiPo({ | |
264 | x: ex, | |
265 | y: ey, | |
4a209313 | 266 | c: !!tr ? tr.c : initColor, |
173f11dc BA |
267 | p: !!tr ? tr.p : initPiece |
268 | }) | |
269 | ]; | |
270 | return mv; | |
271 | } | |
272 | // Now the two cases with union / release: | |
273 | const destColor = this.board[ex][ey].charAt(0); | |
274 | const destPiece = this.board[ex][ey].charAt(1); | |
275 | mv.vanish.push( | |
276 | new PiPo({ | |
277 | x: ex, | |
278 | y: ey, | |
279 | c: destColor, | |
280 | p: destPiece | |
281 | }) | |
282 | ); | |
283 | if (ChessRules.PIECES.includes(destPiece)) { | |
284 | // Normal piece: just create union | |
4a209313 BA |
285 | let args = [!!tr ? tr.p : initPiece, destPiece]; |
286 | if (c == 'b') args = args.reverse(); | |
287 | const cp = this.getUnionCode(args[0], args[1]); | |
173f11dc BA |
288 | mv.appear = [ |
289 | new PiPo({ | |
290 | x: ex, | |
291 | y: ey, | |
292 | c: cp.c, | |
293 | p: cp.p | |
294 | }) | |
295 | ]; | |
296 | return mv; | |
297 | } | |
298 | // Releasing a piece in an union: keep track of released piece | |
299 | const up = this.getUnionPieces(destColor, destPiece); | |
4a209313 BA |
300 | let args = [!!tr ? tr.p : initPiece, up[oppCol]]; |
301 | if (c == 'b') args = args.reverse(); | |
302 | const cp = this.getUnionCode(args[0], args[1]); | |
173f11dc BA |
303 | mv.appear = [ |
304 | new PiPo({ | |
305 | x: ex, | |
306 | y: ey, | |
307 | c: cp.c, | |
308 | p: cp.p | |
309 | }) | |
310 | ]; | |
311 | mv.released = up[c]; | |
312 | return mv; | |
313 | } | |
314 | ||
4a209313 | 315 | getPotentialMovesFrom([x, y]) { |
173f11dc BA |
316 | const L = this.lastMoveEnd.length; |
317 | const lm = this.lastMoveEnd[L-1]; | |
4a209313 BA |
318 | if (!!lm && (x != lm.x || y != lm.y)) return []; |
319 | const piece = (!!lm ? lm.p : this.getPiece(x, y)); | |
173f11dc | 320 | if (!!lm) { |
4a209313 | 321 | var saveSquare = this.board[x][y]; |
173f11dc BA |
322 | this.board[x][y] = this.turn + piece; |
323 | } | |
324 | let baseMoves = []; | |
325 | switch (piece || this.getPiece(x, y)) { | |
326 | case V.PAWN: | |
327 | baseMoves = this.getPotentialPawnMoves([x, y]); | |
328 | break; | |
329 | case V.ROOK: | |
330 | baseMoves = this.getPotentialRookMoves([x, y]); | |
331 | break; | |
332 | case V.KNIGHT: | |
333 | baseMoves = this.getPotentialKnightMoves([x, y]); | |
334 | break; | |
335 | case V.BISHOP: | |
336 | baseMoves = this.getPotentialBishopMoves([x, y]); | |
337 | break; | |
338 | case V.QUEEN: | |
339 | baseMoves = this.getPotentialQueenMoves([x, y]); | |
340 | break; | |
341 | case V.KING: | |
342 | baseMoves = this.getPotentialKingMoves([x, y]); | |
343 | break; | |
344 | } | |
345 | // When a pawn in an union reaches final rank with a non-standard | |
346 | // promotion move: apply promotion anyway | |
347 | let moves = []; | |
4a209313 BA |
348 | const c = this.turn; |
349 | const oppCol = V.GetOppCol(c); | |
350 | const oppLastRank = (c == 'w' ? 7 : 0); | |
173f11dc | 351 | baseMoves.forEach(m => { |
4a209313 BA |
352 | if ( |
353 | m.end.x == oppLastRank && | |
354 | ['c', 'd', 'e', 'f', 'g'].includes(m.appear[0].p) | |
355 | ) { | |
356 | // Move to first rank, which is last rank for opponent's pawn. | |
357 | // => Show promotion choices. | |
358 | // Find our piece in union (not a pawn) | |
359 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); | |
360 | // merge with all potential promotion pieces + push (loop) | |
361 | for (let promotionPiece of [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]) { | |
362 | let args = [up[c], promotionPiece]; | |
363 | if (c == 'b') args = args.reverse(); | |
364 | const cp = this.getUnionCode(args[0], args[1]); | |
365 | let cpMove = JSON.parse(JSON.stringify(m)); | |
366 | cpMove.appear[0].c = cp.c; | |
367 | cpMove.appear[0].p = cp.p; | |
368 | moves.push(cpMove); | |
369 | } | |
370 | } | |
371 | else { | |
372 | if ( | |
373 | m.vanish.length > 0 && | |
374 | m.vanish[0].p == V.PAWN && | |
375 | m.start.y != m.end.y && | |
376 | this.board[m.end.x][m.end.y] == V.EMPTY | |
377 | ) { | |
378 | if (!!lm) | |
379 | // No en-passant inside a chaining | |
380 | return; | |
381 | // Fix en-passant capture: union type, maybe released piece too | |
382 | const cs = [m.end.x + (c == 'w' ? 1 : -1), m.end.y]; | |
383 | const color = this.board[cs[0]][cs[1]].charAt(0); | |
384 | const code = this.board[cs[0]][cs[1]].charAt(1); | |
385 | if (code == V.PAWN) { | |
386 | // Simple en-passant capture (usual: just form union) | |
387 | m.appear[0].c = 'w'; | |
388 | m.appear[0].p = 'a'; | |
389 | } | |
390 | else { | |
391 | // An union pawn + something juste moved two squares | |
392 | const up = this.getUnionPieces(color, code); | |
393 | m.released = up[c]; | |
394 | let args = [V.PAWN, up[oppCol]]; | |
395 | if (c == 'b') args = args.reverse(); | |
396 | const cp = this.getUnionCode(args[0], args[1]); | |
397 | m.appear[0].c = cp.c; | |
398 | m.appear[0].p = cp.p; | |
399 | } | |
400 | } | |
401 | moves.push(m); | |
402 | } | |
173f11dc | 403 | }); |
4a209313 | 404 | if (!!lm) this.board[x][y] = saveSquare; |
173f11dc BA |
405 | return moves; |
406 | } | |
407 | ||
4a209313 BA |
408 | getEpSquare(moveOrSquare) { |
409 | if (typeof moveOrSquare === "string") { | |
410 | const square = moveOrSquare; | |
411 | if (square == "-") return undefined; | |
412 | return V.SquareToCoords(square); | |
413 | } | |
414 | const move = moveOrSquare; | |
415 | const s = move.start, | |
416 | e = move.end; | |
417 | const oppCol = V.GetOppCol(this.turn); | |
418 | if ( | |
419 | s.y == e.y && | |
420 | Math.abs(s.x - e.x) == 2 && | |
421 | this.getPiece(s.x, s.y, oppCol) == V.PAWN | |
422 | ) { | |
423 | return { | |
424 | x: (s.x + e.x) / 2, | |
425 | y: s.y | |
426 | }; | |
427 | } | |
428 | return undefined; | |
429 | } | |
430 | ||
431 | // Does m2 un-do m1 ? (to disallow undoing union moves) | |
432 | oppositeMoves(m1, m2) { | |
433 | return ( | |
434 | !!m1 && | |
435 | !(ChessRules.PIECES.includes(m2.appear[0].p)) && | |
436 | m2.vanish.length == 1 && | |
437 | m1.start.x == m2.end.x && | |
438 | m1.end.x == m2.start.x && | |
439 | m1.start.y == m2.end.y && | |
440 | m1.end.y == m2.start.y | |
441 | ); | |
442 | } | |
443 | ||
444 | // Do not consider checks for now (TODO) | |
445 | underCheck() { | |
446 | return false; | |
447 | } | |
448 | getCheckSquares() { | |
449 | return []; | |
450 | } | |
451 | filterValid(moves) { | |
452 | if (moves.length == 0) return []; | |
453 | const L = this.umoves.length; //at least 1: init from FEN | |
454 | return moves.filter(m => !this.oppositeMoves(this.umoves[L - 1], m)); | |
455 | } | |
456 | ||
173f11dc BA |
457 | play(move) { |
458 | this.epSquares.push(this.getEpSquare(move)); | |
459 | // Check if the move is the last of the turn: all cases except releases | |
4a209313 | 460 | if (!move.released) { |
173f11dc BA |
461 | // No more union releases available |
462 | this.turn = V.GetOppCol(this.turn); | |
463 | this.movesCount++; | |
464 | this.lastMoveEnd.push(null); | |
465 | } | |
4a209313 | 466 | else this.lastMoveEnd.push(Object.assign({ p: move.released }, move.end)); |
173f11dc | 467 | V.PlayOnBoard(this.board, move); |
4a209313 | 468 | this.umoves.push(this.getUmove(move)); |
3cf54395 | 469 | this.postPlay(move); |
173f11dc BA |
470 | } |
471 | ||
4a209313 BA |
472 | postPlay(move) { |
473 | if (move.vanish.length == 0) | |
474 | // A piece released just moved. Cannot be the king. | |
475 | return; | |
476 | const c = move.vanish[0].c; | |
477 | const piece = move.vanish[0].p; | |
478 | if (piece == V.KING) | |
479 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; | |
480 | this.updateCastleFlags(move, piece); | |
481 | } | |
482 | ||
173f11dc BA |
483 | undo(move) { |
484 | this.epSquares.pop(); | |
485 | V.UndoOnBoard(this.board, move); | |
486 | this.lastMoveEnd.pop(); | |
4a209313 | 487 | if (!move.released) { |
173f11dc BA |
488 | this.turn = V.GetOppCol(this.turn); |
489 | this.movesCount--; | |
490 | } | |
4a209313 | 491 | this.umoves.pop(); |
3cf54395 | 492 | this.postUndo(move); |
173f11dc BA |
493 | } |
494 | ||
4a209313 BA |
495 | postUndo(move) { |
496 | if (this.getPiece(move.start.x, move.start.y) == V.KING) | |
497 | this.kingPos[this.turn] = [move.start.x, move.start.y]; | |
498 | } | |
499 | ||
173f11dc BA |
500 | getCurrentScore() { |
501 | // Check kings: if one is dancing, the side lost | |
4a209313 | 502 | // But, if both dancing, let's say it's a draw :-) |
173f11dc | 503 | const [kpW, kpB] = [this.kingPos['w'], this.kingPos['b']]; |
4a209313 BA |
504 | const atKingPlace = [ |
505 | this.board[kpW[0]][kpW[1]].charAt(1), | |
506 | this.board[kpB[0]][kpB[1]].charAt(1) | |
507 | ]; | |
508 | if (!atKingPlace.includes('k')) return "1/2"; | |
509 | if (atKingPlace[0] != 'k') return "0-1"; | |
510 | if (atKingPlace[1] != 'k') return "1-0"; | |
173f11dc BA |
511 | return "*"; |
512 | } | |
513 | ||
514 | getComputerMove() { | |
4a209313 BA |
515 | let initMoves = this.getAllValidMoves(); |
516 | if (initMoves.length == 0) return null; | |
517 | // Loop until valid move is found (no blocked pawn released...) | |
518 | while (true) { | |
519 | let moves = JSON.parse(JSON.stringify(initMoves)); | |
520 | let mvArray = []; | |
521 | let mv = null; | |
522 | // Just play random moves (for now at least. TODO?) | |
523 | while (moves.length > 0) { | |
524 | mv = moves[randInt(moves.length)]; | |
525 | mvArray.push(mv); | |
526 | this.play(mv); | |
527 | if (!!mv.released) | |
528 | // A piece was just released from an union | |
529 | moves = this.getPotentialMovesFrom([mv.end.x, mv.end.y]); | |
530 | else break; | |
531 | } | |
532 | for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); | |
533 | if (!mv.released) return (mvArray.length > 1 ? mvArray : mvArray[0]); | |
173f11dc | 534 | } |
173f11dc BA |
535 | } |
536 | ||
537 | // NOTE: evalPosition() is wrong, but unused since bot plays at random | |
538 | ||
539 | getNotation(move) { | |
4a209313 BA |
540 | if (move.appear.length == 2 && move.appear[0].p == V.KING) |
541 | return (move.end.y < move.start.y ? "0-0-0" : "0-0"); | |
542 | ||
543 | const c = this.turn; | |
544 | const L = this.lastMoveEnd.length; | |
545 | const lm = this.lastMoveEnd[L-1]; | |
546 | let piece = null; | |
547 | if (!lm && move.vanish.length == 0) | |
548 | // When importing a game, the info move.released is lost | |
549 | piece = move.appear[0].p; | |
550 | else piece = (!!lm ? lm.p : move.vanish[0].p); | |
551 | if (!(ChessRules.PIECES.includes(piece))) { | |
552 | // Decode (moving) union | |
553 | const up = this.getUnionPieces( | |
554 | move.vanish.length > 0 ? move.vanish[0].c : move.appear[0].c, piece); | |
555 | piece = up[c] | |
556 | } | |
557 | ||
558 | // Basic move notation: | |
559 | let notation = piece.toUpperCase(); | |
560 | if ( | |
561 | this.board[move.end.x][move.end.y] != V.EMPTY || | |
562 | (piece == V.PAWN && move.start.y != move.end.y) | |
563 | ) { | |
564 | notation += "x"; | |
565 | } | |
566 | const finalSquare = V.CoordsToSquare(move.end); | |
567 | notation += finalSquare; | |
568 | ||
569 | // Add potential promotion indications: | |
570 | const firstLastRank = (c == 'w' ? [7, 0] : [0, 7]); | |
571 | if (move.end.x == firstLastRank[1] && piece == V.PAWN) { | |
572 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); | |
573 | notation += "=" + up[c].toUpperCase(); | |
574 | } | |
575 | else if ( | |
576 | move.end.x == firstLastRank[0] && | |
577 | move.vanish.length > 0 && | |
578 | ['c', 'd', 'e', 'f', 'g'].includes(move.vanish[0].p) | |
579 | ) { | |
580 | // We promoted an opponent's pawn | |
581 | const oppCol = V.GetOppCol(c); | |
582 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); | |
583 | notation += "=" + up[oppCol].toUpperCase(); | |
584 | } | |
585 | ||
586 | return notation; | |
173f11dc BA |
587 | } |
588 | ||
589 | }; |