Commit | Line | Data |
---|---|---|
afbf3ca7 | 1 | import { randInt } from "@/utils/alea"; |
2a8a94c9 BA |
2 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
3 | ||
32f6285e | 4 | export class EightpiecesRules extends ChessRules { |
7e8a7ea1 | 5 | |
2a8a94c9 BA |
6 | static get JAILER() { |
7 | return "j"; | |
8 | } | |
9 | static get SENTRY() { | |
10 | return "s"; | |
11 | } | |
12 | static get LANCER() { | |
13 | return "l"; | |
14 | } | |
15 | ||
14edde72 BA |
16 | static get IMAGE_EXTENSION() { |
17 | // Temporarily, for the time SVG pieces are being designed: | |
18 | return ".png"; | |
19 | } | |
20 | ||
ee307044 | 21 | // Lancer directions *from white perspective* |
28b32b4f BA |
22 | static get LANCER_DIRS() { |
23 | return { | |
24 | 'c': [-1, 0], //north | |
25 | 'd': [-1, 1], //N-E | |
26 | 'e': [0, 1], //east | |
27 | 'f': [1, 1], //S-E | |
28 | 'g': [1, 0], //south | |
29 | 'h': [1, -1], //S-W | |
30 | 'm': [0, -1], //west | |
31 | 'o': [-1, -1] //N-W | |
32 | }; | |
33 | } | |
34 | ||
3a2a7b5f BA |
35 | static get PIECES() { |
36 | return ChessRules.PIECES | |
37 | .concat([V.JAILER, V.SENTRY]) | |
38 | .concat(Object.keys(V.LANCER_DIRS)); | |
39 | } | |
40 | ||
28b32b4f BA |
41 | getPiece(i, j) { |
42 | const piece = this.board[i][j].charAt(1); | |
43 | // Special lancer case: 8 possible orientations | |
44 | if (Object.keys(V.LANCER_DIRS).includes(piece)) return V.LANCER; | |
45 | return piece; | |
46 | } | |
47 | ||
3a2a7b5f | 48 | getPpath(b, color, score, orientation) { |
14edde72 | 49 | if ([V.JAILER, V.SENTRY].includes(b[1])) return "Eightpieces/tmp_png/" + b; |
3a2a7b5f | 50 | if (Object.keys(V.LANCER_DIRS).includes(b[1])) { |
14edde72 | 51 | if (orientation == 'w') return "Eightpieces/tmp_png/" + b; |
3a2a7b5f BA |
52 | // Find opposite direction for adequate display: |
53 | let oppDir = ''; | |
54 | switch (b[1]) { | |
55 | case 'c': | |
56 | oppDir = 'g'; | |
57 | break; | |
58 | case 'g': | |
59 | oppDir = 'c'; | |
60 | break; | |
61 | case 'd': | |
62 | oppDir = 'h'; | |
63 | break; | |
64 | case 'h': | |
65 | oppDir = 'd'; | |
66 | break; | |
67 | case 'e': | |
68 | oppDir = 'm'; | |
69 | break; | |
70 | case 'm': | |
71 | oppDir = 'e'; | |
72 | break; | |
73 | case 'f': | |
74 | oppDir = 'o'; | |
75 | break; | |
76 | case 'o': | |
77 | oppDir = 'f'; | |
78 | break; | |
79 | } | |
14edde72 | 80 | return "Eightpieces/tmp_png/" + b[0] + oppDir; |
3a2a7b5f | 81 | } |
14edde72 BA |
82 | // TODO: after we have SVG pieces, remove the folder and next prefix: |
83 | return "Eightpieces/tmp_png/" + b; | |
90e814b6 BA |
84 | } |
85 | ||
c7550017 BA |
86 | getPPpath(m, orientation) { |
87 | return ( | |
88 | this.getPpath( | |
89 | m.appear[0].c + m.appear[0].p, | |
90 | null, | |
91 | null, | |
92 | orientation | |
93 | ) | |
94 | ); | |
3a2a7b5f BA |
95 | } |
96 | ||
90e814b6 BA |
97 | static ParseFen(fen) { |
98 | const fenParts = fen.split(" "); | |
3a2a7b5f BA |
99 | return Object.assign( |
100 | ChessRules.ParseFen(fen), | |
101 | { sentrypush: fenParts[5] } | |
102 | ); | |
103 | } | |
104 | ||
105 | static IsGoodFen(fen) { | |
106 | if (!ChessRules.IsGoodFen(fen)) return false; | |
107 | const fenParsed = V.ParseFen(fen); | |
108 | // 5) Check sentry push (if any) | |
109 | if ( | |
110 | fenParsed.sentrypush != "-" && | |
61656127 | 111 | !fenParsed.sentrypush.match(/^([a-h][1-8]){2,2}$/) |
3a2a7b5f BA |
112 | ) { |
113 | return false; | |
114 | } | |
115 | return true; | |
90e814b6 BA |
116 | } |
117 | ||
118 | getFen() { | |
6b7b2cf7 | 119 | return super.getFen() + " " + this.getSentrypushFen(); |
90e814b6 BA |
120 | } |
121 | ||
122 | getFenForRepeat() { | |
6b7b2cf7 | 123 | return super.getFenForRepeat() + "_" + this.getSentrypushFen(); |
90e814b6 BA |
124 | } |
125 | ||
6b7b2cf7 BA |
126 | getSentrypushFen() { |
127 | const L = this.sentryPush.length; | |
128 | if (!this.sentryPush[L-1]) return "-"; | |
90e814b6 | 129 | let res = ""; |
0a9cef13 BA |
130 | const spL = this.sentryPush[L-1].length; |
131 | // Condensate path: just need initial and final squares: | |
132 | return [0, spL - 1] | |
133 | .map(i => V.CoordsToSquare(this.sentryPush[L-1][i])) | |
61656127 | 134 | .join(""); |
2a8a94c9 BA |
135 | } |
136 | ||
28b32b4f BA |
137 | setOtherVariables(fen) { |
138 | super.setOtherVariables(fen); | |
139 | // subTurn == 2 only when a sentry moved, and is about to push something | |
140 | this.subTurn = 1; | |
afbf3ca7 BA |
141 | // Sentry position just after a "capture" (subTurn from 1 to 2) |
142 | this.sentryPos = null; | |
28b32b4f | 143 | // Stack pieces' forbidden squares after a sentry move at each turn |
90e814b6 | 144 | const parsedFen = V.ParseFen(fen); |
6b7b2cf7 | 145 | if (parsedFen.sentrypush == "-") this.sentryPush = [null]; |
90e814b6 | 146 | else { |
0a9cef13 | 147 | // Expand init + dest squares into a full path: |
61656127 BA |
148 | const init = V.SquareToCoords(parsedFen.sentrypush.substr(0, 2)), |
149 | dest = V.SquareToCoords(parsedFen.sentrypush.substr(2)); | |
0a9cef13 BA |
150 | let newPath = [init]; |
151 | const delta = ['x', 'y'].map(i => Math.abs(dest[i] - init[i])); | |
152 | // Check that it's not a knight movement: | |
153 | if (delta[0] == 0 || delta[1] == 0 || delta[0] == delta[1]) { | |
154 | const step = ['x', 'y'].map((i, idx) => { | |
155 | return (dest[i] - init[i]) / delta[idx] || 0 | |
156 | }); | |
157 | let x = init.x + step[0], | |
158 | y = init.y + step[1]; | |
159 | while (x != dest.x || y != dest.y) { | |
160 | newPath.push({ x: x, y: y }); | |
161 | x += step[0]; | |
162 | y += step[1]; | |
163 | } | |
164 | } | |
165 | newPath.push(dest); | |
166 | this.sentryPush = [newPath]; | |
90e814b6 | 167 | } |
2a8a94c9 BA |
168 | } |
169 | ||
4313762d BA |
170 | static GenRandInitFen(options) { |
171 | if (options.randomness == 0) | |
d54f6261 | 172 | return "jfsqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JDSQKBNR w 0 ahah - -"; |
9842aca2 | 173 | |
4313762d | 174 | const baseFen = ChessRules.GenRandInitFen(options); |
0fb43db7 BA |
175 | const fenParts = baseFen.split(' '); |
176 | const posParts = fenParts[0].split('/'); | |
9842aca2 | 177 | |
0fb43db7 BA |
178 | // Replace one bishop by sentry, so that sentries on different colors |
179 | // Also replace one random rook by jailer, | |
180 | // and one random knight by lancer (facing north/south) | |
9842aca2 | 181 | |
0fb43db7 BA |
182 | // "replaced" array contains -2 initially, then either -1 if skipped, |
183 | // or (eventually) the index of replacement: | |
184 | let newPos = { 0: "", 7: "" }; | |
185 | let sentryOddity = -1; | |
186 | for (let rank of [0, 7]) { | |
187 | let replaced = { 'b': -2, 'n': -2, 'r': -2 }; | |
188 | for (let i = 0; i < 8; i++) { | |
189 | const curChar = posParts[rank].charAt(i).toLowerCase(); | |
190 | if (['b', 'n', 'r'].includes(curChar)) { | |
191 | if ( | |
192 | replaced[curChar] == -1 || | |
193 | (curChar == 'b' && rank == 7 && i % 2 == sentryOddity) || | |
194 | ( | |
195 | (curChar != 'b' || rank == 0) && | |
196 | replaced[curChar] == -2 && | |
197 | randInt(2) == 0 | |
198 | ) | |
199 | ) { | |
200 | replaced[curChar] = i; | |
201 | if (curChar == 'b') { | |
202 | if (sentryOddity < 0) sentryOddity = i % 2; | |
203 | newPos[rank] += 's'; | |
204 | } | |
205 | else if (curChar == 'r') newPos[rank] += 'j'; | |
206 | else | |
207 | // Lancer: orientation depends on side | |
208 | newPos[rank] += (rank == 0 ? 'g' : 'c'); | |
209 | } | |
210 | else { | |
211 | if (replaced[curChar] == -2) replaced[curChar]++; | |
212 | newPos[rank] += curChar; | |
213 | } | |
214 | } | |
215 | else newPos[rank] += curChar; | |
9842aca2 | 216 | } |
9842aca2 | 217 | } |
0fb43db7 | 218 | |
9842aca2 | 219 | return ( |
0fb43db7 BA |
220 | newPos[0] + "/" + posParts.slice(1, 7).join('/') + "/" + |
221 | newPos[7].toUpperCase() + " " + fenParts.slice(1, 5).join(' ') + " -" | |
9842aca2 | 222 | ); |
2a8a94c9 BA |
223 | } |
224 | ||
b0a0468a BA |
225 | canTake([x1, y1], [x2, y2]) { |
226 | if (this.subTurn == 2) | |
227 | // Only self captures on this subturn: | |
228 | return this.getColor(x1, y1) == this.getColor(x2, y2); | |
229 | return super.canTake([x1, y1], [x2, y2]); | |
230 | } | |
231 | ||
6b7b2cf7 BA |
232 | // Is piece on square (x,y) immobilized? |
233 | isImmobilized([x, y]) { | |
234 | const color = this.getColor(x, y); | |
235 | const oppCol = V.GetOppCol(color); | |
236 | for (let step of V.steps[V.ROOK]) { | |
237 | const [i, j] = [x + step[0], y + step[1]]; | |
238 | if ( | |
239 | V.OnBoard(i, j) && | |
240 | this.board[i][j] != V.EMPTY && | |
241 | this.getColor(i, j) == oppCol | |
242 | ) { | |
bfed878d | 243 | if (this.getPiece(i, j) == V.JAILER) return [i, j]; |
6b7b2cf7 BA |
244 | } |
245 | } | |
246 | return null; | |
247 | } | |
248 | ||
bfed878d BA |
249 | canIplay(side, [x, y]) { |
250 | return ( | |
2c5d7b20 BA |
251 | (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side) |
252 | || | |
bfed878d BA |
253 | (this.subTurn == 2 && x == this.sentryPos.x && y == this.sentryPos.y) |
254 | ); | |
255 | } | |
256 | ||
3a2a7b5f | 257 | getPotentialMovesFrom([x, y]) { |
d9a7a1e4 BA |
258 | const piece = this.getPiece(x, y); |
259 | const L = this.sentryPush.length; | |
1b56b736 | 260 | // At subTurn == 2, jailers aren't effective (Jeff K) |
3a2a7b5f BA |
261 | if (this.subTurn == 1) { |
262 | const jsq = this.isImmobilized([x, y]); | |
263 | if (!!jsq) { | |
264 | let moves = []; | |
265 | // Special pass move if king: | |
d9a7a1e4 | 266 | if (piece == V.KING) { |
3a2a7b5f BA |
267 | moves.push( |
268 | new Move({ | |
269 | appear: [], | |
270 | vanish: [], | |
271 | start: { x: x, y: y }, | |
272 | end: { x: jsq[0], y: jsq[1] } | |
273 | }) | |
274 | ); | |
275 | } | |
d9a7a1e4 BA |
276 | else if (piece == V.LANCER && !!this.sentryPush[L-1]) { |
277 | // A pushed lancer next to the jailer: reorient | |
278 | const color = this.getColor(x, y); | |
279 | const curDir = this.board[x][y].charAt(1); | |
280 | Object.keys(V.LANCER_DIRS).forEach(k => { | |
281 | moves.push( | |
282 | new Move({ | |
283 | appear: [{ x: x, y: y, c: color, p: k }], | |
284 | vanish: [{ x: x, y: y, c: color, p: curDir }], | |
285 | start: { x: x, y: y }, | |
286 | end: { x: jsq[0], y: jsq[1] } | |
287 | }) | |
288 | ); | |
289 | }); | |
290 | } | |
3a2a7b5f BA |
291 | return moves; |
292 | } | |
293 | } | |
bfed878d | 294 | let moves = []; |
d9a7a1e4 | 295 | switch (piece) { |
6b7b2cf7 | 296 | case V.JAILER: |
bfed878d BA |
297 | moves = this.getPotentialJailerMoves([x, y]); |
298 | break; | |
6b7b2cf7 | 299 | case V.SENTRY: |
bfed878d BA |
300 | moves = this.getPotentialSentryMoves([x, y]); |
301 | break; | |
13102cab | 302 | case V.LANCER: |
bfed878d BA |
303 | moves = this.getPotentialLancerMoves([x, y]); |
304 | break; | |
6b7b2cf7 | 305 | default: |
bfed878d BA |
306 | moves = super.getPotentialMovesFrom([x, y]); |
307 | break; | |
6b7b2cf7 | 308 | } |
bfed878d | 309 | if (!!this.sentryPush[L-1]) { |
d9a7a1e4 BA |
310 | // Delete moves walking back on sentry push path, |
311 | // only if not a pawn, and the piece is the pushed one. | |
312 | const pl = this.sentryPush[L-1].length; | |
313 | const finalPushedSq = this.sentryPush[L-1][pl-1]; | |
bfed878d BA |
314 | moves = moves.filter(m => { |
315 | if ( | |
316 | m.vanish[0].p != V.PAWN && | |
d9a7a1e4 | 317 | m.start.x == finalPushedSq.x && m.start.y == finalPushedSq.y && |
bfed878d BA |
318 | this.sentryPush[L-1].some(sq => sq.x == m.end.x && sq.y == m.end.y) |
319 | ) { | |
320 | return false; | |
321 | } | |
322 | return true; | |
323 | }); | |
0fb43db7 BA |
324 | } |
325 | else if (this.subTurn == 2) { | |
b0a0468a BA |
326 | // Put back the sentinel on board: |
327 | const color = this.turn; | |
bfed878d | 328 | moves.forEach(m => { |
b0a0468a | 329 | m.appear.push({x: x, y: y, p: V.SENTRY, c: color}); |
bfed878d BA |
330 | }); |
331 | } | |
332 | return moves; | |
6b7b2cf7 BA |
333 | } |
334 | ||
ee307044 | 335 | getPotentialPawnMoves([x, y]) { |
bfed878d | 336 | const color = this.getColor(x, y); |
ee307044 BA |
337 | let moves = []; |
338 | const [sizeX, sizeY] = [V.size.x, V.size.y]; | |
89781a55 | 339 | let shiftX = (color == "w" ? -1 : 1); |
b0a0468a | 340 | if (this.subTurn == 2) shiftX *= -1; |
89781a55 | 341 | const firstRank = color == "w" ? sizeX - 1 : 0; |
ee307044 BA |
342 | const startRank = color == "w" ? sizeX - 2 : 1; |
343 | const lastRank = color == "w" ? 0 : sizeX - 1; | |
344 | ||
b0a0468a BA |
345 | // Pawns might be pushed on 1st rank and attempt to move again: |
346 | if (!V.OnBoard(x + shiftX, y)) return []; | |
347 | ||
7f1df0d9 BA |
348 | // A push cannot put a pawn on last rank (it goes backward) |
349 | let finalPieces = [V.PAWN]; | |
350 | if (x + shiftX == lastRank) { | |
351 | // Only allow direction facing inside board: | |
352 | const allowedLancerDirs = | |
353 | lastRank == 0 | |
354 | ? ['e', 'f', 'g', 'h', 'm'] | |
355 | : ['c', 'd', 'e', 'm', 'o']; | |
356 | finalPieces = | |
357 | allowedLancerDirs | |
358 | .concat([V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.SENTRY, V.JAILER]); | |
359 | } | |
ee307044 BA |
360 | if (this.board[x + shiftX][y] == V.EMPTY) { |
361 | // One square forward | |
362 | for (let piece of finalPieces) { | |
363 | moves.push( | |
364 | this.getBasicMove([x, y], [x + shiftX, y], { | |
365 | c: color, | |
366 | p: piece | |
367 | }) | |
368 | ); | |
369 | } | |
370 | if ( | |
89781a55 BA |
371 | // 2-squares jumps forbidden if pawn push |
372 | this.subTurn == 1 && | |
373 | [startRank, firstRank].includes(x) && | |
ee307044 BA |
374 | this.board[x + 2 * shiftX][y] == V.EMPTY |
375 | ) { | |
376 | // Two squares jump | |
377 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); | |
378 | } | |
379 | } | |
380 | // Captures | |
381 | for (let shiftY of [-1, 1]) { | |
382 | if ( | |
383 | y + shiftY >= 0 && | |
384 | y + shiftY < sizeY && | |
385 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
386 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
387 | ) { | |
388 | for (let piece of finalPieces) { | |
389 | moves.push( | |
390 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
391 | c: color, | |
392 | p: piece | |
393 | }) | |
394 | ); | |
6b7b2cf7 | 395 | } |
ee307044 BA |
396 | } |
397 | } | |
398 | ||
89781a55 | 399 | // En passant: only on subTurn == 1 |
ee307044 | 400 | const Lep = this.epSquares.length; |
3a2a7b5f | 401 | const epSquare = this.epSquares[Lep - 1]; |
ee307044 | 402 | if ( |
89781a55 | 403 | this.subTurn == 1 && |
ee307044 BA |
404 | !!epSquare && |
405 | epSquare.x == x + shiftX && | |
406 | Math.abs(epSquare.y - y) == 1 | |
407 | ) { | |
408 | let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); | |
409 | enpassantMove.vanish.push({ | |
410 | x: x, | |
411 | y: epSquare.y, | |
412 | p: "p", | |
413 | c: this.getColor(x, epSquare.y) | |
6b7b2cf7 | 414 | }); |
ee307044 | 415 | moves.push(enpassantMove); |
6b7b2cf7 | 416 | } |
6b7b2cf7 | 417 | |
ee307044 | 418 | return moves; |
9842aca2 BA |
419 | } |
420 | ||
1b56b736 BA |
421 | doClick(square) { |
422 | if (isNaN(square[0])) return null; | |
423 | const L = this.sentryPush.length; | |
424 | const [x, y] = [square[0], square[1]]; | |
425 | const color = this.turn; | |
426 | if ( | |
427 | this.subTurn == 2 || | |
428 | this.board[x][y] == V.EMPTY || | |
429 | this.getPiece(x, y) != V.LANCER || | |
430 | this.getColor(x, y) != color || | |
431 | !!this.sentryPush[L-1] | |
432 | ) { | |
433 | return null; | |
434 | } | |
435 | // Stuck lancer? | |
436 | const orientation = this.board[x][y][1]; | |
437 | const step = V.LANCER_DIRS[orientation]; | |
438 | if (!V.OnBoard(x + step[0], y + step[1])) { | |
439 | let choices = []; | |
440 | Object.keys(V.LANCER_DIRS).forEach(k => { | |
441 | const dir = V.LANCER_DIRS[k]; | |
442 | if ( | |
443 | (dir[0] != step[0] || dir[1] != step[1]) && | |
444 | V.OnBoard(x + dir[0], y + dir[1]) | |
445 | ) { | |
446 | choices.push( | |
447 | new Move({ | |
448 | vanish: [ | |
449 | new PiPo({ | |
450 | x: x, | |
451 | y: y, | |
452 | c: color, | |
453 | p: orientation | |
454 | }) | |
455 | ], | |
456 | appear: [ | |
457 | new PiPo({ | |
458 | x: x, | |
459 | y: y, | |
460 | c: color, | |
461 | p: k | |
462 | }) | |
463 | ], | |
464 | start: { x: x, y : y }, | |
465 | end: { x: -1, y: -1 } | |
466 | }) | |
467 | ); | |
468 | } | |
469 | }); | |
470 | return choices; | |
471 | } | |
472 | return null; | |
473 | } | |
474 | ||
3a2a7b5f | 475 | // Obtain all lancer moves in "step" direction |
afbf3ca7 | 476 | getPotentialLancerMoves_aux([x, y], step, tr) { |
9842aca2 BA |
477 | let moves = []; |
478 | // Add all moves to vacant squares until opponent is met: | |
b0a0468a BA |
479 | const color = this.getColor(x, y); |
480 | const oppCol = | |
481 | this.subTurn == 1 | |
482 | ? V.GetOppCol(color) | |
483 | // at subTurn == 2, consider own pieces as opponent | |
484 | : color; | |
9842aca2 BA |
485 | let sq = [x + step[0], y + step[1]]; |
486 | while (V.OnBoard(sq[0], sq[1]) && this.getColor(sq[0], sq[1]) != oppCol) { | |
487 | if (this.board[sq[0]][sq[1]] == V.EMPTY) | |
afbf3ca7 | 488 | moves.push(this.getBasicMove([x, y], sq, tr)); |
9842aca2 BA |
489 | sq[0] += step[0]; |
490 | sq[1] += step[1]; | |
491 | } | |
492 | if (V.OnBoard(sq[0], sq[1])) | |
493 | // Add capturing move | |
afbf3ca7 | 494 | moves.push(this.getBasicMove([x, y], sq, tr)); |
9842aca2 | 495 | return moves; |
6b7b2cf7 BA |
496 | } |
497 | ||
498 | getPotentialLancerMoves([x, y]) { | |
9842aca2 BA |
499 | let moves = []; |
500 | // Add all lancer possible orientations, similar to pawn promotions. | |
501 | // Except if just after a push: allow all movements from init square then | |
ee307044 | 502 | const L = this.sentryPush.length; |
89781a55 | 503 | const color = this.getColor(x, y); |
1b56b736 | 504 | const dirCode = this.board[x][y][1]; |
737a5daf | 505 | const curDir = V.LANCER_DIRS[dirCode]; |
ee307044 | 506 | if (!!this.sentryPush[L-1]) { |
9842aca2 | 507 | // Maybe I was pushed |
ee307044 | 508 | const pl = this.sentryPush[L-1].length; |
9842aca2 | 509 | if ( |
ee307044 BA |
510 | this.sentryPush[L-1][pl-1].x == x && |
511 | this.sentryPush[L-1][pl-1].y == y | |
9842aca2 BA |
512 | ) { |
513 | // I was pushed: allow all directions (for this move only), but | |
3a2a7b5f BA |
514 | // do not change direction after moving, *except* if I keep the |
515 | // same orientation in which I was pushed. | |
1b56b736 BA |
516 | // Also allow simple reorientation ("capturing king"): |
517 | if (!V.OnBoard(x + curDir[0], y + curDir[1])) { | |
518 | const kp = this.kingPos[color]; | |
519 | let reorientMoves = []; | |
520 | Object.keys(V.LANCER_DIRS).forEach(k => { | |
521 | const dir = V.LANCER_DIRS[k]; | |
522 | if ( | |
523 | (dir[0] != curDir[0] || dir[1] != curDir[1]) && | |
524 | V.OnBoard(x + dir[0], y + dir[1]) | |
525 | ) { | |
526 | reorientMoves.push( | |
527 | new Move({ | |
528 | vanish: [ | |
529 | new PiPo({ | |
530 | x: x, | |
531 | y: y, | |
532 | c: color, | |
533 | p: dirCode | |
534 | }) | |
535 | ], | |
536 | appear: [ | |
537 | new PiPo({ | |
538 | x: x, | |
539 | y: y, | |
540 | c: color, | |
541 | p: k | |
542 | }) | |
543 | ], | |
544 | start: { x: x, y : y }, | |
545 | end: { x: kp[0], y: kp[1] } | |
546 | }) | |
547 | ); | |
548 | } | |
549 | }); | |
550 | Array.prototype.push.apply(moves, reorientMoves); | |
551 | } | |
9842aca2 | 552 | Object.values(V.LANCER_DIRS).forEach(step => { |
afbf3ca7 | 553 | const dirCode = Object.keys(V.LANCER_DIRS).find(k => { |
3a2a7b5f BA |
554 | return ( |
555 | V.LANCER_DIRS[k][0] == step[0] && | |
556 | V.LANCER_DIRS[k][1] == step[1] | |
557 | ); | |
afbf3ca7 | 558 | }); |
3a2a7b5f BA |
559 | const dirMoves = |
560 | this.getPotentialLancerMoves_aux( | |
561 | [x, y], | |
562 | step, | |
563 | { p: dirCode, c: color } | |
564 | ); | |
565 | if (curDir[0] == step[0] && curDir[1] == step[1]) { | |
566 | // Keeping same orientation: can choose after | |
567 | let chooseMoves = []; | |
568 | dirMoves.forEach(m => { | |
569 | Object.keys(V.LANCER_DIRS).forEach(k => { | |
33974019 BA |
570 | const newDir = V.LANCER_DIRS[k]; |
571 | // Prevent orientations toward outer board: | |
572 | if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) { | |
573 | let mk = JSON.parse(JSON.stringify(m)); | |
574 | mk.appear[0].p = k; | |
575 | chooseMoves.push(mk); | |
576 | } | |
3a2a7b5f BA |
577 | }); |
578 | }); | |
579 | Array.prototype.push.apply(moves, chooseMoves); | |
33974019 BA |
580 | } |
581 | else Array.prototype.push.apply(moves, dirMoves); | |
9842aca2 BA |
582 | }); |
583 | return moves; | |
584 | } | |
585 | } | |
586 | // I wasn't pushed: standard lancer move | |
9842aca2 BA |
587 | const monodirMoves = |
588 | this.getPotentialLancerMoves_aux([x, y], V.LANCER_DIRS[dirCode]); | |
bfed878d BA |
589 | // Add all possible orientations aftermove except if I'm being pushed |
590 | if (this.subTurn == 1) { | |
591 | monodirMoves.forEach(m => { | |
592 | Object.keys(V.LANCER_DIRS).forEach(k => { | |
33974019 BA |
593 | const newDir = V.LANCER_DIRS[k]; |
594 | // Prevent orientations toward outer board: | |
595 | if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) { | |
596 | let mk = JSON.parse(JSON.stringify(m)); | |
597 | mk.appear[0].p = k; | |
598 | moves.push(mk); | |
599 | } | |
bfed878d | 600 | }); |
9842aca2 | 601 | }); |
bfed878d | 602 | return moves; |
33974019 BA |
603 | } |
604 | else { | |
737a5daf | 605 | // I'm pushed: add potential nudges, except for current orientation |
3a2a7b5f BA |
606 | let potentialNudges = []; |
607 | for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { | |
608 | if ( | |
737a5daf | 609 | (step[0] != curDir[0] || step[1] != curDir[1]) && |
3a2a7b5f BA |
610 | V.OnBoard(x + step[0], y + step[1]) && |
611 | this.board[x + step[0]][y + step[1]] == V.EMPTY | |
612 | ) { | |
89781a55 BA |
613 | const newDirCode = Object.keys(V.LANCER_DIRS).find(k => { |
614 | const codeStep = V.LANCER_DIRS[k]; | |
615 | return (codeStep[0] == step[0] && codeStep[1] == step[1]); | |
616 | }); | |
3a2a7b5f BA |
617 | potentialNudges.push( |
618 | this.getBasicMove( | |
619 | [x, y], | |
89781a55 BA |
620 | [x + step[0], y + step[1]], |
621 | { c: color, p: newDirCode } | |
3a2a7b5f BA |
622 | ) |
623 | ); | |
624 | } | |
625 | } | |
626 | return monodirMoves.concat(potentialNudges); | |
627 | } | |
6b7b2cf7 BA |
628 | } |
629 | ||
630 | getPotentialSentryMoves([x, y]) { | |
631 | // The sentry moves a priori like a bishop: | |
632 | let moves = super.getPotentialBishopMoves([x, y]); | |
ee307044 BA |
633 | // ...but captures are replaced by special move, if and only if |
634 | // "captured" piece can move now, considered as the capturer unit. | |
afbf3ca7 BA |
635 | // --> except is subTurn == 2, in this case I don't push anything. |
636 | if (this.subTurn == 2) return moves.filter(m => m.vanish.length == 1); | |
9842aca2 BA |
637 | moves.forEach(m => { |
638 | if (m.vanish.length == 2) { | |
639 | // Temporarily cancel the sentry capture: | |
640 | m.appear.pop(); | |
641 | m.vanish.pop(); | |
642 | } | |
643 | }); | |
bfed878d | 644 | const color = this.getColor(x, y); |
ee307044 | 645 | const fMoves = moves.filter(m => { |
b0a0468a | 646 | // Can the pushed unit make any move? ...resulting in a non-self-check? |
bfed878d BA |
647 | if (m.appear.length == 0) { |
648 | let res = false; | |
649 | this.play(m); | |
b0a0468a | 650 | let moves2 = this.getPotentialMovesFrom([m.end.x, m.end.y]); |
bfed878d BA |
651 | for (let m2 of moves2) { |
652 | this.play(m2); | |
653 | res = !this.underCheck(color); | |
654 | this.undo(m2); | |
655 | if (res) break; | |
656 | } | |
657 | this.undo(m); | |
658 | return res; | |
659 | } | |
660 | return true; | |
ee307044 | 661 | }); |
ee307044 | 662 | return fMoves; |
6b7b2cf7 BA |
663 | } |
664 | ||
665 | getPotentialJailerMoves([x, y]) { | |
ee307044 BA |
666 | return super.getPotentialRookMoves([x, y]).filter(m => { |
667 | // Remove jailer captures | |
668 | return m.vanish[0].p != V.JAILER || m.vanish.length == 1; | |
669 | }); | |
6b7b2cf7 BA |
670 | } |
671 | ||
b0a0468a BA |
672 | getPotentialKingMoves(sq) { |
673 | const moves = this.getSlideNJumpMoves( | |
4313762d | 674 | sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1); |
b0a0468a BA |
675 | return ( |
676 | this.subTurn == 1 | |
677 | ? moves.concat(this.getCastleMoves(sq)) | |
678 | : moves | |
679 | ); | |
680 | } | |
681 | ||
3a2a7b5f BA |
682 | atLeastOneMove() { |
683 | // If in second-half of a move, we already know that a move is possible | |
684 | if (this.subTurn == 2) return true; | |
685 | return super.atLeastOneMove(); | |
686 | } | |
687 | ||
ee307044 | 688 | filterValid(moves) { |
b0a0468a BA |
689 | if (moves.length == 0) return []; |
690 | const basicFilter = (m, c) => { | |
691 | this.play(m); | |
692 | const res = !this.underCheck(c); | |
693 | this.undo(m); | |
694 | return res; | |
695 | }; | |
bfed878d BA |
696 | // Disable check tests for sentry pushes, |
697 | // because in this case the move isn't finished | |
698 | let movesWithoutSentryPushes = []; | |
699 | let movesWithSentryPushes = []; | |
700 | moves.forEach(m => { | |
b0a0468a BA |
701 | // Second condition below for special king "pass" moves |
702 | if (m.appear.length > 0 || m.vanish.length == 0) | |
703 | movesWithoutSentryPushes.push(m); | |
bfed878d BA |
704 | else movesWithSentryPushes.push(m); |
705 | }); | |
b0a0468a BA |
706 | const color = this.turn; |
707 | const oppCol = V.GetOppCol(color); | |
708 | const filteredMoves = | |
709 | movesWithoutSentryPushes.filter(m => basicFilter(m, color)); | |
710 | // If at least one full move made, everything is allowed. | |
711 | // Else: forbid checks and captures. | |
712 | return ( | |
713 | this.movesCount >= 2 | |
714 | ? filteredMoves | |
715 | : filteredMoves.filter(m => { | |
d958cc68 | 716 | return (m.vanish.length <= 1 && basicFilter(m, oppCol)); |
b0a0468a BA |
717 | }) |
718 | ).concat(movesWithSentryPushes); | |
bfed878d BA |
719 | } |
720 | ||
721 | getAllValidMoves() { | |
722 | if (this.subTurn == 1) return super.getAllValidMoves(); | |
723 | // Sentry push: | |
afbf3ca7 | 724 | const sentrySq = [this.sentryPos.x, this.sentryPos.y]; |
bfed878d | 725 | return this.filterValid(this.getPotentialMovesFrom(sentrySq)); |
ee307044 BA |
726 | } |
727 | ||
68e19a44 | 728 | isAttacked(sq, color) { |
afbf3ca7 | 729 | return ( |
68e19a44 BA |
730 | super.isAttacked(sq, color) || |
731 | this.isAttackedByLancer(sq, color) || | |
732 | this.isAttackedBySentry(sq, color) | |
9dca2c93 | 733 | // The jailer doesn't capture. |
afbf3ca7 BA |
734 | ); |
735 | } | |
736 | ||
68e19a44 | 737 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { |
b0a0468a BA |
738 | for (let step of steps) { |
739 | let rx = x + step[0], | |
740 | ry = y + step[1]; | |
741 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { | |
742 | rx += step[0]; | |
743 | ry += step[1]; | |
744 | } | |
745 | if ( | |
746 | V.OnBoard(rx, ry) && | |
68e19a44 BA |
747 | this.getPiece(rx, ry) == piece && |
748 | this.getColor(rx, ry) == color && | |
b0a0468a BA |
749 | !this.isImmobilized([rx, ry]) |
750 | ) { | |
751 | return true; | |
752 | } | |
753 | } | |
754 | return false; | |
755 | } | |
756 | ||
68e19a44 BA |
757 | isAttackedByPawn([x, y], color) { |
758 | const pawnShift = (color == "w" ? 1 : -1); | |
759 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { | |
760 | for (let i of [-1, 1]) { | |
761 | if ( | |
762 | y + i >= 0 && | |
763 | y + i < V.size.y && | |
764 | this.getPiece(x + pawnShift, y + i) == V.PAWN && | |
765 | this.getColor(x + pawnShift, y + i) == color && | |
766 | !this.isImmobilized([x + pawnShift, y + i]) | |
767 | ) { | |
768 | return true; | |
b0a0468a BA |
769 | } |
770 | } | |
771 | } | |
772 | return false; | |
773 | } | |
774 | ||
68e19a44 | 775 | isAttackedByLancer([x, y], color) { |
afbf3ca7 BA |
776 | for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { |
777 | // If in this direction there are only enemy pieces and empty squares, | |
778 | // and we meet a lancer: can he reach us? | |
779 | // NOTE: do not stop at first lancer, there might be several! | |
780 | let coord = { x: x + step[0], y: y + step[1] }; | |
781 | let lancerPos = []; | |
782 | while ( | |
783 | V.OnBoard(coord.x, coord.y) && | |
784 | ( | |
785 | this.board[coord.x][coord.y] == V.EMPTY || | |
68e19a44 | 786 | this.getColor(coord.x, coord.y) == color |
afbf3ca7 BA |
787 | ) |
788 | ) { | |
b0a0468a BA |
789 | if ( |
790 | this.getPiece(coord.x, coord.y) == V.LANCER && | |
791 | !this.isImmobilized([coord.x, coord.y]) | |
792 | ) { | |
3a2a7b5f | 793 | lancerPos.push({x: coord.x, y: coord.y}); |
b0a0468a | 794 | } |
3a2a7b5f BA |
795 | coord.x += step[0]; |
796 | coord.y += step[1]; | |
afbf3ca7 | 797 | } |
737a5daf BA |
798 | const L = this.sentryPush.length; |
799 | const pl = (!!this.sentryPush[L-1] ? this.sentryPush[L-1].length : 0); | |
afbf3ca7 BA |
800 | for (let xy of lancerPos) { |
801 | const dir = V.LANCER_DIRS[this.board[xy.x][xy.y].charAt(1)]; | |
737a5daf BA |
802 | if ( |
803 | (dir[0] == -step[0] && dir[1] == -step[1]) || | |
804 | // If the lancer was just pushed, this is an attack too: | |
805 | ( | |
806 | !!this.sentryPush[L-1] && | |
807 | this.sentryPush[L-1][pl-1].x == xy.x && | |
808 | this.sentryPush[L-1][pl-1].y == xy.y | |
809 | ) | |
810 | ) { | |
811 | return true; | |
812 | } | |
afbf3ca7 BA |
813 | } |
814 | } | |
815 | return false; | |
816 | } | |
817 | ||
818 | // Helper to check sentries attacks: | |
819 | selfAttack([x1, y1], [x2, y2]) { | |
820 | const color = this.getColor(x1, y1); | |
9dca2c93 | 821 | const oppCol = V.GetOppCol(color); |
afbf3ca7 | 822 | const sliderAttack = (allowedSteps, lancer) => { |
89781a55 BA |
823 | const deltaX = x2 - x1, |
824 | absDeltaX = Math.abs(deltaX); | |
825 | const deltaY = y2 - y1, | |
826 | absDeltaY = Math.abs(deltaY); | |
518a0dc9 | 827 | const step = [ deltaX / absDeltaX || 0, deltaY / absDeltaY || 0 ]; |
89781a55 BA |
828 | if ( |
829 | // Check that the step is a priori valid: | |
830 | (absDeltaX != absDeltaY && deltaX != 0 && deltaY != 0) || | |
831 | allowedSteps.every(st => st[0] != step[0] || st[1] != step[1]) | |
832 | ) { | |
afbf3ca7 | 833 | return false; |
89781a55 | 834 | } |
3a2a7b5f | 835 | let sq = [ x1 + step[0], y1 + step[1] ]; |
f14572c4 | 836 | while (sq[0] != x2 || sq[1] != y2) { |
afbf3ca7 | 837 | if ( |
3a2a7b5f | 838 | // NOTE: no need to check OnBoard in this special case |
afbf3ca7 | 839 | (!lancer && this.board[sq[0]][sq[1]] != V.EMPTY) || |
9dca2c93 | 840 | (!!lancer && this.getColor(sq[0], sq[1]) == oppCol) |
afbf3ca7 BA |
841 | ) { |
842 | return false; | |
843 | } | |
3a2a7b5f BA |
844 | sq[0] += step[0]; |
845 | sq[1] += step[1]; | |
afbf3ca7 BA |
846 | } |
847 | return true; | |
848 | }; | |
849 | switch (this.getPiece(x1, y1)) { | |
850 | case V.PAWN: { | |
851 | // Pushed pawns move as enemy pawns | |
852 | const shift = (color == 'w' ? 1 : -1); | |
853 | return (x1 + shift == x2 && Math.abs(y1 - y2) == 1); | |
854 | } | |
855 | case V.KNIGHT: { | |
856 | const deltaX = Math.abs(x1 - x2); | |
857 | const deltaY = Math.abs(y1 - y2); | |
858 | return ( | |
859 | deltaX + deltaY == 3 && | |
860 | [1, 2].includes(deltaX) && | |
861 | [1, 2].includes(deltaY) | |
862 | ); | |
863 | } | |
864 | case V.ROOK: | |
865 | return sliderAttack(V.steps[V.ROOK]); | |
866 | case V.BISHOP: | |
867 | return sliderAttack(V.steps[V.BISHOP]); | |
868 | case V.QUEEN: | |
869 | return sliderAttack(V.steps[V.ROOK].concat(V.steps[V.BISHOP])); | |
870 | case V.LANCER: { | |
2c5d7b20 BA |
871 | // Special case: as long as no enemy units stands in-between, |
872 | // it attacks (if it points toward the king). | |
afbf3ca7 BA |
873 | const allowedStep = V.LANCER_DIRS[this.board[x1][y1].charAt(1)]; |
874 | return sliderAttack([allowedStep], "lancer"); | |
875 | } | |
876 | // No sentries or jailer tests: they cannot self-capture | |
877 | } | |
878 | return false; | |
879 | } | |
880 | ||
68e19a44 | 881 | isAttackedBySentry([x, y], color) { |
afbf3ca7 BA |
882 | // Attacked by sentry means it can self-take our king. |
883 | // Just check diagonals of enemy sentry(ies), and if it reaches | |
884 | // one of our pieces: can I self-take? | |
68e19a44 | 885 | const myColor = V.GetOppCol(color); |
afbf3ca7 BA |
886 | let candidates = []; |
887 | for (let i=0; i<V.size.x; i++) { | |
888 | for (let j=0; j<V.size.y; j++) { | |
889 | if ( | |
890 | this.getPiece(i,j) == V.SENTRY && | |
68e19a44 | 891 | this.getColor(i,j) == color && |
b0a0468a | 892 | !this.isImmobilized([i, j]) |
afbf3ca7 BA |
893 | ) { |
894 | for (let step of V.steps[V.BISHOP]) { | |
895 | let sq = [ i + step[0], j + step[1] ]; | |
896 | while ( | |
897 | V.OnBoard(sq[0], sq[1]) && | |
898 | this.board[sq[0]][sq[1]] == V.EMPTY | |
899 | ) { | |
900 | sq[0] += step[0]; | |
901 | sq[1] += step[1]; | |
902 | } | |
903 | if ( | |
904 | V.OnBoard(sq[0], sq[1]) && | |
68e19a44 | 905 | this.getColor(sq[0], sq[1]) == myColor |
afbf3ca7 | 906 | ) { |
3a2a7b5f | 907 | candidates.push([ sq[0], sq[1] ]); |
afbf3ca7 BA |
908 | } |
909 | } | |
910 | } | |
911 | } | |
912 | } | |
913 | for (let c of candidates) | |
914 | if (this.selfAttack(c, [x, y])) return true; | |
915 | return false; | |
916 | } | |
917 | ||
918 | // Jailer doesn't capture or give check | |
919 | ||
d54f6261 BA |
920 | prePlay(move) { |
921 | if (move.appear.length == 0 && move.vanish.length == 1) | |
922 | // The sentry is about to push a piece: subTurn goes from 1 to 2 | |
923 | this.sentryPos = { x: move.end.x, y: move.end.y }; | |
924 | if (this.subTurn == 2 && move.vanish[0].p != V.PAWN) { | |
925 | // A piece is pushed: forbid array of squares between start and end | |
926 | // of move, included (except if it's a pawn) | |
927 | let squares = []; | |
928 | if ([V.KNIGHT,V.KING].includes(move.vanish[0].p)) | |
929 | // short-range pieces: just forbid initial square | |
930 | squares.push({ x: move.start.x, y: move.start.y }); | |
931 | else { | |
932 | const deltaX = move.end.x - move.start.x; | |
933 | const deltaY = move.end.y - move.start.y; | |
934 | const step = [ | |
935 | deltaX / Math.abs(deltaX) || 0, | |
936 | deltaY / Math.abs(deltaY) || 0 | |
937 | ]; | |
938 | for ( | |
939 | let sq = {x: move.start.x, y: move.start.y}; | |
940 | sq.x != move.end.x || sq.y != move.end.y; | |
941 | sq.x += step[0], sq.y += step[1] | |
942 | ) { | |
943 | squares.push({ x: sq.x, y: sq.y }); | |
944 | } | |
945 | } | |
946 | // Add end square as well, to know if I was pushed (useful for lancers) | |
947 | squares.push({ x: move.end.x, y: move.end.y }); | |
948 | this.sentryPush.push(squares); | |
949 | } else this.sentryPush.push(null); | |
950 | } | |
951 | ||
952 | play(move) { | |
953 | this.prePlay(move); | |
954 | move.flags = JSON.stringify(this.aggregateFlags()); | |
955 | this.epSquares.push(this.getEpSquare(move)); | |
956 | V.PlayOnBoard(this.board, move); | |
957 | // Is it a sentry push? (useful for undo) | |
958 | move.sentryPush = (this.subTurn == 2); | |
959 | if (this.subTurn == 1) this.movesCount++; | |
960 | if (move.appear.length == 0 && move.vanish.length == 1) this.subTurn = 2; | |
961 | else { | |
962 | // Turn changes only if not a sentry "pre-push" | |
963 | this.turn = V.GetOppCol(this.turn); | |
964 | this.subTurn = 1; | |
965 | } | |
966 | this.postPlay(move); | |
967 | } | |
968 | ||
969 | postPlay(move) { | |
970 | if (move.vanish.length == 0 || this.subTurn == 2) | |
971 | // Special pass move of the king, or sentry pre-push: nothing to update | |
972 | return; | |
973 | const c = move.vanish[0].c; | |
974 | const piece = move.vanish[0].p; | |
975 | const firstRank = c == "w" ? V.size.x - 1 : 0; | |
976 | ||
977 | if (piece == V.KING) { | |
978 | this.kingPos[c][0] = move.appear[0].x; | |
979 | this.kingPos[c][1] = move.appear[0].y; | |
980 | this.castleFlags[c] = [V.size.y, V.size.y]; | |
981 | return; | |
982 | } | |
983 | // Update castling flags if rooks are moved | |
984 | const oppCol = V.GetOppCol(c); | |
985 | const oppFirstRank = V.size.x - 1 - firstRank; | |
986 | if ( | |
987 | move.start.x == firstRank && //our rook moves? | |
988 | this.castleFlags[c].includes(move.start.y) | |
989 | ) { | |
990 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
991 | this.castleFlags[c][flagIdx] = V.size.y; | |
992 | } else if ( | |
993 | move.end.x == oppFirstRank && //we took opponent rook? | |
994 | this.castleFlags[oppCol].includes(move.end.y) | |
995 | ) { | |
996 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
997 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
998 | } | |
999 | } | |
1000 | ||
1001 | undo(move) { | |
1002 | this.epSquares.pop(); | |
1003 | this.disaggregateFlags(JSON.parse(move.flags)); | |
1004 | V.UndoOnBoard(this.board, move); | |
1005 | // Decrement movesCount except if the move is a sentry push | |
1006 | if (!move.sentryPush) this.movesCount--; | |
1007 | if (this.subTurn == 2) this.subTurn = 1; | |
1008 | else { | |
1009 | this.turn = V.GetOppCol(this.turn); | |
1010 | if (move.sentryPush) this.subTurn = 2; | |
1011 | } | |
1012 | this.postUndo(move); | |
1013 | } | |
1014 | ||
1015 | postUndo(move) { | |
1016 | super.postUndo(move); | |
1017 | this.sentryPush.pop(); | |
1018 | } | |
1019 | ||
2a8a94c9 BA |
1020 | static get VALUES() { |
1021 | return Object.assign( | |
28b32b4f | 1022 | { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations |
2a8a94c9 BA |
1023 | ChessRules.VALUES |
1024 | ); | |
1025 | } | |
6b7b2cf7 | 1026 | |
afbf3ca7 BA |
1027 | getComputerMove() { |
1028 | const maxeval = V.INFINITY; | |
1029 | const color = this.turn; | |
1030 | let moves1 = this.getAllValidMoves(); | |
1031 | ||
1032 | if (moves1.length == 0) | |
1033 | // TODO: this situation should not happen | |
1034 | return null; | |
1035 | ||
3a2a7b5f | 1036 | const setEval = (move, next) => { |
afbf3ca7 | 1037 | const score = this.getCurrentScore(); |
3a2a7b5f | 1038 | const curEval = move.eval; |
afbf3ca7 BA |
1039 | if (score != "*") { |
1040 | move.eval = | |
1041 | score == "1/2" | |
1042 | ? 0 | |
1043 | : (score == "1-0" ? 1 : -1) * maxeval; | |
3a2a7b5f BA |
1044 | } else move.eval = this.evalPosition(); |
1045 | if ( | |
1046 | // "next" is defined after sentry pushes | |
1047 | !!next && ( | |
1048 | !curEval || | |
1049 | color == 'w' && move.eval > curEval || | |
1050 | color == 'b' && move.eval < curEval | |
1051 | ) | |
1052 | ) { | |
1053 | move.second = next; | |
1054 | } | |
afbf3ca7 BA |
1055 | }; |
1056 | ||
1057 | // Just search_depth == 1 (because of sentries. TODO: can do better...) | |
1058 | moves1.forEach(m1 => { | |
1059 | this.play(m1); | |
1060 | if (this.subTurn == 1) setEval(m1); | |
1061 | else { | |
1062 | // Need to play every pushes and count: | |
1063 | const moves2 = this.getAllValidMoves(); | |
1064 | moves2.forEach(m2 => { | |
1065 | this.play(m2); | |
3a2a7b5f | 1066 | setEval(m1, m2); |
afbf3ca7 BA |
1067 | this.undo(m2); |
1068 | }); | |
1069 | } | |
1070 | this.undo(m1); | |
1071 | }); | |
1072 | ||
1073 | moves1.sort((a, b) => { | |
1074 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); | |
1075 | }); | |
1076 | let candidates = [0]; | |
1077 | for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++) | |
1078 | candidates.push(j); | |
3a2a7b5f BA |
1079 | const choice = moves1[candidates[randInt(candidates.length)]]; |
1080 | return (!choice.second ? choice : [choice, choice.second]); | |
afbf3ca7 BA |
1081 | } |
1082 | ||
f14572c4 BA |
1083 | // For moves notation: |
1084 | static get LANCER_DIRNAMES() { | |
1085 | return { | |
1086 | 'c': "N", | |
1087 | 'd': "NE", | |
1088 | 'e': "E", | |
1089 | 'f': "SE", | |
1090 | 'g': "S", | |
1091 | 'h': "SW", | |
1092 | 'm': "W", | |
1093 | 'o': "NW" | |
1094 | }; | |
1095 | } | |
1096 | ||
6b7b2cf7 BA |
1097 | getNotation(move) { |
1098 | // Special case "king takes jailer" is a pass move | |
1099 | if (move.appear.length == 0 && move.vanish.length == 0) return "pass"; | |
f14572c4 | 1100 | let notation = undefined; |
b0a0468a BA |
1101 | if (this.subTurn == 2) { |
1102 | // Do not consider appear[1] (sentry) for sentry pushes | |
1103 | const simpleMove = { | |
1104 | appear: [move.appear[0]], | |
1105 | vanish: move.vanish, | |
1106 | start: move.start, | |
1107 | end: move.end | |
1108 | }; | |
f14572c4 | 1109 | notation = super.getNotation(simpleMove); |
1b56b736 BA |
1110 | } |
1111 | else if ( | |
1112 | move.appear.length > 0 && | |
1113 | move.vanish[0].x == move.appear[0].x && | |
1114 | move.vanish[0].y == move.appear[0].y | |
1115 | ) { | |
1116 | // Lancer in-place reorientation: | |
1117 | notation = "L" + V.CoordsToSquare(move.start) + ":R"; | |
1118 | } | |
1119 | else notation = super.getNotation(move); | |
f14572c4 BA |
1120 | if (Object.keys(V.LANCER_DIRNAMES).includes(move.vanish[0].p)) |
1121 | // Lancer: add direction info | |
1122 | notation += "=" + V.LANCER_DIRNAMES[move.appear[0].p]; | |
7e476ce4 BA |
1123 | else if ( |
1124 | move.vanish[0].p == V.PAWN && | |
1125 | Object.keys(V.LANCER_DIRNAMES).includes(move.appear[0].p) | |
1126 | ) { | |
1127 | // Fix promotions in lancer: | |
2c5d7b20 BA |
1128 | notation = notation.slice(0, -1) + |
1129 | "L:" + V.LANCER_DIRNAMES[move.appear[0].p]; | |
7e476ce4 | 1130 | } |
f14572c4 | 1131 | return notation; |
6b7b2cf7 | 1132 | } |
7e8a7ea1 | 1133 | |
2a8a94c9 | 1134 | }; |