398a96cca55ed7316ac2ca05dbdd927f705a75b3
[vchess.git] / client / src / variants / Grand.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt } from "@/utils/alea";
4
5 // NOTE: initial setup differs from the original; see
6 // https://www.chessvariants.com/large.dir/freeling.html
7 export class GrandRules extends ChessRules {
8
9 static IsGoodFen(fen) {
10 if (!ChessRules.IsGoodFen(fen)) return false;
11 const fenParsed = V.ParseFen(fen);
12 // 5) Check captures
13 if (!fenParsed.captured || !fenParsed.captured.match(/^[0-9]{12,12}$/))
14 return false;
15 return true;
16 }
17
18 static IsGoodEnpassant(enpassant) {
19 if (enpassant != "-") return !!enpassant.match(/^([a-j][0-9]{1,2},?)+$/);
20 return true;
21 }
22
23 static ParseFen(fen) {
24 const fenParts = fen.split(" ");
25 return Object.assign(
26 ChessRules.ParseFen(fen),
27 { captured: fenParts[5] }
28 );
29 }
30
31 getPpath(b) {
32 return ([V.MARSHALL, V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b;
33 }
34
35 getFen() {
36 return super.getFen() + " " + this.getCapturedFen();
37 }
38
39 getFenForRepeat() {
40 return super.getFenForRepeat() + "_" + this.getCapturedFen();
41 }
42
43 getCapturedFen() {
44 let counts = [...Array(12).fill(0)];
45 let i = 0;
46 for (let j = 0; j < V.PIECES.length; j++) {
47 if ([V.KING, V.PAWN].includes(V.PIECES[j]))
48 // No king captured, and pawns don't promote in pawns
49 continue;
50 counts[i] = this.captured["w"][V.PIECES[j]];
51 counts[6 + i] = this.captured["b"][V.PIECES[j]];
52 i++;
53 }
54 return counts.join("");
55 }
56
57 setOtherVariables(fen) {
58 super.setOtherVariables(fen);
59 const captured =
60 V.ParseFen(fen).captured.split("").map(x => parseInt(x, 10));
61 // Initialize captured pieces' counts from FEN
62 this.captured = {
63 w: {
64 [V.ROOK]: captured[0],
65 [V.KNIGHT]: captured[1],
66 [V.BISHOP]: captured[2],
67 [V.QUEEN]: captured[3],
68 [V.MARSHALL]: captured[4],
69 [V.CARDINAL]: captured[5]
70 },
71 b: {
72 [V.ROOK]: captured[6],
73 [V.KNIGHT]: captured[7],
74 [V.BISHOP]: captured[8],
75 [V.QUEEN]: captured[9],
76 [V.MARSHALL]: captured[10],
77 [V.CARDINAL]: captured[11]
78 }
79 };
80 }
81
82 static get size() {
83 return { x: 10, y: 10 };
84 }
85
86 // Rook + knight:
87 static get MARSHALL() {
88 return "m";
89 }
90
91 // Bishop + knight
92 static get CARDINAL() {
93 return "c";
94 }
95
96 static get PIECES() {
97 return ChessRules.PIECES.concat([V.MARSHALL, V.CARDINAL]);
98 }
99
100 // There may be 2 enPassant squares (if pawn jump 3 squares)
101 getEnpassantFen() {
102 const L = this.epSquares.length;
103 if (!this.epSquares[L - 1]) return "-"; //no en-passant
104 let res = "";
105 this.epSquares[L - 1].forEach(sq => {
106 res += V.CoordsToSquare(sq) + ",";
107 });
108 return res.slice(0, -1); //remove last comma
109 }
110
111 // En-passant after 2-sq or 3-sq jumps
112 getEpSquare(moveOrSquare) {
113 if (!moveOrSquare) return undefined;
114 if (typeof moveOrSquare === "string") {
115 const square = moveOrSquare;
116 if (square == "-") return undefined;
117 let res = [];
118 square.split(",").forEach(sq => {
119 res.push(V.SquareToCoords(sq));
120 });
121 return res;
122 }
123 // Argument is a move:
124 const move = moveOrSquare;
125 const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x];
126 if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) {
127 const step = (ex - sx) / Math.abs(ex - sx);
128 let res = [
129 {
130 x: sx + step,
131 y: sy
132 }
133 ];
134 if (sx + 2 * step != ex) {
135 // 3-squares jump
136 res.push({
137 x: sx + 2 * step,
138 y: sy
139 });
140 }
141 return res;
142 }
143 return undefined; //default
144 }
145
146 getPotentialMovesFrom([x, y]) {
147 switch (this.getPiece(x, y)) {
148 case V.MARSHALL:
149 return this.getPotentialMarshallMoves([x, y]);
150 case V.CARDINAL:
151 return this.getPotentialCardinalMoves([x, y]);
152 default:
153 return super.getPotentialMovesFrom([x, y]);
154 }
155 }
156
157 // Special pawn rules: promotions to captured friendly pieces,
158 // optional on ranks 8-9 and mandatory on rank 10.
159 getPotentialPawnMoves([x, y]) {
160 const color = this.turn;
161 let moves = [];
162 const [sizeX, sizeY] = [V.size.x, V.size.y];
163 const shiftX = color == "w" ? -1 : 1;
164 const startRanks = color == "w" ? [sizeX - 2, sizeX - 3] : [1, 2];
165 const lastRanks =
166 color == "w" ? [0, 1, 2] : [sizeX - 1, sizeX - 2, sizeX - 3];
167 const promotionPieces = [
168 V.ROOK,
169 V.KNIGHT,
170 V.BISHOP,
171 V.QUEEN,
172 V.MARSHALL,
173 V.CARDINAL
174 ];
175
176 // Always x+shiftX >= 0 && x+shiftX < sizeX, because no pawns on last rank
177 let finalPieces = undefined;
178 if (lastRanks.includes(x + shiftX)) {
179 finalPieces = promotionPieces.filter(p => this.captured[color][p] > 0);
180 if (x + shiftX != lastRanks[0]) finalPieces.push(V.PAWN);
181 } else finalPieces = [V.PAWN];
182 if (this.board[x + shiftX][y] == V.EMPTY) {
183 // One square forward
184 for (let piece of finalPieces)
185 moves.push(
186 this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
187 );
188 if (startRanks.includes(x)) {
189 if (this.board[x + 2 * shiftX][y] == V.EMPTY) {
190 // Two squares jump
191 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
192 if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) {
193 // Three squares jump
194 moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
195 }
196 }
197 }
198 }
199 // Captures
200 for (let shiftY of [-1, 1]) {
201 if (
202 y + shiftY >= 0 &&
203 y + shiftY < sizeY &&
204 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
205 this.canTake([x, y], [x + shiftX, y + shiftY])
206 ) {
207 for (let piece of finalPieces) {
208 moves.push(
209 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
210 c: color,
211 p: piece
212 })
213 );
214 }
215 }
216 }
217
218 // En passant
219 const Lep = this.epSquares.length;
220 const epSquare = this.epSquares[Lep - 1];
221 if (!!epSquare) {
222 for (let epsq of epSquare) {
223 // TODO: some redundant checks
224 if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) {
225 let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]);
226 // WARNING: the captured pawn may be diagonally behind us,
227 // if it's a 3-squares jump and we take on 1st passing square
228 const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX;
229 enpassantMove.vanish.push({
230 x: px,
231 y: epsq.y,
232 p: "p",
233 c: this.getColor(px, epsq.y)
234 });
235 moves.push(enpassantMove);
236 }
237 }
238 }
239
240 return moves;
241 }
242
243 // TODO: different castle?
244
245 getPotentialMarshallMoves(sq) {
246 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat(
247 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep")
248 );
249 }
250
251 getPotentialCardinalMoves(sq) {
252 return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat(
253 this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep")
254 );
255 }
256
257 isAttacked(sq, color) {
258 return (
259 super.isAttacked(sq, color) ||
260 this.isAttackedByMarshall(sq, color) ||
261 this.isAttackedByCardinal(sq, color)
262 );
263 }
264
265 isAttackedByMarshall(sq, color) {
266 return (
267 this.isAttackedBySlideNJump(sq, color, V.MARSHALL, V.steps[V.ROOK]) ||
268 this.isAttackedBySlideNJump(
269 sq,
270 color,
271 V.MARSHALL,
272 V.steps[V.KNIGHT],
273 "oneStep"
274 )
275 );
276 }
277
278 isAttackedByCardinal(sq, color) {
279 return (
280 this.isAttackedBySlideNJump(sq, color, V.CARDINAL, V.steps[V.BISHOP]) ||
281 this.isAttackedBySlideNJump(
282 sq,
283 color,
284 V.CARDINAL,
285 V.steps[V.KNIGHT],
286 "oneStep"
287 )
288 );
289 }
290
291 postPlay(move) {
292 super.postPlay(move);
293 if (move.vanish.length == 2 && move.appear.length == 1)
294 // Capture: update this.captured
295 this.captured[move.vanish[1].c][move.vanish[1].p]++;
296 }
297
298 postUndo(move) {
299 super.postUndo(move);
300 if (move.vanish.length == 2 && move.appear.length == 1)
301 this.captured[move.vanish[1].c][move.vanish[1].p]--;
302 }
303
304 static get VALUES() {
305 return Object.assign(
306 { c: 5, m: 7 }, //experimental
307 ChessRules.VALUES
308 );
309 }
310
311 static get SEARCH_DEPTH() {
312 return 2;
313 }
314
315 static GenRandInitFen(randomness) {
316 if (randomness == 0) {
317 return (
318 "r8r/1nbqkmcbn1/pppppppppp/91/91/91/91/PPPPPPPPPP/1NBQKMCBN1/R8R " +
319 // No castling in the official initial setup
320 "w 0 zzzz - 00000000000000"
321 );
322 }
323
324 let pieces = { w: new Array(10), b: new Array(10) };
325 let flags = "";
326 // Shuffle pieces on first and last rank
327 for (let c of ["w", "b"]) {
328 if (c == 'b' && randomness == 1) {
329 pieces['b'] = pieces['w'];
330 flags += flags;
331 break;
332 }
333
334 let positions = ArrayFun.range(10);
335
336 // Get random squares for bishops
337 let randIndex = 2 * randInt(5);
338 let bishop1Pos = positions[randIndex];
339 // The second bishop must be on a square of different color
340 let randIndex_tmp = 2 * randInt(5) + 1;
341 let bishop2Pos = positions[randIndex_tmp];
342 // Remove chosen squares
343 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
344 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
345
346 // Get random squares for knights
347 randIndex = randInt(8);
348 let knight1Pos = positions[randIndex];
349 positions.splice(randIndex, 1);
350 randIndex = randInt(7);
351 let knight2Pos = positions[randIndex];
352 positions.splice(randIndex, 1);
353
354 // Get random square for queen
355 randIndex = randInt(6);
356 let queenPos = positions[randIndex];
357 positions.splice(randIndex, 1);
358
359 // ...random square for marshall
360 randIndex = randInt(5);
361 let marshallPos = positions[randIndex];
362 positions.splice(randIndex, 1);
363
364 // ...random square for cardinal
365 randIndex = randInt(4);
366 let cardinalPos = positions[randIndex];
367 positions.splice(randIndex, 1);
368
369 // Rooks and king positions are now fixed,
370 // because of the ordering rook-king-rook
371 let rook1Pos = positions[0];
372 let kingPos = positions[1];
373 let rook2Pos = positions[2];
374
375 // Finally put the shuffled pieces in the board array
376 pieces[c][rook1Pos] = "r";
377 pieces[c][knight1Pos] = "n";
378 pieces[c][bishop1Pos] = "b";
379 pieces[c][queenPos] = "q";
380 pieces[c][marshallPos] = "m";
381 pieces[c][cardinalPos] = "c";
382 pieces[c][kingPos] = "k";
383 pieces[c][bishop2Pos] = "b";
384 pieces[c][knight2Pos] = "n";
385 pieces[c][rook2Pos] = "r";
386 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
387 }
388 return (
389 pieces["b"].join("") +
390 "/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/" +
391 pieces["w"].join("").toUpperCase() +
392 " w 0 " + flags + " - 00000000000000"
393 );
394 }
395
396 };