Add Tencubed and Omega variants + some fixes (updateCastleFlags()) + cleaner FEN...
[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
32f6285e 5export 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) {
2c5d7b20
BA
40 const c = this.getColor(i, j);
41 this.enlightened[c][i][j] = true;
dac39588 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 (
2c5d7b20
BA
46 V.OnBoard(i + pawnShift[c], j + shiftY) &&
47 this.board[i + pawnShift[c]][j + shiftY] == V.EMPTY
6808d7a1 48 ) {
2c5d7b20 49 this.enlightened[c][i + pawnShift[c]][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
c3d16e78
BA
81 filterValid(moves) {
82 // Used in the interface
83 return moves;
84 }
85
dac39588 86 // Has to be redefined to avoid an infinite loop
6808d7a1 87 getAllValidMoves() {
dac39588 88 const color = this.turn;
dac39588 89 let potentialMoves = [];
6808d7a1
BA
90 for (let i = 0; i < V.size.x; i++) {
91 for (let j = 0; j < V.size.y; j++) {
92 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color)
93 Array.prototype.push.apply(
94 potentialMoves,
95 this.getPotentialMovesFrom([i, j])
96 );
dac39588
BA
97 }
98 }
99 return potentialMoves; //because there are no checks
100 }
f6dbe8e3 101
6808d7a1 102 getCheckSquares() {
dac39588
BA
103 return [];
104 }
375ecdd1 105
3a2a7b5f
BA
106 postPlay(move) {
107 super.postPlay(move);
241bf8f2
BA
108 if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
109 // We took opponent king (because if castle vanish[1] is a rook)
6808d7a1 110 this.kingPos[this.turn] = [-1, -1];
388e4c40 111
dac39588
BA
112 // Update lights for both colors:
113 this.updateEnlightened();
114 }
388e4c40 115
3a2a7b5f
BA
116 postUndo(move) {
117 super.postUndo(move);
dac39588
BA
118 const c = move.vanish[0].c;
119 const oppCol = V.GetOppCol(c);
241bf8f2
BA
120 if (this.kingPos[oppCol][0] < 0)
121 // Last move took opponent's king:
122 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
388e4c40 123
dac39588
BA
124 // Update lights for both colors:
125 this.updateEnlightened();
126 }
375ecdd1 127
6808d7a1 128 getCurrentScore() {
dac39588
BA
129 const color = this.turn;
130 const kp = this.kingPos[color];
6808d7a1 131 if (kp[0] < 0)
241bf8f2 132 // King disappeared
6808d7a1 133 return color == "w" ? "0-1" : "1-0";
241bf8f2
BA
134 // Assume that stalemate is impossible (I think so. Would need proof...)
135 return "*";
dac39588 136 }
375ecdd1 137
6808d7a1 138 static get THRESHOLD_MATE() {
dac39588
BA
139 return 500; //checkmates evals may be slightly below 1000
140 }
5915f720 141
dac39588 142 // In this special situation, we just look 1 half move ahead
6808d7a1 143 getComputerMove() {
dac39588
BA
144 const maxeval = V.INFINITY;
145 const color = this.turn;
146 const oppCol = V.GetOppCol(color);
6808d7a1 147 const pawnShift = color == "w" ? -1 : 1;
5915f720 148
dac39588
BA
149 // Do not cheat: the current enlightment is all we can see
150 const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
5915f720 151
dac39588
BA
152 // Can a slider on (i,j) apparently take my king?
153 // NOTE: inaccurate because assume yes if some squares are shadowed
6808d7a1 154 const sliderTake = ([i, j], piece) => {
dac39588
BA
155 const kp = this.kingPos[color];
156 let step = undefined;
6808d7a1
BA
157 if (piece == V.BISHOP) {
158 if (Math.abs(kp[0] - i) == Math.abs(kp[1] - j)) {
159 step = [
160 (i - kp[0]) / Math.abs(i - kp[0]),
161 (j - kp[1]) / Math.abs(j - kp[1])
dac39588
BA
162 ];
163 }
6808d7a1
BA
164 } else if (piece == V.ROOK) {
165 if (kp[0] == i) step = [0, (j - kp[1]) / Math.abs(j - kp[1])];
166 else if (kp[1] == j) step = [(i - kp[0]) / Math.abs(i - kp[0]), 0];
dac39588 167 }
6808d7a1 168 if (!step) return false;
dac39588
BA
169 // Check for obstacles
170 let obstacle = false;
171 for (
6808d7a1 172 let x = kp[0] + step[0], y = kp[1] + step[1];
dac39588 173 x != i && y != j;
6808d7a1
BA
174 x += step[0], y += step[1]
175 ) {
176 if (myLight[x][y] && this.board[x][y] != V.EMPTY) {
dac39588
BA
177 obstacle = true;
178 break;
179 }
180 }
6808d7a1 181 if (!obstacle) return true;
dac39588
BA
182 return false;
183 };
5915f720 184
dac39588
BA
185 // Do I see something which can take my king ?
186 const kingThreats = () => {
187 const kp = this.kingPos[color];
6808d7a1
BA
188 for (let i = 0; i < V.size.x; i++) {
189 for (let j = 0; j < V.size.y; j++) {
190 if (
191 myLight[i][j] &&
192 this.board[i][j] != V.EMPTY &&
193 this.getColor(i, j) != color
194 ) {
195 switch (this.getPiece(i, j)) {
dac39588 196 case V.PAWN:
6808d7a1 197 if (kp[0] + pawnShift == i && Math.abs(kp[1] - j) == 1)
dac39588
BA
198 return true;
199 break;
200 case V.KNIGHT:
6808d7a1
BA
201 if (
202 (Math.abs(kp[0] - i) == 2 && Math.abs(kp[1] - j) == 1) ||
203 (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 2)
204 ) {
dac39588
BA
205 return true;
206 }
207 break;
208 case V.KING:
209 if (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 1)
210 return true;
211 break;
212 case V.BISHOP:
6808d7a1 213 if (sliderTake([i, j], V.BISHOP)) return true;
dac39588
BA
214 break;
215 case V.ROOK:
6808d7a1 216 if (sliderTake([i, j], V.ROOK)) return true;
dac39588
BA
217 break;
218 case V.QUEEN:
6808d7a1 219 if (sliderTake([i, j], V.BISHOP) || sliderTake([i, j], V.ROOK))
dac39588
BA
220 return true;
221 break;
222 }
223 }
224 }
225 }
226 return false;
227 };
5915f720 228
dac39588 229 let moves = this.getAllValidMoves();
6808d7a1 230 for (let move of moves) {
dac39588 231 this.play(move);
6808d7a1 232 if (this.kingPos[oppCol][0] >= 0 && kingThreats()) {
dac39588
BA
233 // We didn't take opponent king, and our king will be captured: bad
234 move.eval = -maxeval;
235 }
236 this.undo(move);
4f518610 237
6808d7a1 238 if (move.eval) continue;
5915f720 239
dac39588 240 move.eval = 0; //a priori...
5915f720 241
dac39588 242 // Can I take something ? If yes, do it if it seems good...
6808d7a1 243 if (move.vanish.length == 2 && move.vanish[1].c != color) {
71ef1664 244 // OK this isn't a castling move
dac39588
BA
245 const myPieceVal = V.VALUES[move.appear[0].p];
246 const hisPieceVal = V.VALUES[move.vanish[1].p];
71ef1664 247 // Favor captures
2c5d7b20
BA
248 if (myPieceVal <= hisPieceVal)
249 move.eval = hisPieceVal - myPieceVal + 1;
6808d7a1 250 else {
dac39588
BA
251 // Taking a pawn with minor piece,
252 // or minor piece or pawn with a rook,
253 // or anything but a queen with a queen,
254 // or anything with a king.
71ef1664
BA
255 move.eval = hisPieceVal - myPieceVal;
256 //Math.random() < 0.5 ? 1 : -1;
dac39588
BA
257 }
258 }
259 }
5915f720 260
dac39588
BA
261 // TODO: also need to implement the case when an opponent piece (in light)
262 // is threatening something - maybe not the king, but e.g. pawn takes rook.
5915f720 263
6808d7a1 264 moves.sort((a, b) => b.eval - a.eval);
dac39588 265 let candidates = [0];
6808d7a1 266 for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++)
dac39588
BA
267 candidates.push(j);
268 return moves[candidates[randInt(candidates.length)]];
269 }
6808d7a1 270};