Fix Dark variant when en-passant captures are possible. Credit variants authors
[vchess.git] / client / src / variants / Dark.js
CommitLineData
0c3fe8a6 1import { ChessRules } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
0c3fe8a6
BA
3import { randInt } from "@/utils/alea";
4
6808d7a1 5export const VariantRules = class DarkRules extends ChessRules {
20620465 6 // Analyse in Dark mode makes no sense
8477e53d 7 static get CanAnalyze() {
20620465
BA
8 return false;
9 }
10
11 // Moves are revealed only when game ends
12 static get ShowMoves() {
13 return "none";
14 }
15
6808d7a1 16 setOtherVariables(fen) {
dac39588 17 super.setOtherVariables(fen);
6808d7a1 18 const [sizeX, sizeY] = [V.size.x, V.size.y];
dac39588 19 this.enlightened = {
6808d7a1
BA
20 w: ArrayFun.init(sizeX, sizeY),
21 b: ArrayFun.init(sizeX, sizeY)
dac39588
BA
22 };
23 // Setup enlightened: squares reachable by each side
24 // (TODO: one side would be enough ?)
25 this.updateEnlightened();
26 }
375ecdd1 27
6808d7a1
BA
28 updateEnlightened() {
29 for (let i = 0; i < V.size.x; i++) {
30 for (let j = 0; j < V.size.y; j++) {
dac39588
BA
31 this.enlightened["w"][i][j] = false;
32 this.enlightened["b"][i][j] = false;
33 }
34 }
6808d7a1 35 const pawnShift = { w: -1, b: 1 };
dac39588 36 // Initialize with pieces positions (which are seen)
6808d7a1
BA
37 for (let i = 0; i < V.size.x; i++) {
38 for (let j = 0; j < V.size.y; j++) {
39 if (this.board[i][j] != V.EMPTY) {
40 const color = this.getColor(i, j);
dac39588
BA
41 this.enlightened[color][i][j] = true;
42 // Add potential squares visible by "impossible pawn capture"
6808d7a1
BA
43 if (this.getPiece(i, j) == V.PAWN) {
44 for (let shiftY of [-1, 1]) {
45 if (
46 V.OnBoard(i + pawnShift[color], j + shiftY) &&
47 this.board[i + pawnShift[color]][j + shiftY] == V.EMPTY
48 ) {
241bf8f2 49 this.enlightened[color][i + pawnShift[color]][j + shiftY] = true;
dac39588
BA
50 }
51 }
52 }
53 }
54 }
55 }
56 const currentTurn = this.turn;
57 this.turn = "w";
58 const movesWhite = this.getAllValidMoves();
59 this.turn = "b";
60 const movesBlack = this.getAllValidMoves();
61 this.turn = currentTurn;
62 for (let move of movesWhite)
63 this.enlightened["w"][move.end.x][move.end.y] = true;
64 for (let move of movesBlack)
65 this.enlightened["b"][move.end.x][move.end.y] = true;
d048c4c9
BA
66 // Include en-passant capturing square if any:
67 let moves = currentTurn == "w" ? movesWhite : movesBlack;
68 for (let m of moves) {
69 if (
70 m.appear[0].p == V.PAWN &&
71 m.vanish.length == 2 &&
72 m.vanish[1].x != m.end.x
73 ) {
74 const psq = m.vanish[1];
75 this.enlightened[currentTurn][psq.x][psq.y] = true;
76 break;
77 }
78 }
dac39588 79 }
375ecdd1 80
dac39588 81 // Has to be redefined to avoid an infinite loop
6808d7a1 82 getAllValidMoves() {
dac39588 83 const color = this.turn;
dac39588 84 let potentialMoves = [];
6808d7a1
BA
85 for (let i = 0; i < V.size.x; i++) {
86 for (let j = 0; j < V.size.y; j++) {
87 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color)
88 Array.prototype.push.apply(
89 potentialMoves,
90 this.getPotentialMovesFrom([i, j])
91 );
dac39588
BA
92 }
93 }
94 return potentialMoves; //because there are no checks
95 }
f6dbe8e3 96
6808d7a1 97 getCheckSquares() {
dac39588
BA
98 return [];
99 }
375ecdd1 100
6808d7a1 101 updateVariables(move) {
dac39588 102 super.updateVariables(move);
241bf8f2
BA
103 if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
104 // We took opponent king (because if castle vanish[1] is a rook)
6808d7a1 105 this.kingPos[this.turn] = [-1, -1];
388e4c40 106
dac39588
BA
107 // Update lights for both colors:
108 this.updateEnlightened();
109 }
388e4c40 110
6808d7a1 111 unupdateVariables(move) {
dac39588
BA
112 super.unupdateVariables(move);
113 const c = move.vanish[0].c;
114 const oppCol = V.GetOppCol(c);
241bf8f2
BA
115 if (this.kingPos[oppCol][0] < 0)
116 // Last move took opponent's king:
117 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
388e4c40 118
dac39588
BA
119 // Update lights for both colors:
120 this.updateEnlightened();
121 }
375ecdd1 122
6808d7a1 123 getCurrentScore() {
dac39588
BA
124 const color = this.turn;
125 const kp = this.kingPos[color];
6808d7a1 126 if (kp[0] < 0)
241bf8f2 127 // King disappeared
6808d7a1 128 return color == "w" ? "0-1" : "1-0";
241bf8f2
BA
129 // Assume that stalemate is impossible (I think so. Would need proof...)
130 return "*";
dac39588 131 }
375ecdd1 132
6808d7a1 133 static get THRESHOLD_MATE() {
dac39588
BA
134 return 500; //checkmates evals may be slightly below 1000
135 }
5915f720 136
dac39588 137 // In this special situation, we just look 1 half move ahead
6808d7a1 138 getComputerMove() {
dac39588
BA
139 const maxeval = V.INFINITY;
140 const color = this.turn;
141 const oppCol = V.GetOppCol(color);
6808d7a1 142 const pawnShift = color == "w" ? -1 : 1;
5915f720 143
dac39588
BA
144 // Do not cheat: the current enlightment is all we can see
145 const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
5915f720 146
dac39588
BA
147 // Can a slider on (i,j) apparently take my king?
148 // NOTE: inaccurate because assume yes if some squares are shadowed
6808d7a1 149 const sliderTake = ([i, j], piece) => {
dac39588
BA
150 const kp = this.kingPos[color];
151 let step = undefined;
6808d7a1
BA
152 if (piece == V.BISHOP) {
153 if (Math.abs(kp[0] - i) == Math.abs(kp[1] - j)) {
154 step = [
155 (i - kp[0]) / Math.abs(i - kp[0]),
156 (j - kp[1]) / Math.abs(j - kp[1])
dac39588
BA
157 ];
158 }
6808d7a1
BA
159 } else if (piece == V.ROOK) {
160 if (kp[0] == i) step = [0, (j - kp[1]) / Math.abs(j - kp[1])];
161 else if (kp[1] == j) step = [(i - kp[0]) / Math.abs(i - kp[0]), 0];
dac39588 162 }
6808d7a1 163 if (!step) return false;
dac39588
BA
164 // Check for obstacles
165 let obstacle = false;
166 for (
6808d7a1 167 let x = kp[0] + step[0], y = kp[1] + step[1];
dac39588 168 x != i && y != j;
6808d7a1
BA
169 x += step[0], y += step[1]
170 ) {
171 if (myLight[x][y] && this.board[x][y] != V.EMPTY) {
dac39588
BA
172 obstacle = true;
173 break;
174 }
175 }
6808d7a1 176 if (!obstacle) return true;
dac39588
BA
177 return false;
178 };
5915f720 179
dac39588
BA
180 // Do I see something which can take my king ?
181 const kingThreats = () => {
182 const kp = this.kingPos[color];
6808d7a1
BA
183 for (let i = 0; i < V.size.x; i++) {
184 for (let j = 0; j < V.size.y; j++) {
185 if (
186 myLight[i][j] &&
187 this.board[i][j] != V.EMPTY &&
188 this.getColor(i, j) != color
189 ) {
190 switch (this.getPiece(i, j)) {
dac39588 191 case V.PAWN:
6808d7a1 192 if (kp[0] + pawnShift == i && Math.abs(kp[1] - j) == 1)
dac39588
BA
193 return true;
194 break;
195 case V.KNIGHT:
6808d7a1
BA
196 if (
197 (Math.abs(kp[0] - i) == 2 && Math.abs(kp[1] - j) == 1) ||
198 (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 2)
199 ) {
dac39588
BA
200 return true;
201 }
202 break;
203 case V.KING:
204 if (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 1)
205 return true;
206 break;
207 case V.BISHOP:
6808d7a1 208 if (sliderTake([i, j], V.BISHOP)) return true;
dac39588
BA
209 break;
210 case V.ROOK:
6808d7a1 211 if (sliderTake([i, j], V.ROOK)) return true;
dac39588
BA
212 break;
213 case V.QUEEN:
6808d7a1 214 if (sliderTake([i, j], V.BISHOP) || sliderTake([i, j], V.ROOK))
dac39588
BA
215 return true;
216 break;
217 }
218 }
219 }
220 }
221 return false;
222 };
5915f720 223
dac39588 224 let moves = this.getAllValidMoves();
6808d7a1 225 for (let move of moves) {
dac39588 226 this.play(move);
6808d7a1 227 if (this.kingPos[oppCol][0] >= 0 && kingThreats()) {
dac39588
BA
228 // We didn't take opponent king, and our king will be captured: bad
229 move.eval = -maxeval;
230 }
231 this.undo(move);
4f518610 232
6808d7a1 233 if (move.eval) continue;
5915f720 234
dac39588 235 move.eval = 0; //a priori...
5915f720 236
dac39588 237 // Can I take something ? If yes, do it if it seems good...
6808d7a1 238 if (move.vanish.length == 2 && move.vanish[1].c != color) {
71ef1664 239 // OK this isn't a castling move
dac39588
BA
240 const myPieceVal = V.VALUES[move.appear[0].p];
241 const hisPieceVal = V.VALUES[move.vanish[1].p];
71ef1664
BA
242 // Favor captures
243 if (myPieceVal <= hisPieceVal) move.eval = hisPieceVal - myPieceVal + 1;
6808d7a1 244 else {
dac39588
BA
245 // Taking a pawn with minor piece,
246 // or minor piece or pawn with a rook,
247 // or anything but a queen with a queen,
248 // or anything with a king.
71ef1664
BA
249 move.eval = hisPieceVal - myPieceVal;
250 //Math.random() < 0.5 ? 1 : -1;
dac39588
BA
251 }
252 }
253 }
5915f720 254
dac39588
BA
255 // TODO: also need to implement the case when an opponent piece (in light)
256 // is threatening something - maybe not the king, but e.g. pawn takes rook.
5915f720 257
6808d7a1 258 moves.sort((a, b) => b.eval - a.eval);
dac39588 259 let candidates = [0];
6808d7a1 260 for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++)
dac39588
BA
261 candidates.push(j);
262 return moves[candidates[randInt(candidates.length)]];
263 }
6808d7a1 264};