Fix checkered variant
[vchess.git] / client / src / variants / Checkered.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2
3export const VariantRules = class CheckeredRules extends ChessRules
1d184b4c 4{
1d184b4c
BA
5 static getPpath(b)
6 {
7 return b[0]=='c' ? "Checkered/"+b : b;
8 }
2d7194bd 9
1d184b4c
BA
10 static board2fen(b)
11 {
12 const checkered_codes = {
13 'p': 's',
14 'q': 't',
15 'r': 'u',
16 'b': 'c',
17 'n': 'o',
18 };
19 if (b[0]=="c")
20 return checkered_codes[b[1]];
21 return ChessRules.board2fen(b);
22 }
2d7194bd 23
1d184b4c
BA
24 static fen2board(f)
25 {
7931e479 26 // Tolerate upper-case versions of checkered pieces (why not?)
1d184b4c
BA
27 const checkered_pieces = {
28 's': 'p',
7931e479 29 'S': 'p',
1d184b4c 30 't': 'q',
7931e479 31 'T': 'q',
1d184b4c 32 'u': 'r',
7931e479 33 'U': 'r',
1d184b4c 34 'c': 'b',
7931e479 35 'C': 'b',
1d184b4c 36 'o': 'n',
7931e479 37 'O': 'n',
1d184b4c
BA
38 };
39 if (Object.keys(checkered_pieces).includes(f))
40 return 'c'+checkered_pieces[f];
41 return ChessRules.fen2board(f);
42 }
43
2d7194bd
BA
44 static get PIECES()
45 {
7931e479
BA
46 return ChessRules.PIECES.concat(['s','t','u','c','o']);
47 }
48
03cff0f7
BA
49 setOtherVariables(fen)
50 {
51 super.setOtherVariables(fen);
52 // Local stack of non-capturing checkered moves:
53 this.cmoves = [];
54 const cmove = fen.split(" ")[5];
55 if (cmove == "-")
56 this.cmoves.push(null);
57 else
58 {
59 this.cmoves.push({
60 start: ChessRules.SquareToCoords(cmove.substr(0,2)),
61 end: ChessRules.SquareToCoords(cmove.substr(2)),
62 });
63 }
64 }
65
66 static IsGoodFen(fen)
67 {
68 if (!ChessRules.IsGoodFen(fen))
69 return false;
70 const fenParts = fen.split(" ");
71 if (fenParts.length != 6)
72 return false;
73 if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/))
74 return false;
75 return true;
76 }
77
7931e479
BA
78 static IsGoodFlags(flags)
79 {
80 // 4 for castle + 16 for pawns
81 return !!flags.match(/^[01]{20,20}$/);
82 }
83
c794dbb8 84 setFlags(fenflags)
1d184b4c 85 {
c794dbb8 86 super.setFlags(fenflags); //castleFlags
2526c041
BA
87 this.pawnFlags =
88 {
8d61fc4a
BA
89 "w": [...Array(8).fill(true)], //pawns can move 2 squares?
90 "b": [...Array(8).fill(true)],
2526c041 91 };
c794dbb8
BA
92 if (!fenflags)
93 return;
94 const flags = fenflags.substr(4); //skip first 4 digits, for castle
1d184b4c
BA
95 for (let c of ['w','b'])
96 {
97 for (let i=0; i<8; i++)
2526c041 98 this.pawnFlags[c][i] = (flags.charAt((c=='w'?0:8)+i) == '1');
1d184b4c 99 }
1d184b4c
BA
100 }
101
2d7194bd
BA
102 aggregateFlags()
103 {
2526c041 104 return [this.castleFlags, this.pawnFlags];
1d184b4c
BA
105 }
106
2d7194bd 107 disaggregateFlags(flags)
1d184b4c 108 {
2526c041
BA
109 this.castleFlags = flags[0];
110 this.pawnFlags = flags[1];
1d184b4c
BA
111 }
112
03cff0f7
BA
113 getCmove(move)
114 {
115 if (move.appear[0].c == 'c' && move.vanish.length == 1)
116 return {start: move.start, end: move.end};
117 return null;
118 }
119
2526c041 120 canTake([x1,y1], [x2,y2])
1d184b4c 121 {
2526c041
BA
122 const color1 = this.getColor(x1,y1);
123 const color2 = this.getColor(x2,y2);
124 // Checkered aren't captured
125 return color1 != color2 && color2 != 'c' && (color1 != 'c' || color2 != this.turn);
1d184b4c
BA
126 }
127
2526c041
BA
128 // Post-processing: apply "checkerization" of standard moves
129 getPotentialMovesFrom([x,y])
1d184b4c 130 {
2526c041 131 let standardMoves = super.getPotentialMovesFrom([x,y]);
aea1443e 132 const lastRank = this.turn == "w" ? 0 : 7;
0b7d99ec 133 if (this.getPiece(x,y) == V.KING)
2526c041 134 return standardMoves; //king has to be treated differently (for castles)
1d184b4c 135 let moves = [];
2526c041 136 standardMoves.forEach(m => {
0b7d99ec 137 if (m.vanish[0].p == V.PAWN)
1d184b4c 138 {
2316f8b8
BA
139 if (Math.abs(m.end.x-m.start.x)==2 && !this.pawnFlags[this.turn][m.start.y])
140 return; //skip forbidden 2-squares jumps
0b7d99ec
BA
141 if (this.board[m.end.x][m.end.y] == V.EMPTY && m.vanish.length==2
142 && this.getColor(m.start.x,m.start.y) == 'c')
2316f8b8
BA
143 {
144 return; //checkered pawns cannot take en-passant
145 }
1d184b4c 146 }
2526c041
BA
147 if (m.vanish.length == 1)
148 moves.push(m); //no capture
149 else
1d184b4c 150 {
2526c041
BA
151 // A capture occured (m.vanish.length == 2)
152 m.appear[0].c = "c";
aea1443e 153 moves.push(m);
68f5ccc8 154 if (m.appear[0].p != m.vanish[1].p //avoid promotions (already treated):
0b7d99ec 155 && (m.vanish[0].p != V.PAWN || m.end.x != lastRank))
1d184b4c 156 {
2526c041
BA
157 // Add transformation into captured piece
158 let m2 = JSON.parse(JSON.stringify(m));
aea1443e 159 m2.appear[0].p = m.vanish[1].p;
2526c041 160 moves.push(m2);
1d184b4c
BA
161 }
162 }
2526c041 163 });
1d184b4c
BA
164 return moves;
165 }
166
46302e64 167 canIplay(side, [x,y])
1d184b4c 168 {
7931e479 169 return (side == this.turn && [side,'c'].includes(this.getColor(x,y)));
1d184b4c
BA
170 }
171
172 // Does m2 un-do m1 ? (to disallow undoing checkered moves)
173 oppositeMoves(m1, m2)
174 {
03cff0f7
BA
175 return (!!m1 && m2.appear[0].c == 'c'
176 && m2.appear.length == 1 && m2.vanish.length == 1
1d184b4c 177 && m1.start.x == m2.end.x && m1.end.x == m2.start.x
03cff0f7 178 && m1.start.y == m2.end.y && m1.end.y == m2.start.y);
1d184b4c
BA
179 }
180
181 filterValid(moves)
182 {
183 if (moves.length == 0)
184 return [];
46302e64 185 const color = this.turn;
1d184b4c 186 return moves.filter(m => {
03cff0f7
BA
187 const L = this.cmoves.length; //at least 1: init from FEN
188 if (this.oppositeMoves(this.cmoves[L-1], m))
1d184b4c 189 return false;
f6dbe8e3
BA
190 this.play(m);
191 const res = !this.underCheck(color);
192 this.undo(m);
193 return res;
1d184b4c
BA
194 });
195 }
196
46302e64
BA
197 isAttackedByPawn([x,y], colors)
198 {
199 for (let c of colors)
200 {
201 const color = (c=="c" ? this.turn : c);
202 let pawnShift = (color=="w" ? 1 : -1);
203 if (x+pawnShift>=0 && x+pawnShift<8)
204 {
205 for (let i of [-1,1])
206 {
0b7d99ec 207 if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==V.PAWN
46302e64
BA
208 && this.getColor(x+pawnShift,y+i)==c)
209 {
210 return true;
211 }
212 }
213 }
214 }
215 return false;
216 }
1d184b4c 217
f6dbe8e3 218 underCheck(color)
1d184b4c 219 {
26b8e4f7 220 return this.isAttacked(this.kingPos[color], [V.GetOppCol(color),'c']);
1d184b4c
BA
221 }
222
f6dbe8e3 223 getCheckSquares(color)
bd6ff57c 224 {
2d7194bd 225 // Artifically change turn, for checkered pawns
26b8e4f7 226 this.turn = V.GetOppCol(color);
92342261 227 const kingAttacked = this.isAttacked(
26b8e4f7 228 this.kingPos[color], [V.GetOppCol(color),'c']);
bd6ff57c 229 let res = kingAttacked
2d7194bd
BA
230 ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
231 : [];
232 this.turn = color;
bd6ff57c
BA
233 return res;
234 }
235
1d184b4c
BA
236 updateVariables(move)
237 {
0b7d99ec
BA
238 super.updateVariables(move);
239 // Does this move turn off a 2-squares pawn flag?
1d184b4c 240 const secondRank = [1,6];
0b7d99ec 241 if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN)
2526c041 242 this.pawnFlags[move.start.x==6 ? "w" : "b"][move.start.y] = false;
1d184b4c
BA
243 }
244
0c3fe8a6 245 getCurrentScore()
1d184b4c 246 {
0c3fe8a6
BA
247 if (this.atLeastOneMove()) // game not over
248 return "*";
249
250 const color = this.turn;
7931e479 251 // Artifically change turn, for checkered pawns
26b8e4f7
BA
252 this.turn = V.GetOppCol(this.turn);
253 const res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color),'c'])
e2b216fe
BA
254 ? (color == "w" ? "0-1" : "1-0")
255 : "1/2";
26b8e4f7 256 this.turn = V.GetOppCol(this.turn);
e2b216fe 257 return res;
1d184b4c
BA
258 }
259
260 evalPosition()
261 {
1d184b4c
BA
262 let evaluation = 0;
263 //Just count material for now, considering checkered neutral (...)
0b7d99ec 264 for (let i=0; i<V.size.x; i++)
1d184b4c 265 {
0b7d99ec 266 for (let j=0; j<V.size.y; j++)
1d184b4c 267 {
0b7d99ec 268 if (this.board[i][j] != V.EMPTY)
1d184b4c
BA
269 {
270 const sqColor = this.getColor(i,j);
271 const sign = sqColor == "w" ? 1 : (sqColor=="b" ? -1 : 0);
0b7d99ec 272 evaluation += sign * V.VALUES[this.getPiece(i,j)];
1d184b4c
BA
273 }
274 }
275 }
276 return evaluation;
277 }
278
279 static GenRandInitFen()
280 {
c794dbb8 281 const randFen = ChessRules.GenRandInitFen();
03cff0f7
BA
282 // Add 16 pawns flags + empty cmove:
283 return randFen.replace(" w 0 1111", " w 0 11111111111111111111 -");
1d184b4c
BA
284 }
285
03cff0f7
BA
286 static ParseFen(fen)
287 {
288 const fenParsed = ChessRules.ParseFen(fen);
289 return Object.assign({},
290 ChessRules.ParseFen(fen),
291 {cmove: fen.split(" ")[5]});
292 }
293
294 getFen()
295 {
296 const L = this.cmoves.length;
297 const cmoveFen = (!this.cmoves[L-1]
298 ? "-"
299 : ChessRules.CoordsToSquare(this.cmoves[L-1].start)
300 + ChessRules.CoordsToSquare(this.cmoves[L-1].end));
301 return super.getFen() + " " + cmoveFen;
302 }
303
1d184b4c
BA
304 getFlagsFen()
305 {
2526c041 306 let fen = super.getFlagsFen();
1d184b4c
BA
307 // Add pawns flags
308 for (let c of ['w','b'])
309 {
310 for (let i=0; i<8; i++)
2526c041 311 fen += this.pawnFlags[c][i] ? '1' : '0';
1d184b4c
BA
312 }
313 return fen;
314 }
315
03cff0f7
BA
316 // TODO (design): this cmove update here or in (un)updateVariables ?
317 play(move)
318 {
319 this.cmoves.push( this.getCmove(move) );
320 super.play(move);
321 }
322
323 undo(move)
324 {
325 this.cmoves.pop();
326 super.undo(move);
327 }
328
1d184b4c
BA
329 getNotation(move)
330 {
331 if (move.appear.length == 2)
332 {
333 // Castle
334 if (move.end.y < move.start.y)
335 return "0-0-0";
336 else
337 return "0-0";
338 }
339
340 // Translate final square
2d7194bd 341 const finalSquare = V.CoordsToSquare(move.end);
1d184b4c 342
2d7194bd 343 const piece = this.getPiece(move.start.x, move.start.y);
0b7d99ec 344 if (piece == V.PAWN)
1d184b4c
BA
345 {
346 // Pawn move
347 let notation = "";
348 if (move.vanish.length > 1)
349 {
350 // Capture
26c1e3bd 351 const startColumn = V.CoordToColumn(move.start.y);
92342261
BA
352 notation = startColumn + "x" + finalSquare +
353 "=" + move.appear[0].p.toUpperCase();
1d184b4c
BA
354 }
355 else //no capture
098e8468 356 {
1d184b4c 357 notation = finalSquare;
098e8468
BA
358 if (move.appear.length > 0 && piece != move.appear[0].p) //promotion
359 notation += "=" + move.appear[0].p.toUpperCase();
360 }
1d184b4c
BA
361 return notation;
362 }
363
364 else
365 {
366 // Piece movement
367 return piece.toUpperCase() + (move.vanish.length > 1 ? "x" : "") + finalSquare
368 + (move.vanish.length > 1 ? "=" + move.appear[0].p.toUpperCase() : "");
369 }
370 }
371}