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