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