6cf3b6787996314abc0ac38c0a0292db7ad50c59
[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 const VariantRules = 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 setOtherVariables(fen) {
17 super.setOtherVariables(fen);
18 const [sizeX, sizeY] = [V.size.x, V.size.y];
19 this.enlightened = {
20 w: ArrayFun.init(sizeX, sizeY),
21 b: ArrayFun.init(sizeX, sizeY)
22 };
23 // Setup enlightened: squares reachable by each side
24 // (TODO: one side would be enough ?)
25 this.updateEnlightened();
26 }
27
28 updateEnlightened() {
29 for (let i = 0; i < V.size.x; i++) {
30 for (let j = 0; j < V.size.y; j++) {
31 this.enlightened["w"][i][j] = false;
32 this.enlightened["b"][i][j] = false;
33 }
34 }
35 const pawnShift = { w: -1, b: 1 };
36 // Initialize with pieces positions (which are seen)
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);
41 this.enlightened[color][i][j] = true;
42 // Add potential squares visible by "impossible pawn capture"
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 ) {
49 this.enlightened[color][i + pawnShift[color]][j + shiftY] = true;
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;
66 }
67
68 // Has to be redefined to avoid an infinite loop
69 getAllValidMoves() {
70 const color = this.turn;
71 let potentialMoves = [];
72 for (let i = 0; i < V.size.x; i++) {
73 for (let j = 0; j < V.size.y; j++) {
74 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color)
75 Array.prototype.push.apply(
76 potentialMoves,
77 this.getPotentialMovesFrom([i, j])
78 );
79 }
80 }
81 return potentialMoves; //because there are no checks
82 }
83
84 getCheckSquares() {
85 return [];
86 }
87
88 updateVariables(move) {
89 super.updateVariables(move);
90 if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
91 // We took opponent king (because if castle vanish[1] is a rook)
92 this.kingPos[this.turn] = [-1, -1];
93
94 // Update lights for both colors:
95 this.updateEnlightened();
96 }
97
98 unupdateVariables(move) {
99 super.unupdateVariables(move);
100 const c = move.vanish[0].c;
101 const oppCol = V.GetOppCol(c);
102 if (this.kingPos[oppCol][0] < 0)
103 // Last move took opponent's king:
104 this.kingPos[oppCol] = [move.vanish[1].x, move.vanish[1].y];
105
106 // Update lights for both colors:
107 this.updateEnlightened();
108 }
109
110 getCurrentScore() {
111 const color = this.turn;
112 const kp = this.kingPos[color];
113 if (kp[0] < 0)
114 // King disappeared
115 return color == "w" ? "0-1" : "1-0";
116 // Assume that stalemate is impossible (I think so. Would need proof...)
117 return "*";
118 }
119
120 static get THRESHOLD_MATE() {
121 return 500; //checkmates evals may be slightly below 1000
122 }
123
124 // In this special situation, we just look 1 half move ahead
125 getComputerMove() {
126 const maxeval = V.INFINITY;
127 const color = this.turn;
128 const oppCol = V.GetOppCol(color);
129 const pawnShift = color == "w" ? -1 : 1;
130
131 // Do not cheat: the current enlightment is all we can see
132 const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
133
134 // Can a slider on (i,j) apparently take my king?
135 // NOTE: inaccurate because assume yes if some squares are shadowed
136 const sliderTake = ([i, j], piece) => {
137 const kp = this.kingPos[color];
138 let step = undefined;
139 if (piece == V.BISHOP) {
140 if (Math.abs(kp[0] - i) == Math.abs(kp[1] - j)) {
141 step = [
142 (i - kp[0]) / Math.abs(i - kp[0]),
143 (j - kp[1]) / Math.abs(j - kp[1])
144 ];
145 }
146 } else if (piece == V.ROOK) {
147 if (kp[0] == i) step = [0, (j - kp[1]) / Math.abs(j - kp[1])];
148 else if (kp[1] == j) step = [(i - kp[0]) / Math.abs(i - kp[0]), 0];
149 }
150 if (!step) return false;
151 // Check for obstacles
152 let obstacle = false;
153 for (
154 let x = kp[0] + step[0], y = kp[1] + step[1];
155 x != i && y != j;
156 x += step[0], y += step[1]
157 ) {
158 if (myLight[x][y] && this.board[x][y] != V.EMPTY) {
159 obstacle = true;
160 break;
161 }
162 }
163 if (!obstacle) return true;
164 return false;
165 };
166
167 // Do I see something which can take my king ?
168 const kingThreats = () => {
169 const kp = this.kingPos[color];
170 for (let i = 0; i < V.size.x; i++) {
171 for (let j = 0; j < V.size.y; j++) {
172 if (
173 myLight[i][j] &&
174 this.board[i][j] != V.EMPTY &&
175 this.getColor(i, j) != color
176 ) {
177 switch (this.getPiece(i, j)) {
178 case V.PAWN:
179 if (kp[0] + pawnShift == i && Math.abs(kp[1] - j) == 1)
180 return true;
181 break;
182 case V.KNIGHT:
183 if (
184 (Math.abs(kp[0] - i) == 2 && Math.abs(kp[1] - j) == 1) ||
185 (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 2)
186 ) {
187 return true;
188 }
189 break;
190 case V.KING:
191 if (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 1)
192 return true;
193 break;
194 case V.BISHOP:
195 if (sliderTake([i, j], V.BISHOP)) return true;
196 break;
197 case V.ROOK:
198 if (sliderTake([i, j], V.ROOK)) return true;
199 break;
200 case V.QUEEN:
201 if (sliderTake([i, j], V.BISHOP) || sliderTake([i, j], V.ROOK))
202 return true;
203 break;
204 }
205 }
206 }
207 }
208 return false;
209 };
210
211 let moves = this.getAllValidMoves();
212 for (let move of moves) {
213 this.play(move);
214 if (this.kingPos[oppCol][0] >= 0 && kingThreats()) {
215 // We didn't take opponent king, and our king will be captured: bad
216 move.eval = -maxeval;
217 }
218 this.undo(move);
219
220 if (move.eval) continue;
221
222 move.eval = 0; //a priori...
223
224 // Can I take something ? If yes, do it if it seems good...
225 if (move.vanish.length == 2 && move.vanish[1].c != color) {
226 // OK this isn't a castling move
227 const myPieceVal = V.VALUES[move.appear[0].p];
228 const hisPieceVal = V.VALUES[move.vanish[1].p];
229 // Favor captures
230 if (myPieceVal <= hisPieceVal) move.eval = hisPieceVal - myPieceVal + 1;
231 else {
232 // Taking a pawn with minor piece,
233 // or minor piece or pawn with a rook,
234 // or anything but a queen with a queen,
235 // or anything with a king.
236 move.eval = hisPieceVal - myPieceVal;
237 //Math.random() < 0.5 ? 1 : -1;
238 }
239 }
240 }
241
242 // TODO: also need to implement the case when an opponent piece (in light)
243 // is threatening something - maybe not the king, but e.g. pawn takes rook.
244
245 moves.sort((a, b) => b.eval - a.eval);
246 let candidates = [0];
247 for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++)
248 candidates.push(j);
249 return moves[candidates[randInt(candidates.length)]];
250 }
251 };