1 import { ChessRules
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class ApocalypseRules
extends ChessRules
{
10 static get PawnSpecs() {
16 promotions: [V
.KNIGHT
]
21 static get SomeHiddenMoves() {
25 static get HasCastle() {
29 static get HasEnpassant() {
33 static get CanAnalyze() {
37 static get ShowMoves() {
42 // Show the piece taken, if any, and not multiple pawns:
43 if (m
.vanish
.length
== 1) return "Apocalypse/empty";
44 return m
.vanish
[1].c
+ m
.vanish
[1].p
;
48 return [V
.PAWN
, V
.KNIGHT
];
51 static IsGoodPosition(position
) {
52 if (position
.length
== 0) return false;
53 const rows
= position
.split("/");
54 if (rows
.length
!= V
.size
.x
) return false;
55 // At least one pawn per color
56 let pawns
= { "p": 0, "P": 0 };
57 for (let row
of rows
) {
59 for (let i
= 0; i
< row
.length
; i
++) {
60 if (['P','p'].includes(row
[i
])) pawns
[row
[i
]]++;
61 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
63 const num
= parseInt(row
[i
], 10);
64 if (isNaN(num
)) return false;
68 if (sumElts
!= V
.size
.y
) return false;
70 if (Object
.values(pawns
).some(v
=> v
== 0))
75 static IsGoodFen(fen
) {
76 if (!ChessRules
.IsGoodFen(fen
)) return false;
77 const fenParsed
= V
.ParseFen(fen
);
81 fenParsed
.turn
== "b" &&
82 // NOTE: do not check really JSON stringified move...
83 (!fenParsed
.whiteMove
|| fenParsed
.whiteMove
== "-")
86 (fenParsed
.turn
== "w" && fenParsed
.whiteMove
!= "-")
93 static IsGoodFlags(flags
) {
94 return !!flags
.match(/^[0-2]{2,2}$/);
98 return this.penaltyFlags
;
101 disaggregateFlags(flags
) {
102 this.penaltyFlags
= flags
;
105 static ParseFen(fen
) {
106 const fenParts
= fen
.split(" ");
107 return Object
.assign(
108 ChessRules
.ParseFen(fen
),
109 { whiteMove: fenParts
[4] }
114 return { x: 5, y: 5 };
117 static GenRandInitFen() {
118 return "npppn/p3p/5/P3P/NPPPN w 0 00 -";
122 return super.getFen() + " " + this.getWhitemoveFen();
126 return super.getFenForRepeat() + "_" + this.getWhitemoveFen();
131 this.penaltyFlags
['w'].toString() + this.penaltyFlags
['b'].toString()
135 setOtherVariables(fen
) {
136 const parsedFen
= V
.ParseFen(fen
);
137 this.setFlags(parsedFen
.flags
);
138 // Also init whiteMove
140 parsedFen
.whiteMove
!= "-"
141 ? JSON
.parse(parsedFen
.whiteMove
)
146 this.penaltyFlags
= {
147 'w': parseInt(fenflags
[0], 10),
148 'b': parseInt(fenflags
[1], 10)
153 if (!this.whiteMove
) return "-";
154 return JSON
.stringify({
155 start: this.whiteMove
.start
,
156 end: this.whiteMove
.end
,
157 appear: this.whiteMove
.appear
,
158 vanish: this.whiteMove
.vanish
162 getSpeculations(moves
, sq
) {
165 const mHash
= "m" + m
.start
.x
+ m
.start
.y
+ m
.end
.x
+ m
.end
.y
;
166 moveSet
[mHash
] = true;
168 const color
= this.turn
;
169 this.turn
= V
.GetOppCol(color
);
170 const oppMoves
= super.getAllValidMoves();
172 // For each opponent's move, generate valid moves [from sq if same color]
173 let speculations
= [];
174 oppMoves
.forEach(m
=> {
175 V
.PlayOnBoard(this.board
, m
);
176 const newValidMoves
=
179 this.getColor(sq
[0], sq
[1]) == color
180 ? super.getPotentialMovesFrom(sq
)
183 : super.getAllValidMoves();
184 newValidMoves
.forEach(vm
=> {
185 const mHash
= "m" + vm
.start
.x
+ vm
.start
.y
+ vm
.end
.x
+ vm
.end
.y
;
186 if (!moveSet
[mHash
]) {
187 moveSet
[mHash
] = true;
188 vm
.end
.illegal
= true; //potentially illegal!
189 speculations
.push(vm
);
192 V
.UndoOnBoard(this.board
, m
);
197 getPossibleMovesFrom([x
, y
]) {
198 const possibleMoves
= super.getPotentialMovesFrom([x
, y
])
199 // Augment potential moves with opponent's moves speculation:
200 return possibleMoves
.concat(this.getSpeculations(possibleMoves
, [x
, y
]));
204 // Return possible moves + potentially valid moves
205 const validMoves
= super.getAllValidMoves();
206 return validMoves
.concat(this.getSpeculations(validMoves
));
209 addPawnMoves([x1
, y1
], [x2
, y2
], moves
) {
210 let finalPieces
= [V
.PAWN
];
211 const color
= this.turn
;
212 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
213 if (x2
== lastRank
) {
214 // If 0 or 1 horsemen, promote in knight
215 let knightCounter
= 0;
216 let emptySquares
= [];
217 for (let i
= 0; i
< V
.size
.x
; i
++) {
218 for (let j
= 0; j
< V
.size
.y
; j
++) {
219 if (this.board
[i
][j
] == V
.EMPTY
) emptySquares
.push([i
, j
]);
221 this.getColor(i
, j
) == color
&&
222 this.getPiece(i
, j
) == V
.KNIGHT
228 if (knightCounter
<= 1) finalPieces
= [V
.KNIGHT
];
230 // Generate all possible landings, maybe capturing something on the way
231 let capture
= undefined;
232 if (this.board
[x2
][y2
] != V
.EMPTY
) {
233 capture
= JSON
.parse(JSON
.stringify({
236 c: this.getColor(x2
, y2
),
237 p: this.getPiece(x2
, y2
)
240 emptySquares
.forEach(sq
=> {
241 if (sq
[0] != lastRank
) {
242 let newMove
= this.getBasicMove([x1
, y1
], [sq
[0], sq
[1]]);
243 if (!!capture
) newMove
.vanish
.push(capture
);
251 for (let piece
of finalPieces
) {
252 tr
= (piece
!= V
.PAWN
? { c: color
, p: piece
} : null);
253 moves
.push(this.getBasicMove([x1
, y1
], [x2
, y2
], tr
));
262 atLeastOneMove(color
) {
263 const curTurn
= this.turn
;
265 const res
= super.atLeastOneMove();
270 // White and black (partial) moves were played: merge
271 resolveSynchroneMove(move) {
272 let m1
= this.whiteMove
;
274 const movingLikeCapture
= (m
) => {
275 const shift
= (m
.vanish
[0].c
== 'w' ? -1 : 1);
277 m
.start
.x
+ shift
== m
.end
.x
&&
278 Math
.abs(m
.end
.y
- m
.start
.y
) == 1
281 const isPossible
= (m
, other
) => {
284 m
.vanish
[0].p
== V
.KNIGHT
&&
286 m
.vanish
.length
== 1 ||
287 m
.vanish
[1].c
!= m
.vanish
[0].c
||
288 // Self-capture attempt
290 !other
.end
.illegal
&&
291 other
.end
.x
== m
.end
.x
&&
292 other
.end
.y
== m
.end
.y
298 m
.vanish
[0].p
== V
.PAWN
&&
299 !other
.end
.illegal
&&
303 m
.end
.x
== (m
.vanish
[0].c
== "w" ? 0 : V
.size
.x
- 1) &&
304 other
.vanish
.length
== 2 &&
305 other
.vanish
[1].p
== V
.KNIGHT
&&
306 other
.vanish
[1].c
== m
.vanish
[0].c
311 !movingLikeCapture(m
) &&
312 other
.start
.x
== m
.end
.x
&&
313 other
.start
.y
== m
.end
.y
318 movingLikeCapture(m
) &&
319 other
.end
.x
== m
.end
.x
&&
320 other
.end
.y
== m
.end
.y
326 if (!!m1
.end
.illegal
&& !isPossible(m1
, m2
)) {
327 // Either an anticipated capture of something which didn't move
328 // (or not to the right square), or a push through blocus.
329 // ==> Just discard the move, and add a penalty point
330 this.penaltyFlags
[m1
.vanish
[0].c
]++;
333 if (!!m2
.end
.illegal
&& !isPossible(m2
, m1
)) {
334 this.penaltyFlags
[m2
.vanish
[0].c
]++;
337 if (!!m1
.isNull
) m1
= null;
338 if (!!m2
.isNull
) m2
= null;
339 // If one move is illegal, just execute the other
340 if (!m1
&& !!m2
) return m2
;
341 if (!m2
&& !!m1
) return m1
;
342 // For PlayOnBoard (no need for start / end, irrelevant)
347 if (!m1
&& !m2
) return smove
;
348 // Both moves are now legal or at least possible:
349 smove
.vanish
.push(m1
.vanish
[0]);
350 smove
.vanish
.push(m2
.vanish
[0]);
351 if ((m1
.end
.x
!= m2
.end
.x
) || (m1
.end
.y
!= m2
.end
.y
)) {
352 // Easy case: two independant moves
353 smove
.appear
.push(m1
.appear
[0]);
354 smove
.appear
.push(m2
.appear
[0]);
355 // "Captured" pieces may have moved:
357 m1
.vanish
.length
== 2 &&
359 m1
.vanish
[1].x
!= m2
.start
.x
||
360 m1
.vanish
[1].y
!= m2
.start
.y
363 smove
.vanish
.push(m1
.vanish
[1]);
366 m2
.vanish
.length
== 2 &&
368 m2
.vanish
[1].x
!= m1
.start
.x
||
369 m2
.vanish
[1].y
!= m1
.start
.y
372 smove
.vanish
.push(m2
.vanish
[1]);
375 // Collision: priority to the anticipated capture, if any.
376 // If ex-aequo: knight wins (higher risk), or both disappears.
377 // Then, priority to the knight vs pawn: remains.
378 // Finally: both disappears.
380 const p1
= m1
.vanish
[0].p
;
381 const p2
= m2
.vanish
[0].p
;
382 if (!!m1
.end
.illegal
&& !m2
.end
.illegal
) remain
= { c: 'w', p: p1
};
383 else if (!!m2
.end
.illegal
&& !m1
.end
.illegal
) remain
= { c: 'b', p: p2
};
385 // Either both are illegal or both are legal
386 if (p1
== V
.KNIGHT
&& p2
== V
.PAWN
) remain
= { c: 'w', p: p1
};
387 else if (p2
== V
.KNIGHT
&& p1
== V
.PAWN
) remain
= { c: 'b', p: p2
};
388 // If remain is still null: same type same risk, both disappear
403 // Do not play on board (would reveal the move...)
404 move.flags
= JSON
.stringify(this.aggregateFlags());
405 this.turn
= V
.GetOppCol(this.turn
);
411 if (this.turn
== 'b') {
412 // NOTE: whiteMove is used read-only, so no need to copy
413 this.whiteMove
= move;
416 // A full turn just ended:
417 const smove
= this.resolveSynchroneMove(move);
418 V
.PlayOnBoard(this.board
, smove
);
419 move.whiteMove
= this.whiteMove
; //for undo
420 this.whiteMove
= null;
425 this.disaggregateFlags(JSON
.parse(move.flags
));
426 if (this.turn
== 'w')
427 // Back to the middle of the move
428 V
.UndoOnBoard(this.board
, move.smove
);
429 this.turn
= V
.GetOppCol(this.turn
);
435 if (this.turn
== 'w') this.whiteMove
= null;
436 else this.whiteMove
= move.whiteMove
;
444 if (this.turn
== 'b')
445 // Turn (white + black) not over yet
447 // Count footmen: if a side has none, it loses
448 let fmCount
= { 'w': 0, 'b': 0 };
449 for (let i
=0; i
<5; i
++) {
450 for (let j
=0; j
<5; j
++) {
451 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
, j
) == V
.PAWN
)
452 fmCount
[this.getColor(i
, j
)]++;
455 if (Object
.values(fmCount
).some(v
=> v
== 0)) {
456 if (fmCount
['w'] == 0 && fmCount
['b'] == 0)
459 if (fmCount
['w'] == 0) return "0-1";
460 return "1-0"; //fmCount['b'] == 0
462 // Check penaltyFlags: if a side has 2 or more, it loses
463 if (Object
.values(this.penaltyFlags
).every(v
=> v
== 2)) return "1/2";
464 if (this.penaltyFlags
['w'] == 2) return "0-1";
465 if (this.penaltyFlags
['b'] == 2) return "1-0";
466 if (!this.atLeastOneMove('w') || !this.atLeastOneMove('b'))
467 // Stalemate (should be very rare)
473 const maxeval
= V
.INFINITY
;
474 const color
= this.turn
;
475 let moves
= this.getAllValidMoves();
476 if (moves
.length
== 0)
477 // TODO: this situation should not happen
480 // Rank moves at depth 1:
482 let illegalMoves
= [];
484 // Warning: m might be illegal!
485 if (!m
.end
.illegal
) {
486 V
.PlayOnBoard(this.board
, m
);
487 m
.eval
= this.evalPosition();
488 V
.UndoOnBoard(this.board
, m
);
490 } else illegalMoves
.push(m
);
493 const illegalRatio
= illegalMoves
.length
/ moves
.length
;
494 if (Math
.random() < illegalRatio
)
495 // Return a random illegal move
496 return illegalMoves
[randInt(illegalMoves
.length
)];
498 validMoves
.sort((a
, b
) => {
499 return (color
== "w" ? 1 : -1) * (b
.eval
- a
.eval
);
501 let candidates
= [0];
504 i
< validMoves
.length
&& validMoves
[i
].eval
== moves
[0].eval
;
509 return validMoves
[candidates
[randInt(candidates
.length
)]];
513 // Basic system: piece + init + dest square
515 (move.vanish
[0].p
== V
.KNIGHT
? "N" : "") +
516 V
.CoordsToSquare(move.start
) +
517 V
.CoordsToSquare(move.end
)