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