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'], | |
059f0aa2 BA |
32 | z: ['q', 'k'], |
33 | '_': ['k', 'k'] | |
173f11dc BA |
34 | }; |
35 | } | |
36 | ||
6cc34165 BA |
37 | static fen2board(f) { |
38 | // Underscore is character 95, in file w_ | |
39 | return f.charCodeAt() <= 95 ? "w" + f.toLowerCase() : "b" + f; | |
40 | } | |
41 | ||
173f11dc BA |
42 | static IsGoodPosition(position) { |
43 | if (position.length == 0) return false; | |
44 | const rows = position.split("/"); | |
45 | if (rows.length != V.size.x) return false; | |
059f0aa2 | 46 | let kingSymb = ['k', 'g', 'm', 'u', 'x', '_']; |
173f11dc BA |
47 | let kings = { 'k': 0, 'K': 0 }; |
48 | for (let row of rows) { | |
49 | let sumElts = 0; | |
50 | for (let i = 0; i < row.length; i++) { | |
51 | const lowR = row[i].toLowerCase | |
059f0aa2 | 52 | if (!!(row[i].toLowerCase().match(/[a-z_]/))) { |
173f11dc BA |
53 | sumElts++; |
54 | if (kingSymb.includes(row[i])) kings['k']++; | |
059f0aa2 BA |
55 | // Not "else if", if two kings dancing together |
56 | if (kingSymb.some(s => row[i] == s.toUpperCase())) kings['K']++; | |
173f11dc BA |
57 | } |
58 | else { | |
59 | const num = parseInt(row[i], 10); | |
60 | if (isNaN(num) || num <= 0) return false; | |
61 | sumElts += num; | |
62 | } | |
63 | } | |
64 | if (sumElts != V.size.y) return false; | |
65 | } | |
66 | // Both kings should be on board. Exactly one per color. | |
67 | if (Object.values(kings).some(v => v != 1)) return false; | |
68 | return true; | |
69 | } | |
70 | ||
71 | getPpath(b) { | |
72 | return "Pacosako/" + b; | |
73 | } | |
74 | ||
4a209313 | 75 | getPPpath(m) { |
173f11dc BA |
76 | if (ChessRules.PIECES.includes(m.appear[0].p)) return super.getPPpath(m); |
77 | // For an union, show only relevant piece: | |
78 | // The color must be deduced from the move: reaching final rank of who? | |
4a209313 BA |
79 | const color = (m.appear[0].x == 0 ? 'w' : 'b'); |
80 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); | |
81 | return "Pacosako/" + color + up[color]; | |
173f11dc BA |
82 | } |
83 | ||
84 | canTake([x1, y1], [x2, y2]) { | |
4a209313 BA |
85 | const p1 = this.board[x1][y1].charAt(1); |
86 | if (!(ChessRules.PIECES.includes(p1))) return false; | |
87 | const p2 = this.board[x2][y2].charAt(1); | |
88 | if (!(ChessRules.PIECES.includes(p2))) return true; | |
89 | const c1 = this.board[x1][y1].charAt(0); | |
90 | const c2 = this.board[x2][y2].charAt(0); | |
91 | return (c1 != c2); | |
173f11dc BA |
92 | } |
93 | ||
94 | canIplay(side, [x, y]) { | |
4a209313 BA |
95 | return ( |
96 | this.turn == side && | |
97 | ( | |
98 | !(ChessRules.PIECES.includes(this.board[x][y].charAt(1))) || | |
99 | this.board[x][y].charAt(0) == side | |
100 | ) | |
101 | ); | |
173f11dc BA |
102 | } |
103 | ||
104 | scanKings(fen) { | |
105 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; | |
106 | const fenRows = V.ParseFen(fen).position.split("/"); | |
107 | const startRow = { 'w': V.size.x - 1, 'b': 0 }; | |
059f0aa2 | 108 | const kingSymb = ['k', 'g', 'm', 'u', 'x', '_']; |
173f11dc BA |
109 | for (let i = 0; i < fenRows.length; i++) { |
110 | let k = 0; | |
111 | for (let j = 0; j < fenRows[i].length; j++) { | |
112 | const c = fenRows[i].charAt(j); | |
059f0aa2 BA |
113 | if (!!(c.toLowerCase().match(/[a-z_]/))) { |
114 | if (kingSymb.includes(c)) | |
115 | this.kingPos["b"] = [i, k]; | |
116 | // Not "else if", in case of two kings dancing together | |
117 | if (kingSymb.some(s => c == s.toUpperCase())) | |
118 | this.kingPos["w"] = [i, k]; | |
119 | } | |
173f11dc BA |
120 | else { |
121 | const num = parseInt(fenRows[i].charAt(j), 10); | |
122 | if (!isNaN(num)) k += num - 1; | |
123 | } | |
124 | k++; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | setOtherVariables(fen) { | |
130 | super.setOtherVariables(fen); | |
131 | // Stack of "last move" only for intermediate chaining | |
132 | this.lastMoveEnd = [null]; | |
4a209313 BA |
133 | // Local stack of non-capturing union moves: |
134 | this.umoves = []; | |
135 | const umove = V.ParseFen(fen).umove; | |
136 | if (umove == "-") this.umoves.push(null); | |
137 | else { | |
138 | this.umoves.push({ | |
139 | start: ChessRules.SquareToCoords(umove.substr(0, 2)), | |
140 | end: ChessRules.SquareToCoords(umove.substr(2)) | |
141 | }); | |
142 | } | |
143 | } | |
144 | ||
145 | static IsGoodFen(fen) { | |
146 | if (!ChessRules.IsGoodFen(fen)) return false; | |
147 | const fenParts = fen.split(" "); | |
148 | if (fenParts.length != 6) return false; | |
149 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) | |
150 | return false; | |
151 | return true; | |
152 | } | |
153 | ||
059f0aa2 BA |
154 | static IsGoodFlags(flags) { |
155 | // 4 for castle + 16 for pawns | |
156 | return !!flags.match(/^[a-z]{4,4}[01]{16,16}$/); | |
157 | } | |
158 | ||
159 | setFlags(fenflags) { | |
160 | super.setFlags(fenflags); //castleFlags | |
161 | this.pawnFlags = { | |
162 | w: [...Array(8)], //pawns can move 2 squares? | |
163 | b: [...Array(8)] | |
164 | }; | |
165 | const flags = fenflags.substr(4); //skip first 4 letters, for castle | |
166 | for (let c of ["w", "b"]) { | |
167 | for (let i = 0; i < 8; i++) | |
168 | this.pawnFlags[c][i] = flags.charAt((c == "w" ? 0 : 8) + i) == "1"; | |
169 | } | |
170 | } | |
171 | ||
172 | aggregateFlags() { | |
173 | return [this.castleFlags, this.pawnFlags]; | |
174 | } | |
175 | ||
176 | disaggregateFlags(flags) { | |
177 | this.castleFlags = flags[0]; | |
178 | this.pawnFlags = flags[1]; | |
179 | } | |
180 | ||
4a209313 BA |
181 | getUmove(move) { |
182 | if ( | |
183 | move.vanish.length == 1 && | |
184 | !(ChessRules.PIECES.includes(move.appear[0].p)) | |
185 | ) { | |
186 | // An union moving | |
187 | return { start: move.start, end: move.end }; | |
188 | } | |
189 | return null; | |
190 | } | |
191 | ||
192 | static ParseFen(fen) { | |
193 | const fenParts = fen.split(" "); | |
194 | return Object.assign( | |
195 | ChessRules.ParseFen(fen), | |
196 | { umove: fenParts[5] } | |
197 | ); | |
198 | } | |
199 | ||
200 | static GenRandInitFen(randomness) { | |
059f0aa2 BA |
201 | // Add 16 pawns flags + empty umove: |
202 | return ChessRules.GenRandInitFen(randomness) | |
203 | .slice(0, -2) + "1111111111111111 - -"; | |
204 | } | |
205 | ||
206 | getFlagsFen() { | |
207 | let fen = super.getFlagsFen(); | |
208 | // Add pawns flags | |
209 | for (let c of ["w", "b"]) | |
210 | for (let i = 0; i < 8; i++) fen += (this.pawnFlags[c][i] ? "1" : "0"); | |
211 | return fen; | |
4a209313 BA |
212 | } |
213 | ||
214 | getUmoveFen() { | |
215 | const L = this.umoves.length; | |
216 | return ( | |
217 | !this.umoves[L - 1] | |
218 | ? "-" | |
219 | : ChessRules.CoordsToSquare(this.umoves[L - 1].start) + | |
220 | ChessRules.CoordsToSquare(this.umoves[L - 1].end) | |
221 | ); | |
222 | } | |
223 | ||
224 | getFen() { | |
225 | return super.getFen() + " " + this.getUmoveFen(); | |
226 | } | |
227 | ||
228 | getFenForRepeat() { | |
229 | return super.getFenForRepeat() + "_" + this.getUmoveFen(); | |
173f11dc BA |
230 | } |
231 | ||
232 | getColor(i, j) { | |
233 | const p = this.board[i][j].charAt(1); | |
234 | if (ChessRules.PIECES.includes(p)) return super.getColor(i, j); | |
4a209313 | 235 | return this.turn; //union: I can use it, so it's "my" color... |
173f11dc BA |
236 | } |
237 | ||
238 | getPiece(i, j, color) { | |
239 | const p = this.board[i][j].charAt(1); | |
173f11dc BA |
240 | if (ChessRules.PIECES.includes(p)) return p; |
241 | const c = this.board[i][j].charAt(0); | |
242 | // NOTE: this.turn == HACK, but should work... | |
243 | color = color || this.turn; | |
244 | return V.UNIONS[p][c == color ? 0 : 1]; | |
245 | } | |
246 | ||
247 | getUnionPieces(color, code) { | |
248 | const pieces = V.UNIONS[code]; | |
249 | return { | |
250 | w: pieces[color == 'w' ? 0 : 1], | |
251 | b: pieces[color == 'b' ? 0 : 1] | |
252 | }; | |
253 | } | |
254 | ||
4a209313 | 255 | // p1: white piece, p2: black piece |
173f11dc BA |
256 | getUnionCode(p1, p2) { |
257 | let uIdx = ( | |
258 | Object.values(V.UNIONS).findIndex(v => v[0] == p1 && v[1] == p2) | |
259 | ); | |
260 | const c = (uIdx >= 0 ? 'w' : 'b'); | |
261 | if (uIdx == -1) { | |
262 | uIdx = ( | |
263 | Object.values(V.UNIONS).findIndex(v => v[0] == p2 && v[1] == p1) | |
264 | ); | |
265 | } | |
266 | return { c: c, p: Object.keys(V.UNIONS)[uIdx] }; | |
267 | } | |
268 | ||
269 | getBasicMove([sx, sy], [ex, ey], tr) { | |
4a209313 BA |
270 | const L = this.lastMoveEnd.length; |
271 | const lm = this.lastMoveEnd[L-1]; | |
272 | const piece = (!!lm ? lm.p : null); | |
273 | const initColor = (!!piece ? this.turn : this.board[sx][sy].charAt(0)); | |
274 | const initPiece = (piece || this.board[sx][sy].charAt(1)); | |
275 | const c = this.turn; | |
276 | const oppCol = V.GetOppCol(c); | |
277 | if (!!tr && !(ChessRules.PIECES.includes(initPiece))) { | |
278 | // Transformation computed without taking union into account | |
279 | const up = this.getUnionPieces(initColor, initPiece); | |
280 | let args = [tr.p, up[oppCol]]; | |
281 | if (c == 'b') args = args.reverse(); | |
282 | const cp = this.getUnionCode(args[0], args[1]); | |
283 | tr.c = cp.c; | |
284 | tr.p = cp.p; | |
285 | } | |
173f11dc BA |
286 | // 4 cases : moving |
287 | // - union to free square (other cases are illegal: return null) | |
288 | // - normal piece to free square, | |
289 | // to enemy normal piece, or | |
290 | // to union (releasing our piece) | |
291 | let mv = new Move({ | |
4a209313 BA |
292 | start: { x: sx, y: sy }, |
293 | end: { x: ex, y: ey }, | |
294 | vanish: [] | |
295 | }); | |
296 | if (!piece) { | |
297 | mv.vanish = [ | |
173f11dc BA |
298 | new PiPo({ |
299 | x: sx, | |
300 | y: sy, | |
301 | c: initColor, | |
302 | p: initPiece | |
303 | }) | |
4a209313 BA |
304 | ]; |
305 | } | |
173f11dc BA |
306 | // Treat free square cases first: |
307 | if (this.board[ex][ey] == V.EMPTY) { | |
308 | mv.appear = [ | |
309 | new PiPo({ | |
310 | x: ex, | |
311 | y: ey, | |
4a209313 | 312 | c: !!tr ? tr.c : initColor, |
173f11dc BA |
313 | p: !!tr ? tr.p : initPiece |
314 | }) | |
315 | ]; | |
316 | return mv; | |
317 | } | |
318 | // Now the two cases with union / release: | |
319 | const destColor = this.board[ex][ey].charAt(0); | |
320 | const destPiece = this.board[ex][ey].charAt(1); | |
321 | mv.vanish.push( | |
322 | new PiPo({ | |
323 | x: ex, | |
324 | y: ey, | |
325 | c: destColor, | |
326 | p: destPiece | |
327 | }) | |
328 | ); | |
329 | if (ChessRules.PIECES.includes(destPiece)) { | |
330 | // Normal piece: just create union | |
4a209313 BA |
331 | let args = [!!tr ? tr.p : initPiece, destPiece]; |
332 | if (c == 'b') args = args.reverse(); | |
333 | const cp = this.getUnionCode(args[0], args[1]); | |
173f11dc BA |
334 | mv.appear = [ |
335 | new PiPo({ | |
336 | x: ex, | |
337 | y: ey, | |
338 | c: cp.c, | |
339 | p: cp.p | |
340 | }) | |
341 | ]; | |
342 | return mv; | |
343 | } | |
344 | // Releasing a piece in an union: keep track of released piece | |
345 | const up = this.getUnionPieces(destColor, destPiece); | |
4a209313 BA |
346 | let args = [!!tr ? tr.p : initPiece, up[oppCol]]; |
347 | if (c == 'b') args = args.reverse(); | |
348 | const cp = this.getUnionCode(args[0], args[1]); | |
173f11dc BA |
349 | mv.appear = [ |
350 | new PiPo({ | |
351 | x: ex, | |
352 | y: ey, | |
353 | c: cp.c, | |
354 | p: cp.p | |
355 | }) | |
356 | ]; | |
357 | mv.released = up[c]; | |
358 | return mv; | |
359 | } | |
360 | ||
4a209313 | 361 | getPotentialMovesFrom([x, y]) { |
173f11dc BA |
362 | const L = this.lastMoveEnd.length; |
363 | const lm = this.lastMoveEnd[L-1]; | |
4a209313 BA |
364 | if (!!lm && (x != lm.x || y != lm.y)) return []; |
365 | const piece = (!!lm ? lm.p : this.getPiece(x, y)); | |
173f11dc | 366 | if (!!lm) { |
4a209313 | 367 | var saveSquare = this.board[x][y]; |
173f11dc BA |
368 | this.board[x][y] = this.turn + piece; |
369 | } | |
370 | let baseMoves = []; | |
059f0aa2 | 371 | const c = this.turn; |
173f11dc | 372 | switch (piece || this.getPiece(x, y)) { |
059f0aa2 BA |
373 | case V.PAWN: { |
374 | const firstRank = (c == 'w' ? 7 : 0); | |
375 | baseMoves = this.getPotentialPawnMoves([x, y]).filter(m => { | |
376 | // Skip forbidden 2-squares jumps (except from first rank) | |
377 | // Also skip unions capturing en-passant (not allowed). | |
378 | return ( | |
379 | ( | |
380 | m.start.x == firstRank || | |
381 | Math.abs(m.end.x - m.start.x) == 1 || | |
382 | this.pawnFlags[c][m.start.y] | |
383 | ) | |
384 | && | |
385 | ( | |
386 | this.board[x][y].charAt(1) == V.PAWN || | |
387 | m.start.y == m.end.y | |
388 | ) | |
389 | ); | |
390 | }); | |
173f11dc | 391 | break; |
059f0aa2 | 392 | } |
173f11dc BA |
393 | case V.ROOK: |
394 | baseMoves = this.getPotentialRookMoves([x, y]); | |
395 | break; | |
396 | case V.KNIGHT: | |
397 | baseMoves = this.getPotentialKnightMoves([x, y]); | |
398 | break; | |
399 | case V.BISHOP: | |
400 | baseMoves = this.getPotentialBishopMoves([x, y]); | |
401 | break; | |
402 | case V.QUEEN: | |
403 | baseMoves = this.getPotentialQueenMoves([x, y]); | |
404 | break; | |
405 | case V.KING: | |
406 | baseMoves = this.getPotentialKingMoves([x, y]); | |
407 | break; | |
408 | } | |
409 | // When a pawn in an union reaches final rank with a non-standard | |
410 | // promotion move: apply promotion anyway | |
411 | let moves = []; | |
4a209313 BA |
412 | const oppCol = V.GetOppCol(c); |
413 | const oppLastRank = (c == 'w' ? 7 : 0); | |
173f11dc | 414 | baseMoves.forEach(m => { |
4a209313 BA |
415 | if ( |
416 | m.end.x == oppLastRank && | |
417 | ['c', 'd', 'e', 'f', 'g'].includes(m.appear[0].p) | |
418 | ) { | |
419 | // Move to first rank, which is last rank for opponent's pawn. | |
420 | // => Show promotion choices. | |
421 | // Find our piece in union (not a pawn) | |
422 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); | |
423 | // merge with all potential promotion pieces + push (loop) | |
424 | for (let promotionPiece of [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]) { | |
425 | let args = [up[c], promotionPiece]; | |
426 | if (c == 'b') args = args.reverse(); | |
427 | const cp = this.getUnionCode(args[0], args[1]); | |
428 | let cpMove = JSON.parse(JSON.stringify(m)); | |
429 | cpMove.appear[0].c = cp.c; | |
430 | cpMove.appear[0].p = cp.p; | |
431 | moves.push(cpMove); | |
432 | } | |
433 | } | |
434 | else { | |
435 | if ( | |
436 | m.vanish.length > 0 && | |
437 | m.vanish[0].p == V.PAWN && | |
438 | m.start.y != m.end.y && | |
439 | this.board[m.end.x][m.end.y] == V.EMPTY | |
440 | ) { | |
441 | if (!!lm) | |
442 | // No en-passant inside a chaining | |
443 | return; | |
444 | // Fix en-passant capture: union type, maybe released piece too | |
445 | const cs = [m.end.x + (c == 'w' ? 1 : -1), m.end.y]; | |
446 | const color = this.board[cs[0]][cs[1]].charAt(0); | |
447 | const code = this.board[cs[0]][cs[1]].charAt(1); | |
448 | if (code == V.PAWN) { | |
449 | // Simple en-passant capture (usual: just form union) | |
450 | m.appear[0].c = 'w'; | |
451 | m.appear[0].p = 'a'; | |
452 | } | |
453 | else { | |
454 | // An union pawn + something juste moved two squares | |
455 | const up = this.getUnionPieces(color, code); | |
456 | m.released = up[c]; | |
457 | let args = [V.PAWN, up[oppCol]]; | |
458 | if (c == 'b') args = args.reverse(); | |
459 | const cp = this.getUnionCode(args[0], args[1]); | |
460 | m.appear[0].c = cp.c; | |
461 | m.appear[0].p = cp.p; | |
462 | } | |
463 | } | |
464 | moves.push(m); | |
465 | } | |
173f11dc | 466 | }); |
4a209313 | 467 | if (!!lm) this.board[x][y] = saveSquare; |
173f11dc BA |
468 | return moves; |
469 | } | |
470 | ||
4a209313 BA |
471 | getEpSquare(moveOrSquare) { |
472 | if (typeof moveOrSquare === "string") { | |
473 | const square = moveOrSquare; | |
474 | if (square == "-") return undefined; | |
475 | return V.SquareToCoords(square); | |
476 | } | |
477 | const move = moveOrSquare; | |
478 | const s = move.start, | |
479 | e = move.end; | |
480 | const oppCol = V.GetOppCol(this.turn); | |
481 | if ( | |
482 | s.y == e.y && | |
483 | Math.abs(s.x - e.x) == 2 && | |
484 | this.getPiece(s.x, s.y, oppCol) == V.PAWN | |
485 | ) { | |
486 | return { | |
487 | x: (s.x + e.x) / 2, | |
488 | y: s.y | |
489 | }; | |
490 | } | |
491 | return undefined; | |
492 | } | |
493 | ||
494 | // Does m2 un-do m1 ? (to disallow undoing union moves) | |
495 | oppositeMoves(m1, m2) { | |
496 | return ( | |
497 | !!m1 && | |
498 | !(ChessRules.PIECES.includes(m2.appear[0].p)) && | |
499 | m2.vanish.length == 1 && | |
059f0aa2 | 500 | !m2.released && |
4a209313 BA |
501 | m1.start.x == m2.end.x && |
502 | m1.end.x == m2.start.x && | |
503 | m1.start.y == m2.end.y && | |
504 | m1.end.y == m2.start.y | |
505 | ); | |
506 | } | |
507 | ||
059f0aa2 BA |
508 | getCastleMoves([x, y]) { |
509 | const c = this.getColor(x, y); | |
510 | const oppCol = V.GetOppCol(c); | |
511 | let moves = []; | |
512 | const finalSquares = [ [2, 3], [6, 5] ]; | |
513 | castlingCheck: for (let castleSide = 0; castleSide < 2; castleSide++) { | |
514 | if (this.castleFlags[c][castleSide] >= 8) continue; | |
515 | const rookPos = this.castleFlags[c][castleSide]; | |
0b4bca84 BA |
516 | const castlingColor = this.board[x][rookPos].charAt(0); |
517 | const castlingPiece = this.board[x][rookPos].charAt(1); | |
059f0aa2 BA |
518 | |
519 | // Nothing on the path of the king ? | |
520 | const finDist = finalSquares[castleSide][0] - y; | |
521 | let step = finDist / Math.max(1, Math.abs(finDist)); | |
522 | let i = y; | |
523 | let kingSquares = [y]; | |
524 | do { | |
525 | if ( | |
526 | ( | |
527 | this.board[x][i] != V.EMPTY && | |
528 | (this.getColor(x, i) != c || ![y, rookPos].includes(i)) | |
529 | ) | |
530 | ) { | |
531 | continue castlingCheck; | |
532 | } | |
533 | i += step; | |
534 | kingSquares.push(i); | |
535 | } while (i != finalSquares[castleSide][0]); | |
536 | // No checks on the path of the king ? | |
537 | if (this.isAttacked(kingSquares, oppCol)) continue castlingCheck; | |
538 | ||
539 | // Nothing on the path to the rook? | |
540 | step = castleSide == 0 ? -1 : 1; | |
541 | for (i = y + step; i != rookPos; i += step) { | |
542 | if (this.board[x][i] != V.EMPTY) continue castlingCheck; | |
543 | } | |
544 | ||
545 | // Nothing on final squares, except maybe king and castling rook? | |
546 | for (i = 0; i < 2; i++) { | |
547 | if ( | |
548 | finalSquares[castleSide][i] != rookPos && | |
549 | this.board[x][finalSquares[castleSide][i]] != V.EMPTY && | |
550 | ( | |
551 | finalSquares[castleSide][i] != y || | |
552 | this.getColor(x, finalSquares[castleSide][i]) != c | |
553 | ) | |
554 | ) { | |
555 | continue castlingCheck; | |
556 | } | |
557 | } | |
558 | ||
559 | moves.push( | |
560 | new Move({ | |
561 | appear: [ | |
562 | new PiPo({ | |
563 | x: x, | |
564 | y: finalSquares[castleSide][0], | |
565 | p: V.KING, | |
566 | c: c | |
567 | }), | |
568 | new PiPo({ | |
569 | x: x, | |
570 | y: finalSquares[castleSide][1], | |
0b4bca84 BA |
571 | p: castlingPiece, |
572 | c: castlingColor | |
059f0aa2 BA |
573 | }) |
574 | ], | |
575 | vanish: [ | |
576 | // King might be initially disguised (Titan...) | |
577 | new PiPo({ x: x, y: y, p: V.KING, c: c }), | |
0b4bca84 | 578 | new PiPo({ x: x, y: rookPos, p: castlingPiece, c: castlingColor }) |
059f0aa2 BA |
579 | ], |
580 | end: | |
581 | Math.abs(y - rookPos) <= 2 | |
582 | ? { x: x, y: rookPos } | |
583 | : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) } | |
584 | }) | |
585 | ); | |
586 | } | |
587 | ||
588 | return moves; | |
589 | } | |
590 | ||
0b4bca84 BA |
591 | getEnpassantCaptures(sq, shiftX) { |
592 | // HACK: when artificially change turn, do not consider en-passant | |
593 | const mcMod2 = this.movesCount % 2; | |
594 | const c = this.turn; | |
595 | if ((c == 'w' && mcMod2 == 1) || (c == 'b' && mcMod2 == 0)) return []; | |
596 | return super.getEnpassantCaptures(sq, shiftX); | |
597 | } | |
598 | ||
059f0aa2 BA |
599 | isAttacked_aux(files, color, positions, fromSquare, released) { |
600 | // "positions" = array of FENs to detect infinite loops. Example: | |
601 | // r1q1k2r/p1Pb1ppp/5n2/1f1p4/AV5P/P1eDP3/3B1PP1/R3K1NR, | |
602 | // Bxd2 Bxc3 Bxb4 Bxc3 Bxb4 etc. | |
603 | const newPos = { fen: super.getBaseFen(), piece: released }; | |
604 | if (positions.some(p => p.piece == newPos.piece && p.fen == newPos.fen)) | |
605 | // Start of an infinite loop: exit | |
606 | return false; | |
607 | positions.push(newPos); | |
608 | const rank = (color == 'w' ? 0 : 7); | |
609 | const moves = this.getPotentialMovesFrom(fromSquare); | |
610 | if (moves.some(m => m.end.x == rank && files.includes(m.end.y))) | |
611 | // Found an attack! | |
612 | return true; | |
613 | for (let m of moves) { | |
614 | if (!!m.released) { | |
615 | // Turn won't change since !!m.released | |
616 | this.play(m); | |
617 | const res = this.isAttacked_aux( | |
618 | files, color, positions, [m.end.x, m.end.y], m.released); | |
619 | this.undo(m); | |
620 | if (res) return true; | |
621 | } | |
622 | } | |
4a209313 BA |
623 | return false; |
624 | } | |
059f0aa2 BA |
625 | |
626 | isAttacked(files, color) { | |
627 | const rank = (color == 'w' ? 0 : 7); | |
628 | // Since it's too difficult (impossible?) to search from the square itself, | |
629 | // let's adopt a suboptimal but working strategy: find all attacks. | |
630 | const c = this.turn; | |
631 | // Artificial turn change is required: | |
632 | this.turn = color; | |
633 | let res = false; | |
634 | outerLoop: for (let i=0; i<8; i++) { | |
635 | for (let j=0; j<8; j++) { | |
636 | // Attacks must start from a normal piece, not an union. | |
637 | // Therefore, the following test is correct. | |
638 | if ( | |
639 | this.board[i][j] != V.EMPTY && | |
640 | // Do not start with king (irrelevant, and lead to infinite calls) | |
641 | [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN].includes( | |
642 | this.board[i][j].charAt(1)) && | |
643 | this.board[i][j].charAt(0) == color | |
644 | ) { | |
645 | // Try from here. | |
646 | const moves = this.getPotentialMovesFrom([i, j]); | |
647 | if (moves.some(m => m.end.x == rank && files.includes(m.end.y))) { | |
648 | res = true; | |
649 | break outerLoop; | |
650 | } | |
651 | for (let m of moves) { | |
652 | if (!!m.released) { | |
653 | // Turn won't change since !!m.released | |
654 | this.play(m); | |
655 | let positions = []; | |
656 | res = this.isAttacked_aux( | |
657 | files, color, positions, [m.end.x, m.end.y], m.released); | |
658 | this.undo(m); | |
659 | if (res) break outerLoop; | |
660 | } | |
661 | } | |
662 | } | |
663 | } | |
664 | } | |
665 | this.turn = c; | |
666 | return res; | |
667 | } | |
668 | ||
669 | // Do not consider checks, except to forbid castling | |
4a209313 BA |
670 | getCheckSquares() { |
671 | return []; | |
672 | } | |
673 | filterValid(moves) { | |
674 | if (moves.length == 0) return []; | |
675 | const L = this.umoves.length; //at least 1: init from FEN | |
676 | return moves.filter(m => !this.oppositeMoves(this.umoves[L - 1], m)); | |
677 | } | |
678 | ||
0b4bca84 | 679 | updateCastleFlags(move, piece) { |
4258b58c | 680 | const c = this.turn; |
0b4bca84 | 681 | const firstRank = (c == "w" ? 7 : 0); |
0b4bca84 BA |
682 | if (piece == V.KING && move.appear.length > 0) |
683 | this.castleFlags[c] = [V.size.y, V.size.y]; | |
684 | else if ( | |
685 | move.start.x == firstRank && | |
686 | this.castleFlags[c].includes(move.start.y) | |
687 | ) { | |
688 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
689 | this.castleFlags[c][flagIdx] = V.size.y; | |
690 | } | |
4258b58c BA |
691 | else if ( |
692 | move.end.x == firstRank && | |
693 | this.castleFlags[c].includes(move.end.y) | |
694 | ) { | |
695 | // Move to our rook: necessary normal piece, to union, releasing | |
696 | // (or the rook was moved before!) | |
697 | const flagIdx = (move.end.y == this.castleFlags[c][0] ? 0 : 1); | |
698 | this.castleFlags[c][flagIdx] = V.size.y; | |
699 | } | |
0b4bca84 BA |
700 | } |
701 | ||
4258b58c BA |
702 | prePlay(move) { |
703 | // Easier before move is played in this case (flags are saved) | |
704 | const c = this.turn; | |
705 | const L = this.lastMoveEnd.length; | |
706 | const lm = this.lastMoveEnd[L-1]; | |
707 | const piece = (!!lm ? lm.p : move.vanish[0].p); | |
4a209313 BA |
708 | if (piece == V.KING) |
709 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; | |
710 | this.updateCastleFlags(move, piece); | |
4258b58c BA |
711 | const pawnFirstRank = (c == 'w' ? 6 : 1); |
712 | if (move.start.x == pawnFirstRank) | |
713 | // This move (potentially) turns off a 2-squares pawn flag | |
714 | this.pawnFlags[c][move.start.y] = false; | |
715 | } | |
716 | ||
717 | play(move) { | |
718 | move.flags = JSON.stringify(this.aggregateFlags()); | |
719 | this.prePlay(move); | |
720 | this.epSquares.push(this.getEpSquare(move)); | |
721 | // Check if the move is the last of the turn: all cases except releases | |
722 | if (!move.released) { | |
723 | // No more union releases available | |
724 | this.turn = V.GetOppCol(this.turn); | |
725 | this.movesCount++; | |
726 | this.lastMoveEnd.push(null); | |
059f0aa2 | 727 | } |
4258b58c BA |
728 | else this.lastMoveEnd.push(Object.assign({ p: move.released }, move.end)); |
729 | V.PlayOnBoard(this.board, move); | |
730 | this.umoves.push(this.getUmove(move)); | |
4a209313 BA |
731 | } |
732 | ||
173f11dc BA |
733 | undo(move) { |
734 | this.epSquares.pop(); | |
c27fcf89 | 735 | this.disaggregateFlags(JSON.parse(move.flags)); |
173f11dc BA |
736 | V.UndoOnBoard(this.board, move); |
737 | this.lastMoveEnd.pop(); | |
4a209313 | 738 | if (!move.released) { |
173f11dc BA |
739 | this.turn = V.GetOppCol(this.turn); |
740 | this.movesCount--; | |
741 | } | |
4a209313 | 742 | this.umoves.pop(); |
3cf54395 | 743 | this.postUndo(move); |
173f11dc BA |
744 | } |
745 | ||
4a209313 BA |
746 | postUndo(move) { |
747 | if (this.getPiece(move.start.x, move.start.y) == V.KING) | |
748 | this.kingPos[this.turn] = [move.start.x, move.start.y]; | |
749 | } | |
750 | ||
173f11dc BA |
751 | getCurrentScore() { |
752 | // Check kings: if one is dancing, the side lost | |
4a209313 | 753 | // But, if both dancing, let's say it's a draw :-) |
173f11dc | 754 | const [kpW, kpB] = [this.kingPos['w'], this.kingPos['b']]; |
4a209313 BA |
755 | const atKingPlace = [ |
756 | this.board[kpW[0]][kpW[1]].charAt(1), | |
757 | this.board[kpB[0]][kpB[1]].charAt(1) | |
758 | ]; | |
759 | if (!atKingPlace.includes('k')) return "1/2"; | |
760 | if (atKingPlace[0] != 'k') return "0-1"; | |
761 | if (atKingPlace[1] != 'k') return "1-0"; | |
173f11dc BA |
762 | return "*"; |
763 | } | |
764 | ||
765 | getComputerMove() { | |
4a209313 BA |
766 | let initMoves = this.getAllValidMoves(); |
767 | if (initMoves.length == 0) return null; | |
768 | // Loop until valid move is found (no blocked pawn released...) | |
769 | while (true) { | |
770 | let moves = JSON.parse(JSON.stringify(initMoves)); | |
771 | let mvArray = []; | |
772 | let mv = null; | |
773 | // Just play random moves (for now at least. TODO?) | |
774 | while (moves.length > 0) { | |
775 | mv = moves[randInt(moves.length)]; | |
776 | mvArray.push(mv); | |
777 | this.play(mv); | |
778 | if (!!mv.released) | |
779 | // A piece was just released from an union | |
780 | moves = this.getPotentialMovesFrom([mv.end.x, mv.end.y]); | |
781 | else break; | |
782 | } | |
783 | for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); | |
784 | if (!mv.released) return (mvArray.length > 1 ? mvArray : mvArray[0]); | |
173f11dc | 785 | } |
173f11dc BA |
786 | } |
787 | ||
788 | // NOTE: evalPosition() is wrong, but unused since bot plays at random | |
789 | ||
790 | getNotation(move) { | |
4a209313 BA |
791 | if (move.appear.length == 2 && move.appear[0].p == V.KING) |
792 | return (move.end.y < move.start.y ? "0-0-0" : "0-0"); | |
793 | ||
794 | const c = this.turn; | |
795 | const L = this.lastMoveEnd.length; | |
796 | const lm = this.lastMoveEnd[L-1]; | |
797 | let piece = null; | |
798 | if (!lm && move.vanish.length == 0) | |
799 | // When importing a game, the info move.released is lost | |
800 | piece = move.appear[0].p; | |
801 | else piece = (!!lm ? lm.p : move.vanish[0].p); | |
802 | if (!(ChessRules.PIECES.includes(piece))) { | |
803 | // Decode (moving) union | |
804 | const up = this.getUnionPieces( | |
805 | move.vanish.length > 0 ? move.vanish[0].c : move.appear[0].c, piece); | |
806 | piece = up[c] | |
807 | } | |
808 | ||
809 | // Basic move notation: | |
810 | let notation = piece.toUpperCase(); | |
811 | if ( | |
812 | this.board[move.end.x][move.end.y] != V.EMPTY || | |
813 | (piece == V.PAWN && move.start.y != move.end.y) | |
814 | ) { | |
815 | notation += "x"; | |
816 | } | |
817 | const finalSquare = V.CoordsToSquare(move.end); | |
818 | notation += finalSquare; | |
819 | ||
820 | // Add potential promotion indications: | |
821 | const firstLastRank = (c == 'w' ? [7, 0] : [0, 7]); | |
822 | if (move.end.x == firstLastRank[1] && piece == V.PAWN) { | |
823 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); | |
824 | notation += "=" + up[c].toUpperCase(); | |
825 | } | |
826 | else if ( | |
827 | move.end.x == firstLastRank[0] && | |
828 | move.vanish.length > 0 && | |
829 | ['c', 'd', 'e', 'f', 'g'].includes(move.vanish[0].p) | |
830 | ) { | |
831 | // We promoted an opponent's pawn | |
832 | const oppCol = V.GetOppCol(c); | |
833 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); | |
834 | notation += "=" + up[oppCol].toUpperCase(); | |
835 | } | |
836 | ||
837 | return notation; | |
173f11dc BA |
838 | } |
839 | ||
840 | }; |