addf5dda9188080c46b45b3a2be66fba772a0b40
[vchess.git] / client / src / variants / Apocalypse.js
1 import { ChessRules } from "@/base_rules";
2 import { randInt } from "@/utils/alea";
3
4 export class ApocalypseRules extends ChessRules {
5 static get PawnSpecs() {
6 return Object.assign(
7 {},
8 ChessRules.PawnSpecs,
9 {
10 twoSquares: false,
11 promotions: [V.KNIGHT]
12 }
13 );
14 }
15
16 static get SomeHiddenMoves() {
17 return true;
18 }
19
20 static get HasCastle() {
21 return false;
22 }
23
24 static get HasEnpassant() {
25 return false;
26 }
27
28 static get CanAnalyze() {
29 return false;
30 }
31
32 static get ShowMoves() {
33 return "byrow";
34 }
35
36 getPPpath(m) {
37 // Show the piece taken, if any, and not multiple pawns:
38 if (m.vanish.length == 1) return "Apocalypse/empty";
39 return m.vanish[1].c + m.vanish[1].p;
40 }
41
42 static get PIECES() {
43 return [V.PAWN, V.KNIGHT];
44 }
45
46 static IsGoodPosition(position) {
47 if (position.length == 0) return false;
48 const rows = position.split("/");
49 if (rows.length != V.size.x) return false;
50 // At least one pawn per color
51 let pawns = { "p": 0, "P": 0 };
52 for (let row of rows) {
53 let sumElts = 0;
54 for (let i = 0; i < row.length; i++) {
55 if (['P','p'].includes(row[i])) pawns[row[i]]++;
56 if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
57 else {
58 const num = parseInt(row[i]);
59 if (isNaN(num)) return false;
60 sumElts += num;
61 }
62 }
63 if (sumElts != V.size.y) return false;
64 }
65 if (Object.values(pawns).some(v => v == 0))
66 return false;
67 return true;
68 }
69
70 static IsGoodFen(fen) {
71 if (!ChessRules.IsGoodFen(fen)) return false;
72 const fenParsed = V.ParseFen(fen);
73 // 4) Check whiteMove
74 if (
75 (
76 fenParsed.turn == "b" &&
77 // NOTE: do not check really JSON stringified move...
78 (!fenParsed.whiteMove || fenParsed.whiteMove == "-")
79 )
80 ||
81 (fenParsed.turn == "w" && fenParsed.whiteMove != "-")
82 ) {
83 return false;
84 }
85 return true;
86 }
87
88 static IsGoodFlags(flags) {
89 return !!flags.match(/^[0-2]{2,2}$/);
90 }
91
92 aggregateFlags() {
93 return this.penaltyFlags;
94 }
95
96 disaggregateFlags(flags) {
97 this.penaltyFlags = flags;
98 }
99
100 static ParseFen(fen) {
101 const fenParts = fen.split(" ");
102 return Object.assign(
103 ChessRules.ParseFen(fen),
104 { whiteMove: fenParts[4] }
105 );
106 }
107
108 static get size() {
109 return { x: 5, y: 5 };
110 }
111
112 static GenRandInitFen() {
113 return "npppn/p3p/5/P3P/NPPPN w 0 00 -";
114 }
115
116 getFen() {
117 return super.getFen() + " " + this.getWhitemoveFen();
118 }
119
120 getFenForRepeat() {
121 return super.getFenForRepeat() + "_" + this.getWhitemoveFen();
122 }
123
124 getFlagsFen() {
125 return (
126 this.penaltyFlags['w'].toString() + this.penaltyFlags['b'].toString()
127 );
128 }
129
130 setOtherVariables(fen) {
131 const parsedFen = V.ParseFen(fen);
132 this.setFlags(parsedFen.flags);
133 // Also init whiteMove
134 this.whiteMove =
135 parsedFen.whiteMove != "-"
136 ? JSON.parse(parsedFen.whiteMove)
137 : null;
138 }
139
140 setFlags(fenflags) {
141 this.penaltyFlags = {
142 'w': parseInt(fenflags[0]),
143 'b': parseInt(fenflags[1])
144 };
145 }
146
147 getWhitemoveFen() {
148 if (!this.whiteMove) return "-";
149 return JSON.stringify({
150 start: this.whiteMove.start,
151 end: this.whiteMove.end,
152 appear: this.whiteMove.appear,
153 vanish: this.whiteMove.vanish,
154 illegal: this.whiteMove.illegal
155 });
156 }
157
158 getSpeculations(moves, sq) {
159 let moveSet = {};
160 moves.forEach(m => {
161 const mHash = "m" + m.start.x + m.start.y + m.end.x + m.end.y;
162 moveSet[mHash] = true;
163 });
164 const color = this.turn;
165 this.turn = V.GetOppCol(color);
166 const oppMoves = super.getAllValidMoves();
167 this.turn = color;
168 // For each opponent's move, generate valid moves [from sq if same color]
169 let speculations = [];
170 oppMoves.forEach(m => {
171 V.PlayOnBoard(this.board, m);
172 const newValidMoves =
173 !!sq
174 ? (
175 this.getColor(sq[0], sq[1]) == color
176 ? super.getPotentialMovesFrom(sq)
177 : []
178 )
179 : super.getAllValidMoves();
180 newValidMoves.forEach(vm => {
181 const mHash = "m" + vm.start.x + vm.start.y + vm.end.x + vm.end.y;
182 if (!moveSet[mHash]) {
183 moveSet[mHash] = true;
184 vm.illegal = true; //potentially illegal!
185 speculations.push(vm);
186 }
187 });
188 V.UndoOnBoard(this.board, m);
189 });
190 return speculations;
191 }
192
193 getPossibleMovesFrom([x, y]) {
194 const possibleMoves = super.getPotentialMovesFrom([x, y])
195 // Augment potential moves with opponent's moves speculation:
196 return possibleMoves.concat(this.getSpeculations(possibleMoves, [x, y]));
197 }
198
199 getAllValidMoves() {
200 // Return possible moves + potentially valid moves
201 const validMoves = super.getAllValidMoves();
202 return validMoves.concat(this.getSpeculations(validMoves));
203 }
204
205 addPawnMoves([x1, y1], [x2, y2], moves) {
206 let finalPieces = [V.PAWN];
207 const color = this.turn;
208 const lastRank = (color == "w" ? 0 : V.size.x - 1);
209 if (x2 == lastRank) {
210 // If 0 or 1 horsemen, promote in knight
211 let knightCounter = 0;
212 let emptySquares = [];
213 for (let i = 0; i < V.size.x; i++) {
214 for (let j = 0; j < V.size.y; j++) {
215 if (this.board[i][j] == V.EMPTY) emptySquares.push([i, j]);
216 else if (
217 this.getColor(i, j) == color &&
218 this.getPiece(i, j) == V.KNIGHT
219 ) {
220 knightCounter++;
221 }
222 }
223 }
224 if (knightCounter <= 1) finalPieces = [V.KNIGHT];
225 else {
226 // Generate all possible landings, maybe capturing something on the way
227 let capture = undefined;
228 if (this.board[x2][y2] != V.EMPTY) {
229 capture = JSON.parse(JSON.stringify({
230 x: x2,
231 y: y2,
232 c: this.getColor(x2, y2),
233 p: this.getPiece(x2, y2)
234 }));
235 }
236 emptySquares.forEach(sq => {
237 if (sq[0] != lastRank) {
238 let newMove = this.getBasicMove([x1, y1], [sq[0], sq[1]]);
239 if (!!capture) newMove.vanish.push(capture);
240 moves.push(newMove);
241 }
242 });
243 return;
244 }
245 }
246 let tr = null;
247 for (let piece of finalPieces) {
248 tr = (piece != V.PAWN ? { c: color, p: piece } : null);
249 moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
250 }
251 }
252
253 filterValid(moves) {
254 // No checks:
255 return moves;
256 }
257
258 atLeastOneMove(color) {
259 const curTurn = this.turn;
260 this.turn = color;
261 const res = super.atLeastOneMove();
262 this.turn = curTurn;
263 return res;
264 }
265
266 // White and black (partial) moves were played: merge
267 resolveSynchroneMove(move) {
268 let m1 = this.whiteMove;
269 let m2 = move;
270 const movingLikeCapture = (m) => {
271 const shift = (m.vanish[0].c == 'w' ? -1 : 1);
272 return (
273 m.start.x + shift == m.end.x &&
274 Math.abs(m.end.y - m.start.y) == 1
275 );
276 };
277 const isPossible = (m, other) => {
278 return (
279 (
280 m.vanish[0].p == V.KNIGHT &&
281 (
282 m.vanish.length == 1 ||
283 m.vanish[1].c != m.vanish[0].c ||
284 // Self-capture attempt
285 (
286 !other.illegal &&
287 other.end.x == m.end.x &&
288 other.end.y == m.end.y
289 )
290 )
291 )
292 ||
293 (
294 m.vanish[0].p == V.PAWN &&
295 !other.illegal &&
296 (
297 (
298 // Promotion attempt
299 m.end.x == (m.vanish[0].c == "w" ? 0 : V.size.x - 1) &&
300 other.vanish.length == 2 &&
301 other.vanish[1].p == V.KNIGHT &&
302 other.vanish[1].c == m.vanish[0].c
303 )
304 ||
305 (
306 // Moving attempt
307 !movingLikeCapture(m) &&
308 other.start.x == m.end.x &&
309 other.start.y == m.end.y
310 )
311 ||
312 (
313 // Capture attempt
314 movingLikeCapture(m) &&
315 other.end.x == m.end.x &&
316 other.end.y == m.end.y
317 )
318 )
319 )
320 );
321 };
322 if (!!m1.illegal && !isPossible(m1, m2)) {
323 // Either an anticipated capture of something which didn't move
324 // (or not to the right square), or a push through blocus.
325 // ==> Just discard the move, and add a penalty point
326 this.penaltyFlags[m1.vanish[0].c]++;
327 m1.isNull = true;
328 }
329 if (!!m2.illegal && !isPossible(m2, m1)) {
330 this.penaltyFlags[m2.vanish[0].c]++;
331 m2.isNull = true;
332 }
333 if (!!m1.isNull) m1 = null;
334 if (!!m2.isNull) m2 = null;
335 // If one move is illegal, just execute the other
336 if (!m1 && !!m2) return m2;
337 if (!m2 && !!m1) return m1;
338 // For PlayOnBoard (no need for start / end, irrelevant)
339 let smove = {
340 appear: [],
341 vanish: []
342 };
343 if (!m1 && !m2) return smove;
344 // Both moves are now legal or at least possible:
345 smove.vanish.push(m1.vanish[0]);
346 smove.vanish.push(m2.vanish[0]);
347 if ((m1.end.x != m2.end.x) || (m1.end.y != m2.end.y)) {
348 // Easy case: two independant moves
349 smove.appear.push(m1.appear[0]);
350 smove.appear.push(m2.appear[0]);
351 // "Captured" pieces may have moved:
352 if (
353 m1.vanish.length == 2 &&
354 (
355 m1.vanish[1].x != m2.start.x ||
356 m1.vanish[1].y != m2.start.y
357 )
358 ) {
359 smove.vanish.push(m1.vanish[1]);
360 }
361 if (
362 m2.vanish.length == 2 &&
363 (
364 m2.vanish[1].x != m1.start.x ||
365 m2.vanish[1].y != m1.start.y
366 )
367 ) {
368 smove.vanish.push(m2.vanish[1]);
369 }
370 } else {
371 // Collision: priority to the anticipated capture, if any.
372 // If ex-aequo: knight wins (higher risk), or both disappears.
373 // Then, priority to the knight vs pawn: remains.
374 // Finally: both disappears.
375 let remain = null;
376 const p1 = m1.vanish[0].p;
377 const p2 = m2.vanish[0].p;
378 if (!!m1.illegal && !m2.illegal) remain = { c: 'w', p: p1 };
379 else if (!!m2.illegal && !m1.illegal) remain = { c: 'b', p: p2 };
380 if (!remain) {
381 // Either both are illegal or both are legal
382 if (p1 == V.KNIGHT && p2 == V.PAWN) remain = { c: 'w', p: p1 };
383 else if (p2 == V.KNIGHT && p1 == V.PAWN) remain = { c: 'b', p: p2 };
384 // If remain is still null: same type same risk, both disappear
385 }
386 if (!!remain) {
387 smove.appear.push({
388 x: m1.end.x,
389 y: m1.end.y,
390 p: remain.p,
391 c: remain.c
392 });
393 }
394 }
395 return smove;
396 }
397
398 play(move) {
399 // Do not play on board (would reveal the move...)
400 move.flags = JSON.stringify(this.aggregateFlags());
401 this.turn = V.GetOppCol(this.turn);
402 this.movesCount++;
403 this.postPlay(move);
404 }
405
406 postPlay(move) {
407 if (this.turn == 'b') {
408 // NOTE: whiteMove is used read-only, so no need to copy
409 this.whiteMove = move;
410 return;
411 }
412 // A full turn just ended:
413 const smove = this.resolveSynchroneMove(move);
414 V.PlayOnBoard(this.board, smove);
415 move.whiteMove = this.whiteMove; //for undo
416 this.whiteMove = null;
417 move.smove = smove;
418 }
419
420 undo(move) {
421 this.disaggregateFlags(JSON.parse(move.flags));
422 if (this.turn == 'w')
423 // Back to the middle of the move
424 V.UndoOnBoard(this.board, move.smove);
425 this.turn = V.GetOppCol(this.turn);
426 this.movesCount--;
427 this.postUndo(move);
428 }
429
430 postUndo(move) {
431 if (this.turn == 'w') this.whiteMove = null;
432 else this.whiteMove = move.whiteMove;
433 }
434
435 getCheckSquares() {
436 return [];
437 }
438
439 getCurrentScore() {
440 if (this.turn == 'b')
441 // Turn (white + black) not over yet
442 return "*";
443 // Count footmen: if a side has none, it loses
444 let fmCount = { 'w': 0, 'b': 0 };
445 for (let i=0; i<5; i++) {
446 for (let j=0; j<5; j++) {
447 if (this.board[i][j] != V.EMPTY && this.getPiece(i, j) == V.PAWN)
448 fmCount[this.getColor(i, j)]++;
449 }
450 }
451 if (Object.values(fmCount).some(v => v == 0)) {
452 if (fmCount['w'] == 0 && fmCount['b'] == 0)
453 // Everyone died
454 return "1/2";
455 if (fmCount['w'] == 0) return "0-1";
456 return "1-0"; //fmCount['b'] == 0
457 }
458 // Check penaltyFlags: if a side has 2 or more, it loses
459 if (Object.values(this.penaltyFlags).every(v => v == 2)) return "1/2";
460 if (this.penaltyFlags['w'] == 2) return "0-1";
461 if (this.penaltyFlags['b'] == 2) return "1-0";
462 if (!this.atLeastOneMove('w') || !this.atLeastOneMove('b'))
463 // Stalemate (should be very rare)
464 return "1/2";
465 return "*";
466 }
467
468 getComputerMove() {
469 const maxeval = V.INFINITY;
470 const color = this.turn;
471 let moves = this.getAllValidMoves();
472 if (moves.length == 0)
473 // TODO: this situation should not happen
474 return null;
475
476 // Rank moves at depth 1:
477 let validMoves = [];
478 let illegalMoves = [];
479 moves.forEach(m => {
480 // Warning: m might be illegal!
481 if (!m.illegal) {
482 V.PlayOnBoard(this.board, m);
483 m.eval = this.evalPosition();
484 V.UndoOnBoard(this.board, m);
485 validMoves.push(m);
486 } else illegalMoves.push(m);
487 });
488
489 const illegalRatio = illegalMoves.length / moves.length;
490 if (Math.random() < illegalRatio)
491 // Return a random illegal move
492 return illegalMoves[randInt(illegalMoves.length)];
493
494 validMoves.sort((a, b) => {
495 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
496 });
497 let candidates = [0];
498 for (
499 let i = 1;
500 i < validMoves.length && validMoves[i].eval == moves[0].eval;
501 i++
502 ) {
503 candidates.push(i);
504 }
505 return validMoves[candidates[randInt(candidates.length)]];
506 }
507
508 getNotation(move) {
509 // Basic system: piece + init + dest square
510 return (
511 (move.vanish[0].p == V.KNIGHT ? "N" : "") +
512 V.CoordsToSquare(move.start) +
513 V.CoordsToSquare(move.end)
514 );
515 }
516 };