Fix Eightpieces, add some simple variants, add a basic variants classification instea...
[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 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
123 // Last move took opponent's king:
124 this.kingPos[move.vanish[1].c] = [move.vanish[1].x, move.vanish[1].y];
125
126 // Update lights for both colors:
127 this.updateEnlightened();
128 }
129
130 getCurrentScore() {
131 const color = this.turn;
132 const kp = this.kingPos[color];
133 if (kp[0] < 0)
134 // King disappeared
135 return color == "w" ? "0-1" : "1-0";
136 // Assume that stalemate is impossible (I think so. Would need proof...)
137 return "*";
138 }
139
140 static get THRESHOLD_MATE() {
141 return 500; //checkmates evals may be slightly below 1000
142 }
143
144 // In this special situation, we just look 1 half move ahead
145 getComputerMove() {
146 const maxeval = V.INFINITY;
147 const color = this.turn;
148 const oppCol = V.GetOppCol(color);
149 const pawnShift = color == "w" ? -1 : 1;
150
151 // Do not cheat: the current enlightment is all we can see
152 const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
153
154 // Can a slider on (i,j) apparently take my king?
155 // NOTE: inaccurate because assume yes if some squares are shadowed
156 const sliderTake = ([i, j], piece) => {
157 const kp = this.kingPos[color];
158 let step = undefined;
159 if (piece == V.BISHOP) {
160 if (Math.abs(kp[0] - i) == Math.abs(kp[1] - j)) {
161 step = [
162 (i - kp[0]) / Math.abs(i - kp[0]),
163 (j - kp[1]) / Math.abs(j - kp[1])
164 ];
165 }
166 } else if (piece == V.ROOK) {
167 if (kp[0] == i) step = [0, (j - kp[1]) / Math.abs(j - kp[1])];
168 else if (kp[1] == j) step = [(i - kp[0]) / Math.abs(i - kp[0]), 0];
169 }
170 if (!step) return false;
171 // Check for obstacles
172 let obstacle = false;
173 for (
174 let x = kp[0] + step[0], y = kp[1] + step[1];
175 x != i && y != j;
176 x += step[0], y += step[1]
177 ) {
178 if (myLight[x][y] && this.board[x][y] != V.EMPTY) {
179 obstacle = true;
180 break;
181 }
182 }
183 if (!obstacle) return true;
184 return false;
185 };
186
187 // Do I see something which can take my king ?
188 const kingThreats = () => {
189 const kp = this.kingPos[color];
190 for (let i = 0; i < V.size.x; i++) {
191 for (let j = 0; j < V.size.y; j++) {
192 if (
193 myLight[i][j] &&
194 this.board[i][j] != V.EMPTY &&
195 this.getColor(i, j) != color
196 ) {
197 switch (this.getPiece(i, j)) {
198 case V.PAWN:
199 if (kp[0] + pawnShift == i && Math.abs(kp[1] - j) == 1)
200 return true;
201 break;
202 case V.KNIGHT:
203 if (
204 (Math.abs(kp[0] - i) == 2 && Math.abs(kp[1] - j) == 1) ||
205 (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 2)
206 ) {
207 return true;
208 }
209 break;
210 case V.KING:
211 if (Math.abs(kp[0] - i) == 1 && Math.abs(kp[1] - j) == 1)
212 return true;
213 break;
214 case V.BISHOP:
215 if (sliderTake([i, j], V.BISHOP)) return true;
216 break;
217 case V.ROOK:
218 if (sliderTake([i, j], V.ROOK)) return true;
219 break;
220 case V.QUEEN:
221 if (sliderTake([i, j], V.BISHOP) || sliderTake([i, j], V.ROOK))
222 return true;
223 break;
224 }
225 }
226 }
227 }
228 return false;
229 };
230
231 let moves = this.getAllValidMoves();
232 for (let move of moves) {
233 this.play(move);
234 if (this.kingPos[oppCol][0] >= 0 && kingThreats()) {
235 // We didn't take opponent king, and our king will be captured: bad
236 move.eval = -maxeval;
237 }
238 this.undo(move);
239
240 if (move.eval) continue;
241
242 move.eval = 0; //a priori...
243
244 // Can I take something ? If yes, do it if it seems good...
245 if (move.vanish.length == 2 && move.vanish[1].c != color) {
246 // OK this isn't a castling move
247 const myPieceVal = V.VALUES[move.appear[0].p];
248 const hisPieceVal = V.VALUES[move.vanish[1].p];
249 // Favor captures
250 if (myPieceVal <= hisPieceVal)
251 move.eval = hisPieceVal - myPieceVal + 1;
252 else {
253 // Taking a pawn with minor piece,
254 // or minor piece or pawn with a rook,
255 // or anything but a queen with a queen,
256 // or anything with a king.
257 move.eval = hisPieceVal - myPieceVal;
258 //Math.random() < 0.5 ? 1 : -1;
259 }
260 }
261 }
262
263 // TODO: also need to implement the case when an opponent piece (in light)
264 // is threatening something - maybe not the king, but e.g. pawn takes rook.
265
266 moves.sort((a, b) => b.eval - a.eval);
267 let candidates = [0];
268 for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++)
269 candidates.push(j);
270 return moves[candidates[randInt(candidates.length)]];
271 }
272 };