Commit | Line | Data |
---|---|---|
68e19a44 | 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
0c3fe8a6 | 2 | |
af34341d | 3 | export class Checkered2Rules extends ChessRules { |
6808d7a1 | 4 | static board2fen(b) { |
dac39588 | 5 | const checkered_codes = { |
6808d7a1 BA |
6 | p: "s", |
7 | q: "t", | |
8 | r: "u", | |
9 | b: "c", | |
10 | n: "o" | |
dac39588 | 11 | }; |
6808d7a1 | 12 | if (b[0] == "c") return checkered_codes[b[1]]; |
dac39588 BA |
13 | return ChessRules.board2fen(b); |
14 | } | |
15 | ||
6808d7a1 | 16 | static fen2board(f) { |
dac39588 BA |
17 | // Tolerate upper-case versions of checkered pieces (why not?) |
18 | const checkered_pieces = { | |
6808d7a1 BA |
19 | s: "p", |
20 | S: "p", | |
21 | t: "q", | |
22 | T: "q", | |
23 | u: "r", | |
24 | U: "r", | |
25 | c: "b", | |
26 | C: "b", | |
27 | o: "n", | |
28 | O: "n" | |
dac39588 BA |
29 | }; |
30 | if (Object.keys(checkered_pieces).includes(f)) | |
6808d7a1 | 31 | return "c" + checkered_pieces[f]; |
dac39588 BA |
32 | return ChessRules.fen2board(f); |
33 | } | |
34 | ||
6808d7a1 BA |
35 | static get PIECES() { |
36 | return ChessRules.PIECES.concat(["s", "t", "u", "c", "o"]); | |
dac39588 | 37 | } |
7931e479 | 38 | |
241bf8f2 | 39 | getPpath(b) { |
d1be8046 | 40 | return (b[0] == "c" ? "Checkered/" : "") + b; |
241bf8f2 BA |
41 | } |
42 | ||
6808d7a1 | 43 | setOtherVariables(fen) { |
03cff0f7 BA |
44 | super.setOtherVariables(fen); |
45 | // Local stack of non-capturing checkered moves: | |
46 | this.cmoves = []; | |
3a2a7b5f | 47 | const cmove = V.ParseFen(fen).cmove; |
6808d7a1 BA |
48 | if (cmove == "-") this.cmoves.push(null); |
49 | else { | |
03cff0f7 | 50 | this.cmoves.push({ |
6808d7a1 BA |
51 | start: ChessRules.SquareToCoords(cmove.substr(0, 2)), |
52 | end: ChessRules.SquareToCoords(cmove.substr(2)) | |
03cff0f7 BA |
53 | }); |
54 | } | |
55 | } | |
56 | ||
6808d7a1 BA |
57 | static IsGoodFen(fen) { |
58 | if (!ChessRules.IsGoodFen(fen)) return false; | |
03cff0f7 | 59 | const fenParts = fen.split(" "); |
6808d7a1 | 60 | if (fenParts.length != 6) return false; |
03cff0f7 BA |
61 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) |
62 | return false; | |
63 | return true; | |
64 | } | |
65 | ||
6808d7a1 | 66 | static IsGoodFlags(flags) { |
dac39588 | 67 | // 4 for castle + 16 for pawns |
3a2a7b5f | 68 | return !!flags.match(/^[a-z]{4,4}[01]{16,16}$/); |
dac39588 BA |
69 | } |
70 | ||
6808d7a1 | 71 | setFlags(fenflags) { |
dac39588 | 72 | super.setFlags(fenflags); //castleFlags |
6808d7a1 | 73 | this.pawnFlags = { |
305ede7e BA |
74 | w: [...Array(8)], //pawns can move 2 squares? |
75 | b: [...Array(8)] | |
dac39588 | 76 | }; |
3a2a7b5f | 77 | const flags = fenflags.substr(4); //skip first 4 letters, for castle |
6808d7a1 BA |
78 | for (let c of ["w", "b"]) { |
79 | for (let i = 0; i < 8; i++) | |
80 | this.pawnFlags[c][i] = flags.charAt((c == "w" ? 0 : 8) + i) == "1"; | |
dac39588 BA |
81 | } |
82 | } | |
83 | ||
6808d7a1 | 84 | aggregateFlags() { |
dac39588 BA |
85 | return [this.castleFlags, this.pawnFlags]; |
86 | } | |
87 | ||
6808d7a1 | 88 | disaggregateFlags(flags) { |
dac39588 BA |
89 | this.castleFlags = flags[0]; |
90 | this.pawnFlags = flags[1]; | |
91 | } | |
1d184b4c | 92 | |
e727fe31 | 93 | getEpSquare(moveOrSquare) { |
bbf66837 | 94 | if (typeof moveOrSquare !== "object" || moveOrSquare.appear[0].c != 'c') |
e727fe31 BA |
95 | return super.getEpSquare(moveOrSquare); |
96 | // Checkered move: no en-passant | |
97 | return undefined; | |
98 | } | |
99 | ||
6808d7a1 BA |
100 | getCmove(move) { |
101 | if (move.appear[0].c == "c" && move.vanish.length == 1) | |
102 | return { start: move.start, end: move.end }; | |
03cff0f7 BA |
103 | return null; |
104 | } | |
105 | ||
6808d7a1 BA |
106 | canTake([x1, y1], [x2, y2]) { |
107 | const color1 = this.getColor(x1, y1); | |
108 | const color2 = this.getColor(x2, y2); | |
dac39588 | 109 | // Checkered aren't captured |
6808d7a1 BA |
110 | return ( |
111 | color1 != color2 && | |
112 | color2 != "c" && | |
113 | (color1 != "c" || color2 != this.turn) | |
114 | ); | |
dac39588 BA |
115 | } |
116 | ||
117 | // Post-processing: apply "checkerization" of standard moves | |
6808d7a1 BA |
118 | getPotentialMovesFrom([x, y]) { |
119 | let standardMoves = super.getPotentialMovesFrom([x, y]); | |
dac39588 | 120 | const lastRank = this.turn == "w" ? 0 : 7; |
32f6285e | 121 | // King is treated differently: it never turn checkered |
71ef1664 | 122 | if (this.getPiece(x, y) == V.KING) return standardMoves; |
dac39588 BA |
123 | let moves = []; |
124 | standardMoves.forEach(m => { | |
6808d7a1 BA |
125 | if (m.vanish[0].p == V.PAWN) { |
126 | if ( | |
127 | Math.abs(m.end.x - m.start.x) == 2 && | |
128 | !this.pawnFlags[this.turn][m.start.y] | |
32f6285e | 129 | ) { |
dac39588 | 130 | return; //skip forbidden 2-squares jumps |
32f6285e | 131 | } |
6808d7a1 BA |
132 | if ( |
133 | this.board[m.end.x][m.end.y] == V.EMPTY && | |
134 | m.vanish.length == 2 && | |
135 | this.getColor(m.start.x, m.start.y) == "c" | |
136 | ) { | |
dac39588 BA |
137 | return; //checkered pawns cannot take en-passant |
138 | } | |
139 | } | |
32f6285e BA |
140 | if (m.vanish.length == 1) |
141 | // No capture | |
142 | moves.push(m); | |
6808d7a1 | 143 | else { |
dac39588 BA |
144 | // A capture occured (m.vanish.length == 2) |
145 | m.appear[0].c = "c"; | |
146 | moves.push(m); | |
6808d7a1 | 147 | if ( |
32f6285e BA |
148 | // Avoid promotions (already treated): |
149 | m.appear[0].p != m.vanish[1].p && | |
6808d7a1 BA |
150 | (m.vanish[0].p != V.PAWN || m.end.x != lastRank) |
151 | ) { | |
dac39588 BA |
152 | // Add transformation into captured piece |
153 | let m2 = JSON.parse(JSON.stringify(m)); | |
154 | m2.appear[0].p = m.vanish[1].p; | |
155 | moves.push(m2); | |
156 | } | |
157 | } | |
158 | }); | |
159 | return moves; | |
160 | } | |
161 | ||
e727fe31 | 162 | getPotentialPawnMoves([x, y]) { |
32f6285e BA |
163 | let moves = super.getPotentialPawnMoves([x, y]); |
164 | // Post-process: set right color for checkered moves | |
af34341d | 165 | if (this.getColor(x, y) == 'c') { |
32f6285e BA |
166 | moves.forEach(m => { |
167 | m.appear[0].c = 'c'; //may be done twice if capture | |
168 | m.vanish[0].c = 'c'; | |
e727fe31 | 169 | }); |
af34341d | 170 | } |
68e19a44 BA |
171 | return moves; |
172 | } | |
173 | ||
6808d7a1 BA |
174 | canIplay(side, [x, y]) { |
175 | return side == this.turn && [side, "c"].includes(this.getColor(x, y)); | |
dac39588 BA |
176 | } |
177 | ||
178 | // Does m2 un-do m1 ? (to disallow undoing checkered moves) | |
6808d7a1 BA |
179 | oppositeMoves(m1, m2) { |
180 | return ( | |
61656127 | 181 | !!m1 && |
6808d7a1 BA |
182 | m2.appear[0].c == "c" && |
183 | m2.appear.length == 1 && | |
184 | m2.vanish.length == 1 && | |
185 | m1.start.x == m2.end.x && | |
186 | m1.end.x == m2.start.x && | |
187 | m1.start.y == m2.end.y && | |
188 | m1.end.y == m2.start.y | |
189 | ); | |
dac39588 BA |
190 | } |
191 | ||
6808d7a1 BA |
192 | filterValid(moves) { |
193 | if (moves.length == 0) return []; | |
dac39588 | 194 | const color = this.turn; |
241bf8f2 | 195 | const L = this.cmoves.length; //at least 1: init from FEN |
dac39588 | 196 | return moves.filter(m => { |
6808d7a1 | 197 | if (this.oppositeMoves(this.cmoves[L - 1], m)) return false; |
dac39588 BA |
198 | this.play(m); |
199 | const res = !this.underCheck(color); | |
200 | this.undo(m); | |
201 | return res; | |
202 | }); | |
203 | } | |
204 | ||
d1be8046 BA |
205 | getAllValidMoves() { |
206 | const oppCol = V.GetOppCol(this.turn); | |
207 | let potentialMoves = []; | |
208 | for (let i = 0; i < V.size.x; i++) { | |
209 | for (let j = 0; j < V.size.y; j++) { | |
af34341d | 210 | // NOTE: just testing == color isn't enough because of checkered pieces |
d1be8046 BA |
211 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
212 | Array.prototype.push.apply( | |
213 | potentialMoves, | |
214 | this.getPotentialMovesFrom([i, j]) | |
215 | ); | |
216 | } | |
217 | } | |
218 | } | |
219 | return this.filterValid(potentialMoves); | |
220 | } | |
221 | ||
222 | atLeastOneMove() { | |
223 | const oppCol = V.GetOppCol(this.turn); | |
224 | for (let i = 0; i < V.size.x; i++) { | |
225 | for (let j = 0; j < V.size.y; j++) { | |
b83a675a | 226 | // NOTE: just testing == color isn't enough because of checkered pieces |
d1be8046 BA |
227 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
228 | const moves = this.getPotentialMovesFrom([i, j]); | |
229 | if (moves.length > 0) { | |
230 | for (let k = 0; k < moves.length; k++) { | |
231 | if (this.filterValid([moves[k]]).length > 0) return true; | |
232 | } | |
233 | } | |
234 | } | |
235 | } | |
236 | } | |
237 | return false; | |
238 | } | |
239 | ||
68e19a44 BA |
240 | // colors: array, generally 'w' and 'c' or 'b' and 'c' |
241 | isAttacked(sq, colors) { | |
32f6285e | 242 | if (!Array.isArray(colors)) colors = [colors]; |
68e19a44 BA |
243 | return ( |
244 | this.isAttackedByPawn(sq, colors) || | |
245 | this.isAttackedByRook(sq, colors) || | |
246 | this.isAttackedByKnight(sq, colors) || | |
247 | this.isAttackedByBishop(sq, colors) || | |
248 | this.isAttackedByQueen(sq, colors) || | |
249 | this.isAttackedByKing(sq, colors) | |
250 | ); | |
251 | } | |
252 | ||
6808d7a1 BA |
253 | isAttackedByPawn([x, y], colors) { |
254 | for (let c of colors) { | |
68e19a44 | 255 | const color = (c == "c" ? this.turn : c); |
6808d7a1 BA |
256 | let pawnShift = color == "w" ? 1 : -1; |
257 | if (x + pawnShift >= 0 && x + pawnShift < 8) { | |
258 | for (let i of [-1, 1]) { | |
259 | if ( | |
260 | y + i >= 0 && | |
261 | y + i < 8 && | |
262 | this.getPiece(x + pawnShift, y + i) == V.PAWN && | |
263 | this.getColor(x + pawnShift, y + i) == c | |
264 | ) { | |
dac39588 BA |
265 | return true; |
266 | } | |
267 | } | |
268 | } | |
269 | } | |
270 | return false; | |
271 | } | |
272 | ||
68e19a44 BA |
273 | isAttackedBySlideNJump([x, y], colors, piece, steps, oneStep) { |
274 | for (let step of steps) { | |
275 | let rx = x + step[0], | |
276 | ry = y + step[1]; | |
277 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { | |
278 | rx += step[0]; | |
279 | ry += step[1]; | |
280 | } | |
281 | if ( | |
282 | V.OnBoard(rx, ry) && | |
283 | this.getPiece(rx, ry) === piece && | |
284 | colors.includes(this.getColor(rx, ry)) | |
285 | ) { | |
286 | return true; | |
287 | } | |
288 | } | |
289 | return false; | |
290 | } | |
291 | ||
292 | isAttackedByRook(sq, colors) { | |
293 | return this.isAttackedBySlideNJump(sq, colors, V.ROOK, V.steps[V.ROOK]); | |
294 | } | |
295 | ||
296 | isAttackedByKnight(sq, colors) { | |
297 | return this.isAttackedBySlideNJump( | |
298 | sq, | |
299 | colors, | |
300 | V.KNIGHT, | |
301 | V.steps[V.KNIGHT], | |
302 | "oneStep" | |
303 | ); | |
304 | } | |
305 | ||
306 | isAttackedByBishop(sq, colors) { | |
2c5d7b20 BA |
307 | return this.isAttackedBySlideNJump( |
308 | sq, colors, V.BISHOP, V.steps[V.BISHOP]); | |
68e19a44 BA |
309 | } |
310 | ||
311 | isAttackedByQueen(sq, colors) { | |
312 | return this.isAttackedBySlideNJump( | |
313 | sq, | |
314 | colors, | |
315 | V.QUEEN, | |
316 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]) | |
317 | ); | |
318 | } | |
319 | ||
320 | isAttackedByKing(sq, colors) { | |
321 | return this.isAttackedBySlideNJump( | |
322 | sq, | |
323 | colors, | |
324 | V.KING, | |
325 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
326 | "oneStep" | |
327 | ); | |
328 | } | |
329 | ||
6808d7a1 BA |
330 | underCheck(color) { |
331 | return this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]); | |
dac39588 BA |
332 | } |
333 | ||
af34341d BA |
334 | getCheckSquares() { |
335 | const color = this.turn; | |
dac39588 BA |
336 | // Artifically change turn, for checkered pawns |
337 | this.turn = V.GetOppCol(color); | |
af34341d BA |
338 | const kingAttacked = |
339 | this.isAttacked( | |
340 | this.kingPos[color], | |
341 | [this.turn, 'c'] | |
342 | ); | |
dac39588 | 343 | let res = kingAttacked |
2c5d7b20 | 344 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] |
dac39588 BA |
345 | : []; |
346 | this.turn = color; | |
347 | return res; | |
348 | } | |
349 | ||
3a2a7b5f BA |
350 | postPlay(move) { |
351 | super.postPlay(move); | |
dac39588 | 352 | // Does this move turn off a 2-squares pawn flag? |
3a2a7b5f | 353 | if ([1, 6].includes(move.start.x) && move.vanish[0].p == V.PAWN) |
6808d7a1 | 354 | this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false; |
3a2a7b5f BA |
355 | this.cmoves.push(this.getCmove(move)); |
356 | } | |
357 | ||
358 | postUndo(move) { | |
359 | super.postUndo(move); | |
360 | this.cmoves.pop(); | |
dac39588 BA |
361 | } |
362 | ||
6808d7a1 | 363 | getCurrentScore() { |
bb688df5 | 364 | if (this.atLeastOneMove()) return "*"; |
0c3fe8a6 | 365 | const color = this.turn; |
dac39588 BA |
366 | // Artifically change turn, for checkered pawns |
367 | this.turn = V.GetOppCol(this.turn); | |
6808d7a1 BA |
368 | const res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]) |
369 | ? color == "w" | |
370 | ? "0-1" | |
371 | : "1-0" | |
dac39588 BA |
372 | : "1/2"; |
373 | this.turn = V.GetOppCol(this.turn); | |
374 | return res; | |
375 | } | |
376 | ||
6808d7a1 | 377 | evalPosition() { |
dac39588 | 378 | let evaluation = 0; |
d1be8046 | 379 | // Just count material for now, considering checkered neutral (...) |
6808d7a1 BA |
380 | for (let i = 0; i < V.size.x; i++) { |
381 | for (let j = 0; j < V.size.y; j++) { | |
382 | if (this.board[i][j] != V.EMPTY) { | |
383 | const sqColor = this.getColor(i, j); | |
d1be8046 BA |
384 | if (["w","b"].includes(sqColor)) { |
385 | const sign = sqColor == "w" ? 1 : -1; | |
386 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; | |
387 | } | |
dac39588 BA |
388 | } |
389 | } | |
390 | } | |
391 | return evaluation; | |
392 | } | |
393 | ||
7ba4a5bc | 394 | static GenRandInitFen(randomness) { |
3a2a7b5f | 395 | // Add 16 pawns flags + empty cmove: |
7ba4a5bc | 396 | return ChessRules.GenRandInitFen(randomness) |
3a2a7b5f | 397 | .slice(0, -2) + "1111111111111111 - -"; |
dac39588 | 398 | } |
1d184b4c | 399 | |
6808d7a1 | 400 | static ParseFen(fen) { |
6f2f9437 BA |
401 | return Object.assign( |
402 | ChessRules.ParseFen(fen), | |
403 | { cmove: fen.split(" ")[5] } | |
404 | ); | |
03cff0f7 BA |
405 | } |
406 | ||
af34341d | 407 | getCmoveFen() { |
03cff0f7 | 408 | const L = this.cmoves.length; |
af34341d | 409 | return ( |
305ede7e BA |
410 | !this.cmoves[L - 1] |
411 | ? "-" | |
412 | : ChessRules.CoordsToSquare(this.cmoves[L - 1].start) + | |
af34341d BA |
413 | ChessRules.CoordsToSquare(this.cmoves[L - 1].end) |
414 | ); | |
415 | } | |
416 | ||
417 | getFen() { | |
418 | return super.getFen() + " " + this.getCmoveFen(); | |
419 | } | |
420 | ||
421 | getFenForRepeat() { | |
422 | return super.getFenForRepeat() + "_" + this.getCmoveFen(); | |
03cff0f7 BA |
423 | } |
424 | ||
6808d7a1 | 425 | getFlagsFen() { |
dac39588 BA |
426 | let fen = super.getFlagsFen(); |
427 | // Add pawns flags | |
305ede7e BA |
428 | for (let c of ["w", "b"]) |
429 | for (let i = 0; i < 8; i++) fen += (this.pawnFlags[c][i] ? "1" : "0"); | |
dac39588 BA |
430 | return fen; |
431 | } | |
1d184b4c | 432 | |
b83a675a BA |
433 | static get SEARCH_DEPTH() { |
434 | return 2; | |
435 | } | |
436 | ||
6808d7a1 BA |
437 | getNotation(move) { |
438 | if (move.appear.length == 2) { | |
dac39588 | 439 | // Castle |
6808d7a1 BA |
440 | if (move.end.y < move.start.y) return "0-0-0"; |
441 | return "0-0"; | |
dac39588 BA |
442 | } |
443 | ||
dac39588 | 444 | const finalSquare = V.CoordsToSquare(move.end); |
dac39588 | 445 | const piece = this.getPiece(move.start.x, move.start.y); |
57d9b2c4 | 446 | let notation = ""; |
6808d7a1 | 447 | if (piece == V.PAWN) { |
dac39588 | 448 | // Pawn move |
6808d7a1 | 449 | if (move.vanish.length > 1) { |
dac39588 BA |
450 | // Capture |
451 | const startColumn = V.CoordToColumn(move.start.y); | |
57d9b2c4 BA |
452 | notation = startColumn + "x" + finalSquare; |
453 | } else notation = finalSquare; | |
454 | } else { | |
455 | // Piece movement | |
456 | notation = | |
457 | piece.toUpperCase() + | |
458 | (move.vanish.length > 1 ? "x" : "") + | |
459 | finalSquare; | |
dac39588 | 460 | } |
57d9b2c4 BA |
461 | if (move.appear[0].p != move.vanish[0].p) |
462 | notation += "=" + move.appear[0].p.toUpperCase(); | |
463 | return notation; | |
dac39588 | 464 | } |
6808d7a1 | 465 | }; |