Started code review + some fixes (unfinished)
[vchess.git] / client / src / variants / Extinction.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2
6808d7a1
BA
3export const VariantRules = class ExtinctionRules extends ChessRules {
4 setOtherVariables(fen) {
dac39588
BA
5 super.setOtherVariables(fen);
6 const pos = V.ParseFen(fen).position;
7 // NOTE: no need for safety "|| []", because each piece type must be present
8 // (otherwise game is already over!)
6808d7a1
BA
9 this.material = {
10 w: {
dac39588
BA
11 [V.KING]: pos.match(/K/g).length,
12 [V.QUEEN]: pos.match(/Q/g).length,
13 [V.ROOK]: pos.match(/R/g).length,
14 [V.KNIGHT]: pos.match(/N/g).length,
15 [V.BISHOP]: pos.match(/B/g).length,
16 [V.PAWN]: pos.match(/P/g).length
17 },
6808d7a1 18 b: {
dac39588
BA
19 [V.KING]: pos.match(/k/g).length,
20 [V.QUEEN]: pos.match(/q/g).length,
21 [V.ROOK]: pos.match(/r/g).length,
22 [V.KNIGHT]: pos.match(/n/g).length,
23 [V.BISHOP]: pos.match(/b/g).length,
24 [V.PAWN]: pos.match(/p/g).length
25 }
26 };
27 }
a6abf094 28
6808d7a1
BA
29 getPotentialPawnMoves([x, y]) {
30 let moves = super.getPotentialPawnMoves([x, y]);
dac39588
BA
31 // Add potential promotions into king
32 const color = this.turn;
6808d7a1
BA
33 const shift = color == "w" ? -1 : 1;
34 const lastRank = color == "w" ? 0 : V.size.x - 1;
1221ac47 35
6808d7a1 36 if (x + shift == lastRank) {
dac39588 37 // Normal move
6808d7a1
BA
38 if (this.board[x + shift][y] == V.EMPTY)
39 moves.push(
40 this.getBasicMove([x, y], [x + shift, y], { c: color, p: V.KING })
41 );
dac39588 42 // Captures
6808d7a1
BA
43 if (
44 y > 0 &&
45 this.board[x + shift][y - 1] != V.EMPTY &&
46 this.canTake([x, y], [x + shift, y - 1])
47 ) {
48 moves.push(
49 this.getBasicMove([x, y], [x + shift, y - 1], { c: color, p: V.KING })
50 );
dac39588 51 }
6808d7a1
BA
52 if (
53 y < V.size.y - 1 &&
54 this.board[x + shift][y + 1] != V.EMPTY &&
55 this.canTake([x, y], [x + shift, y + 1])
56 ) {
57 moves.push(
58 this.getBasicMove([x, y], [x + shift, y + 1], { c: color, p: V.KING })
59 );
dac39588
BA
60 }
61 }
1221ac47 62
dac39588
BA
63 return moves;
64 }
1221ac47 65
dac39588 66 // TODO: verify this assertion
6808d7a1 67 atLeastOneMove() {
dac39588
BA
68 return true; //always at least one possible move
69 }
a6abf094 70
6808d7a1 71 underCheck() {
dac39588
BA
72 return false; //there is no check
73 }
a6abf094 74
6808d7a1 75 getCheckSquares() {
dac39588
BA
76 return [];
77 }
a6abf094 78
6808d7a1 79 updateVariables(move) {
dac39588
BA
80 super.updateVariables(move);
81 // Treat the promotion case: (not the capture part)
6808d7a1 82 if (move.appear[0].p != move.vanish[0].p) {
dac39588
BA
83 this.material[move.appear[0].c][move.appear[0].p]++;
84 this.material[move.appear[0].c][V.PAWN]--;
85 }
6808d7a1
BA
86 if (move.vanish.length == 2 && move.appear.length == 1)
87 //capture
dac39588
BA
88 this.material[move.vanish[1].c][move.vanish[1].p]--;
89 }
a6abf094 90
6808d7a1 91 unupdateVariables(move) {
dac39588 92 super.unupdateVariables(move);
6808d7a1 93 if (move.appear[0].p != move.vanish[0].p) {
dac39588
BA
94 this.material[move.appear[0].c][move.appear[0].p]--;
95 this.material[move.appear[0].c][V.PAWN]++;
96 }
6808d7a1 97 if (move.vanish.length == 2 && move.appear.length == 1)
dac39588
BA
98 this.material[move.vanish[1].c][move.vanish[1].p]++;
99 }
a6abf094 100
6808d7a1
BA
101 getCurrentScore() {
102 if (this.atLeastOneMove()) {
103 // game not over?
dac39588 104 const color = this.turn;
6808d7a1
BA
105 if (
106 Object.keys(this.material[color]).some(p => {
107 return this.material[color][p] == 0;
108 })
109 ) {
110 return this.turn == "w" ? "0-1" : "1-0";
dac39588
BA
111 }
112 return "*";
113 }
a6abf094 114
6808d7a1 115 return this.turn == "w" ? "0-1" : "1-0"; //NOTE: currently unreachable...
dac39588 116 }
a6abf094 117
6808d7a1 118 evalPosition() {
dac39588 119 const color = this.turn;
6808d7a1
BA
120 if (
121 Object.keys(this.material[color]).some(p => {
122 return this.material[color][p] == 0;
123 })
124 ) {
dac39588 125 // Very negative (resp. positive) if white (reps. black) pieces set is incomplete
6808d7a1 126 return (color == "w" ? -1 : 1) * V.INFINITY;
dac39588
BA
127 }
128 return super.evalPosition();
129 }
6808d7a1 130};