Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
6808d7a1 | 3 | export const VariantRules = class CheckeredRules 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 = []; | |
47 | const cmove = fen.split(" ")[5]; | |
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 BA |
67 | // 4 for castle + 16 for pawns |
68 | return !!flags.match(/^[01]{20,20}$/); | |
69 | } | |
70 | ||
6808d7a1 | 71 | setFlags(fenflags) { |
dac39588 | 72 | super.setFlags(fenflags); //castleFlags |
6808d7a1 BA |
73 | this.pawnFlags = { |
74 | w: [...Array(8).fill(true)], //pawns can move 2 squares? | |
75 | b: [...Array(8).fill(true)] | |
dac39588 | 76 | }; |
dac39588 | 77 | const flags = fenflags.substr(4); //skip first 4 digits, 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 | |
6808d7a1 BA |
93 | getCmove(move) { |
94 | if (move.appear[0].c == "c" && move.vanish.length == 1) | |
95 | return { start: move.start, end: move.end }; | |
03cff0f7 BA |
96 | return null; |
97 | } | |
98 | ||
6808d7a1 BA |
99 | canTake([x1, y1], [x2, y2]) { |
100 | const color1 = this.getColor(x1, y1); | |
101 | const color2 = this.getColor(x2, y2); | |
dac39588 | 102 | // Checkered aren't captured |
6808d7a1 BA |
103 | return ( |
104 | color1 != color2 && | |
105 | color2 != "c" && | |
106 | (color1 != "c" || color2 != this.turn) | |
107 | ); | |
dac39588 BA |
108 | } |
109 | ||
110 | // Post-processing: apply "checkerization" of standard moves | |
6808d7a1 BA |
111 | getPotentialMovesFrom([x, y]) { |
112 | let standardMoves = super.getPotentialMovesFrom([x, y]); | |
dac39588 | 113 | const lastRank = this.turn == "w" ? 0 : 7; |
71ef1664 BA |
114 | // King has to be treated differently (for castles) |
115 | if (this.getPiece(x, y) == V.KING) return standardMoves; | |
dac39588 BA |
116 | let moves = []; |
117 | standardMoves.forEach(m => { | |
6808d7a1 BA |
118 | if (m.vanish[0].p == V.PAWN) { |
119 | if ( | |
120 | Math.abs(m.end.x - m.start.x) == 2 && | |
121 | !this.pawnFlags[this.turn][m.start.y] | |
122 | ) | |
dac39588 | 123 | return; //skip forbidden 2-squares jumps |
6808d7a1 BA |
124 | if ( |
125 | this.board[m.end.x][m.end.y] == V.EMPTY && | |
126 | m.vanish.length == 2 && | |
127 | this.getColor(m.start.x, m.start.y) == "c" | |
128 | ) { | |
dac39588 BA |
129 | return; //checkered pawns cannot take en-passant |
130 | } | |
131 | } | |
6808d7a1 | 132 | if (m.vanish.length == 1) moves.push(m); |
241bf8f2 | 133 | // No capture |
6808d7a1 | 134 | else { |
dac39588 BA |
135 | // A capture occured (m.vanish.length == 2) |
136 | m.appear[0].c = "c"; | |
137 | moves.push(m); | |
6808d7a1 BA |
138 | if ( |
139 | m.appear[0].p != m.vanish[1].p && //avoid promotions (already treated): | |
140 | (m.vanish[0].p != V.PAWN || m.end.x != lastRank) | |
141 | ) { | |
dac39588 BA |
142 | // Add transformation into captured piece |
143 | let m2 = JSON.parse(JSON.stringify(m)); | |
144 | m2.appear[0].p = m.vanish[1].p; | |
145 | moves.push(m2); | |
146 | } | |
147 | } | |
148 | }); | |
149 | return moves; | |
150 | } | |
151 | ||
6808d7a1 BA |
152 | canIplay(side, [x, y]) { |
153 | return side == this.turn && [side, "c"].includes(this.getColor(x, y)); | |
dac39588 BA |
154 | } |
155 | ||
156 | // Does m2 un-do m1 ? (to disallow undoing checkered moves) | |
6808d7a1 BA |
157 | oppositeMoves(m1, m2) { |
158 | return ( | |
241bf8f2 | 159 | m1 && |
6808d7a1 BA |
160 | m2.appear[0].c == "c" && |
161 | m2.appear.length == 1 && | |
162 | m2.vanish.length == 1 && | |
163 | m1.start.x == m2.end.x && | |
164 | m1.end.x == m2.start.x && | |
165 | m1.start.y == m2.end.y && | |
166 | m1.end.y == m2.start.y | |
167 | ); | |
dac39588 BA |
168 | } |
169 | ||
6808d7a1 BA |
170 | filterValid(moves) { |
171 | if (moves.length == 0) return []; | |
dac39588 | 172 | const color = this.turn; |
241bf8f2 | 173 | const L = this.cmoves.length; //at least 1: init from FEN |
dac39588 | 174 | return moves.filter(m => { |
6808d7a1 | 175 | if (this.oppositeMoves(this.cmoves[L - 1], m)) return false; |
dac39588 BA |
176 | this.play(m); |
177 | const res = !this.underCheck(color); | |
178 | this.undo(m); | |
179 | return res; | |
180 | }); | |
181 | } | |
182 | ||
d1be8046 BA |
183 | getAllValidMoves() { |
184 | const oppCol = V.GetOppCol(this.turn); | |
185 | let potentialMoves = []; | |
186 | for (let i = 0; i < V.size.x; i++) { | |
187 | for (let j = 0; j < V.size.y; j++) { | |
188 | // NOTE: just testing == color isn't enough because of checkred pieces | |
189 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { | |
190 | Array.prototype.push.apply( | |
191 | potentialMoves, | |
192 | this.getPotentialMovesFrom([i, j]) | |
193 | ); | |
194 | } | |
195 | } | |
196 | } | |
197 | return this.filterValid(potentialMoves); | |
198 | } | |
199 | ||
200 | atLeastOneMove() { | |
201 | const oppCol = V.GetOppCol(this.turn); | |
202 | for (let i = 0; i < V.size.x; i++) { | |
203 | for (let j = 0; j < V.size.y; j++) { | |
204 | // NOTE: just testing == color isn't enough because of checkred pieces | |
205 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { | |
206 | const moves = this.getPotentialMovesFrom([i, j]); | |
207 | if (moves.length > 0) { | |
208 | for (let k = 0; k < moves.length; k++) { | |
209 | if (this.filterValid([moves[k]]).length > 0) return true; | |
210 | } | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | return false; | |
216 | } | |
217 | ||
6808d7a1 BA |
218 | isAttackedByPawn([x, y], colors) { |
219 | for (let c of colors) { | |
220 | const color = c == "c" ? this.turn : c; | |
221 | let pawnShift = color == "w" ? 1 : -1; | |
222 | if (x + pawnShift >= 0 && x + pawnShift < 8) { | |
223 | for (let i of [-1, 1]) { | |
224 | if ( | |
225 | y + i >= 0 && | |
226 | y + i < 8 && | |
227 | this.getPiece(x + pawnShift, y + i) == V.PAWN && | |
228 | this.getColor(x + pawnShift, y + i) == c | |
229 | ) { | |
dac39588 BA |
230 | return true; |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | return false; | |
236 | } | |
237 | ||
6808d7a1 BA |
238 | underCheck(color) { |
239 | return this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]); | |
dac39588 BA |
240 | } |
241 | ||
6808d7a1 | 242 | getCheckSquares(color) { |
dac39588 BA |
243 | // Artifically change turn, for checkered pawns |
244 | this.turn = V.GetOppCol(color); | |
6808d7a1 BA |
245 | const kingAttacked = this.isAttacked(this.kingPos[color], [ |
246 | V.GetOppCol(color), | |
247 | "c" | |
248 | ]); | |
dac39588 BA |
249 | let res = kingAttacked |
250 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! | |
251 | : []; | |
252 | this.turn = color; | |
253 | return res; | |
254 | } | |
255 | ||
6808d7a1 | 256 | updateVariables(move) { |
dac39588 BA |
257 | super.updateVariables(move); |
258 | // Does this move turn off a 2-squares pawn flag? | |
6808d7a1 | 259 | const secondRank = [1, 6]; |
dac39588 | 260 | if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN) |
6808d7a1 | 261 | this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false; |
dac39588 BA |
262 | } |
263 | ||
6808d7a1 BA |
264 | getCurrentScore() { |
265 | if (this.atLeastOneMove()) | |
266 | // game not over | |
0c3fe8a6 BA |
267 | return "*"; |
268 | ||
269 | const color = this.turn; | |
dac39588 BA |
270 | // Artifically change turn, for checkered pawns |
271 | this.turn = V.GetOppCol(this.turn); | |
6808d7a1 BA |
272 | const res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]) |
273 | ? color == "w" | |
274 | ? "0-1" | |
275 | : "1-0" | |
dac39588 BA |
276 | : "1/2"; |
277 | this.turn = V.GetOppCol(this.turn); | |
278 | return res; | |
279 | } | |
280 | ||
6808d7a1 | 281 | evalPosition() { |
dac39588 | 282 | let evaluation = 0; |
d1be8046 | 283 | // Just count material for now, considering checkered neutral (...) |
6808d7a1 BA |
284 | for (let i = 0; i < V.size.x; i++) { |
285 | for (let j = 0; j < V.size.y; j++) { | |
286 | if (this.board[i][j] != V.EMPTY) { | |
287 | const sqColor = this.getColor(i, j); | |
d1be8046 BA |
288 | if (["w","b"].includes(sqColor)) { |
289 | const sign = sqColor == "w" ? 1 : -1; | |
290 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; | |
291 | } | |
dac39588 BA |
292 | } |
293 | } | |
294 | } | |
295 | return evaluation; | |
296 | } | |
297 | ||
6808d7a1 | 298 | static GenRandInitFen() { |
dac39588 BA |
299 | const randFen = ChessRules.GenRandInitFen(); |
300 | // Add 16 pawns flags + empty cmove: | |
301 | return randFen.replace(" w 0 1111", " w 0 11111111111111111111 -"); | |
302 | } | |
1d184b4c | 303 | |
6808d7a1 BA |
304 | static ParseFen(fen) { |
305 | return Object.assign({}, ChessRules.ParseFen(fen), { | |
306 | cmove: fen.split(" ")[5] | |
307 | }); | |
03cff0f7 BA |
308 | } |
309 | ||
6808d7a1 | 310 | getFen() { |
03cff0f7 | 311 | const L = this.cmoves.length; |
6808d7a1 | 312 | const cmoveFen = !this.cmoves[L - 1] |
03cff0f7 | 313 | ? "-" |
6808d7a1 BA |
314 | : ChessRules.CoordsToSquare(this.cmoves[L - 1].start) + |
315 | ChessRules.CoordsToSquare(this.cmoves[L - 1].end); | |
03cff0f7 BA |
316 | return super.getFen() + " " + cmoveFen; |
317 | } | |
318 | ||
6808d7a1 | 319 | getFlagsFen() { |
dac39588 BA |
320 | let fen = super.getFlagsFen(); |
321 | // Add pawns flags | |
6808d7a1 BA |
322 | for (let c of ["w", "b"]) { |
323 | for (let i = 0; i < 8; i++) fen += this.pawnFlags[c][i] ? "1" : "0"; | |
dac39588 BA |
324 | } |
325 | return fen; | |
326 | } | |
1d184b4c | 327 | |
03cff0f7 | 328 | // TODO (design): this cmove update here or in (un)updateVariables ? |
6808d7a1 BA |
329 | play(move) { |
330 | this.cmoves.push(this.getCmove(move)); | |
03cff0f7 BA |
331 | super.play(move); |
332 | } | |
333 | ||
6808d7a1 | 334 | undo(move) { |
03cff0f7 BA |
335 | this.cmoves.pop(); |
336 | super.undo(move); | |
337 | } | |
338 | ||
6808d7a1 BA |
339 | getNotation(move) { |
340 | if (move.appear.length == 2) { | |
dac39588 | 341 | // Castle |
6808d7a1 BA |
342 | if (move.end.y < move.start.y) return "0-0-0"; |
343 | return "0-0"; | |
dac39588 BA |
344 | } |
345 | ||
346 | // Translate final square | |
347 | const finalSquare = V.CoordsToSquare(move.end); | |
348 | ||
349 | const piece = this.getPiece(move.start.x, move.start.y); | |
6808d7a1 | 350 | if (piece == V.PAWN) { |
dac39588 BA |
351 | // Pawn move |
352 | let notation = ""; | |
6808d7a1 | 353 | if (move.vanish.length > 1) { |
dac39588 BA |
354 | // Capture |
355 | const startColumn = V.CoordToColumn(move.start.y); | |
6808d7a1 BA |
356 | notation = |
357 | startColumn + | |
358 | "x" + | |
359 | finalSquare + | |
360 | "=" + | |
361 | move.appear[0].p.toUpperCase(); | |
362 | } //no capture | |
363 | else { | |
dac39588 | 364 | notation = finalSquare; |
6808d7a1 BA |
365 | if (move.appear.length > 0 && piece != move.appear[0].p) |
366 | //promotion | |
dac39588 BA |
367 | notation += "=" + move.appear[0].p.toUpperCase(); |
368 | } | |
369 | return notation; | |
370 | } | |
6808d7a1 BA |
371 | // Piece movement |
372 | return ( | |
373 | piece.toUpperCase() + | |
374 | (move.vanish.length > 1 ? "x" : "") + | |
375 | finalSquare + | |
376 | (move.vanish.length > 1 ? "=" + move.appear[0].p.toUpperCase() : "") | |
377 | ); | |
dac39588 | 378 | } |
6808d7a1 | 379 | }; |