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