Commit | Line | Data |
---|---|---|
0c3fe8a6 | 1 | import { ChessRules } from "@/base_rules"; |
6808d7a1 | 2 | import { ArrayFun } from "@/utils/array"; |
0c3fe8a6 BA |
3 | import { randInt } from "@/utils/alea"; |
4 | ||
32f6285e | 5 | export class DarkRules extends ChessRules { |
20620465 | 6 | // Analyse in Dark mode makes no sense |
8477e53d | 7 | static get CanAnalyze() { |
20620465 BA |
8 | return false; |
9 | } | |
10 | ||
11 | // Moves are revealed only when game ends | |
12 | static get ShowMoves() { | |
13 | return "none"; | |
14 | } | |
15 | ||
00eef1ca BA |
16 | static get SomeHiddenMoves() { |
17 | return true; | |
18 | } | |
19 | ||
6808d7a1 | 20 | setOtherVariables(fen) { |
dac39588 | 21 | super.setOtherVariables(fen); |
6808d7a1 | 22 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
dac39588 | 23 | this.enlightened = { |
6808d7a1 BA |
24 | w: ArrayFun.init(sizeX, sizeY), |
25 | b: ArrayFun.init(sizeX, sizeY) | |
dac39588 BA |
26 | }; |
27 | // Setup enlightened: squares reachable by each side | |
28 | // (TODO: one side would be enough ?) | |
29 | this.updateEnlightened(); | |
30 | } | |
375ecdd1 | 31 | |
6808d7a1 BA |
32 | updateEnlightened() { |
33 | for (let i = 0; i < V.size.x; i++) { | |
34 | for (let j = 0; j < V.size.y; j++) { | |
dac39588 BA |
35 | this.enlightened["w"][i][j] = false; |
36 | this.enlightened["b"][i][j] = false; | |
37 | } | |
38 | } | |
6808d7a1 | 39 | const pawnShift = { w: -1, b: 1 }; |
dac39588 | 40 | // Initialize with pieces positions (which are seen) |
6808d7a1 BA |
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) { | |
2c5d7b20 BA |
44 | const c = this.getColor(i, j); |
45 | this.enlightened[c][i][j] = true; | |
dac39588 | 46 | // Add potential squares visible by "impossible pawn capture" |
6808d7a1 BA |
47 | if (this.getPiece(i, j) == V.PAWN) { |
48 | for (let shiftY of [-1, 1]) { | |
49 | if ( | |
2c5d7b20 BA |
50 | V.OnBoard(i + pawnShift[c], j + shiftY) && |
51 | this.board[i + pawnShift[c]][j + shiftY] == V.EMPTY | |
6808d7a1 | 52 | ) { |
2c5d7b20 | 53 | this.enlightened[c][i + pawnShift[c]][j + shiftY] = true; |
dac39588 BA |
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; | |
d048c4c9 BA |
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 | } | |
dac39588 | 83 | } |
375ecdd1 | 84 | |
c3d16e78 BA |
85 | filterValid(moves) { |
86 | // Used in the interface | |
87 | return moves; | |
88 | } | |
89 | ||
dac39588 | 90 | // Has to be redefined to avoid an infinite loop |
6808d7a1 | 91 | getAllValidMoves() { |
dac39588 | 92 | const color = this.turn; |
dac39588 | 93 | let potentialMoves = []; |
6808d7a1 BA |
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 | ); | |
dac39588 BA |
101 | } |
102 | } | |
103 | return potentialMoves; //because there are no checks | |
104 | } | |
f6dbe8e3 | 105 | |
6808d7a1 | 106 | getCheckSquares() { |
dac39588 BA |
107 | return []; |
108 | } | |
375ecdd1 | 109 | |
3a2a7b5f BA |
110 | postPlay(move) { |
111 | super.postPlay(move); | |
737a5daf | 112 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) |
241bf8f2 | 113 | // We took opponent king (because if castle vanish[1] is a rook) |
6808d7a1 | 114 | this.kingPos[this.turn] = [-1, -1]; |
388e4c40 | 115 | |
dac39588 BA |
116 | // Update lights for both colors: |
117 | this.updateEnlightened(); | |
118 | } | |
388e4c40 | 119 | |
3a2a7b5f BA |
120 | postUndo(move) { |
121 | super.postUndo(move); | |
737a5daf | 122 | if (move.vanish.length == 2 && move.vanish[1].p == V.KING) |
241bf8f2 | 123 | // Last move took opponent's king: |
737a5daf | 124 | this.kingPos[move.vanish[1].c] = [move.vanish[1].x, move.vanish[1].y]; |
388e4c40 | 125 | |
dac39588 BA |
126 | // Update lights for both colors: |
127 | this.updateEnlightened(); | |
128 | } | |
375ecdd1 | 129 | |
6808d7a1 | 130 | getCurrentScore() { |
dac39588 BA |
131 | const color = this.turn; |
132 | const kp = this.kingPos[color]; | |
6808d7a1 | 133 | if (kp[0] < 0) |
241bf8f2 | 134 | // King disappeared |
6808d7a1 | 135 | return color == "w" ? "0-1" : "1-0"; |
241bf8f2 BA |
136 | // Assume that stalemate is impossible (I think so. Would need proof...) |
137 | return "*"; | |
dac39588 | 138 | } |
375ecdd1 | 139 | |
6808d7a1 | 140 | static get THRESHOLD_MATE() { |
dac39588 BA |
141 | return 500; //checkmates evals may be slightly below 1000 |
142 | } | |
5915f720 | 143 | |
dac39588 | 144 | // In this special situation, we just look 1 half move ahead |
6808d7a1 | 145 | getComputerMove() { |
dac39588 BA |
146 | const maxeval = V.INFINITY; |
147 | const color = this.turn; | |
148 | const oppCol = V.GetOppCol(color); | |
6808d7a1 | 149 | const pawnShift = color == "w" ? -1 : 1; |
5915f720 | 150 | |
dac39588 BA |
151 | // Do not cheat: the current enlightment is all we can see |
152 | const myLight = JSON.parse(JSON.stringify(this.enlightened[color])); | |
5915f720 | 153 | |
dac39588 BA |
154 | // Can a slider on (i,j) apparently take my king? |
155 | // NOTE: inaccurate because assume yes if some squares are shadowed | |
6808d7a1 | 156 | const sliderTake = ([i, j], piece) => { |
dac39588 BA |
157 | const kp = this.kingPos[color]; |
158 | let step = undefined; | |
6808d7a1 BA |
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]) | |
dac39588 BA |
164 | ]; |
165 | } | |
6808d7a1 BA |
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]; | |
dac39588 | 169 | } |
6808d7a1 | 170 | if (!step) return false; |
dac39588 BA |
171 | // Check for obstacles |
172 | let obstacle = false; | |
173 | for ( | |
6808d7a1 | 174 | let x = kp[0] + step[0], y = kp[1] + step[1]; |
dac39588 | 175 | x != i && y != j; |
6808d7a1 BA |
176 | x += step[0], y += step[1] |
177 | ) { | |
178 | if (myLight[x][y] && this.board[x][y] != V.EMPTY) { | |
dac39588 BA |
179 | obstacle = true; |
180 | break; | |
181 | } | |
182 | } | |
6808d7a1 | 183 | if (!obstacle) return true; |
dac39588 BA |
184 | return false; |
185 | }; | |
5915f720 | 186 | |
dac39588 BA |
187 | // Do I see something which can take my king ? |
188 | const kingThreats = () => { | |
189 | const kp = this.kingPos[color]; | |
6808d7a1 BA |
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)) { | |
dac39588 | 198 | case V.PAWN: |
6808d7a1 | 199 | if (kp[0] + pawnShift == i && Math.abs(kp[1] - j) == 1) |
dac39588 BA |
200 | return true; |
201 | break; | |
202 | case V.KNIGHT: | |
6808d7a1 BA |
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 | ) { | |
dac39588 BA |
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: | |
6808d7a1 | 215 | if (sliderTake([i, j], V.BISHOP)) return true; |
dac39588 BA |
216 | break; |
217 | case V.ROOK: | |
6808d7a1 | 218 | if (sliderTake([i, j], V.ROOK)) return true; |
dac39588 BA |
219 | break; |
220 | case V.QUEEN: | |
6808d7a1 | 221 | if (sliderTake([i, j], V.BISHOP) || sliderTake([i, j], V.ROOK)) |
dac39588 BA |
222 | return true; |
223 | break; | |
224 | } | |
225 | } | |
226 | } | |
227 | } | |
228 | return false; | |
229 | }; | |
5915f720 | 230 | |
dac39588 | 231 | let moves = this.getAllValidMoves(); |
6808d7a1 | 232 | for (let move of moves) { |
dac39588 | 233 | this.play(move); |
6808d7a1 | 234 | if (this.kingPos[oppCol][0] >= 0 && kingThreats()) { |
dac39588 BA |
235 | // We didn't take opponent king, and our king will be captured: bad |
236 | move.eval = -maxeval; | |
237 | } | |
238 | this.undo(move); | |
4f518610 | 239 | |
6808d7a1 | 240 | if (move.eval) continue; |
5915f720 | 241 | |
dac39588 | 242 | move.eval = 0; //a priori... |
5915f720 | 243 | |
dac39588 | 244 | // Can I take something ? If yes, do it if it seems good... |
6808d7a1 | 245 | if (move.vanish.length == 2 && move.vanish[1].c != color) { |
71ef1664 | 246 | // OK this isn't a castling move |
dac39588 BA |
247 | const myPieceVal = V.VALUES[move.appear[0].p]; |
248 | const hisPieceVal = V.VALUES[move.vanish[1].p]; | |
71ef1664 | 249 | // Favor captures |
2c5d7b20 BA |
250 | if (myPieceVal <= hisPieceVal) |
251 | move.eval = hisPieceVal - myPieceVal + 1; | |
6808d7a1 | 252 | else { |
dac39588 BA |
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. | |
71ef1664 BA |
257 | move.eval = hisPieceVal - myPieceVal; |
258 | //Math.random() < 0.5 ? 1 : -1; | |
dac39588 BA |
259 | } |
260 | } | |
261 | } | |
5915f720 | 262 | |
dac39588 BA |
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. | |
5915f720 | 265 | |
6808d7a1 | 266 | moves.sort((a, b) => b.eval - a.eval); |
dac39588 | 267 | let candidates = [0]; |
6808d7a1 | 268 | for (let j = 1; j < moves.length && moves[j].eval == moves[0].eval; j++) |
dac39588 BA |
269 | candidates.push(j); |
270 | return moves[candidates[randInt(candidates.length)]]; | |
271 | } | |
6808d7a1 | 272 | }; |