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