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