Add temporary patch for preset challenges
[vchess.git] / client / src / variants / Racingkings.js
CommitLineData
6b7b2cf7
BA
1import { ChessRules } from "@/base_rules";
2
32f6285e 3export class RacingkingsRules extends ChessRules {
7e8a7ea1 4
6b7b2cf7
BA
5 static get HasFlags() {
6 return false;
7 }
8
9 static get HasEnpassant() {
10 return false;
11 }
12
13 static get CanFlip() {
14 return false;
15 }
16
17 static GenRandInitFen() {
18 return "8/8/8/8/8/8/krbnNBRK/qrbnNBRQ w 0";
19 }
20
21 filterValid(moves) {
22 if (moves.length == 0) return [];
23 const color = this.turn;
24 const oppCol = V.GetOppCol(color);
25 return moves.filter(m => {
26 this.play(m);
27 // Giving check is forbidden as well:
28 const res = !this.underCheck(color) && !this.underCheck(oppCol);
29 this.undo(m);
30 return res;
31 });
32 }
33
34 getCurrentScore() {
35 // If both kings arrived on the last rank, it's a draw
36 if (this.kingPos['w'][0] == 0 && this.kingPos['b'][0] == 0) return "1/2";
37 // If after my move the opponent king is on last rank, I lose.
38 // This is only possible with black.
39 if (this.turn == 'w' && this.kingPos['w'][0] == 0) return "1-0";
40 // Turn has changed:
41 const color = V.GetOppCol(this.turn);
42 if (this.kingPos[color][0] == 0) {
43 // The opposing edge is reached!
44 // If color is white and the black king can arrive on 8th rank
45 // at next move, then it should be a draw:
46 if (color == "w" && this.kingPos['b'][0] == 1) {
47 // Search for a move
0e76401f
BA
48 const oppKingMoves = this.filterValid(
49 this.getPotentialKingMoves(this.kingPos['b']));
6b7b2cf7
BA
50 if (oppKingMoves.some(m => m.end.x == 0)) return "*";
51 }
52 return color == "w" ? "1-0" : "0-1";
53 }
54 if (this.atLeastOneMove()) return "*";
55 // Stalemate (will probably never happen)
56 return "1/2";
57 }
58
59 evalPosition() {
60 // Count material:
61 let evaluation = super.evalPosition();
62 // Ponder with king position:
63 return evaluation/5 + this.kingPos["b"][0] - this.kingPos["w"][0];
64 }
7e8a7ea1 65
6b7b2cf7 66};