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