'update'
[vchess.git] / client / src / variants / Dynamo.js
CommitLineData
b866a62a 1import { ChessRules, Move, PiPo } from "@/base_rules";
ad1e629e 2import { randInt } from "@/utils/alea";
b866a62a 3
0d5335de 4export class DynamoRules extends ChessRules {
7ddfec38 5 // TODO: later, allow to push out pawns on a and h files
61656127
BA
6 static get HasEnpassant() {
7 return false;
8 }
9
c7550017
BA
10 canIplay(side, [x, y]) {
11 // Sometimes opponent's pieces can be moved directly
e78633db 12 return this.turn == side;
c7550017
BA
13 }
14
61656127
BA
15 setOtherVariables(fen) {
16 super.setOtherVariables(fen);
17 this.subTurn = 1;
18 // Local stack of "action moves"
19 this.amoves = [];
20 const amove = V.ParseFen(fen).amove;
93ce6119 21 if (amove != "-") {
61656127 22 const amoveParts = amove.split("/");
ad1e629e 23 let move = {
61656127
BA
24 // No need for start & end
25 appear: [],
26 vanish: []
27 };
28 [0, 1].map(i => {
ad1e629e
BA
29 if (amoveParts[i] != "-") {
30 amoveParts[i].split(".").forEach(av => {
31 // Format is "bpe3"
32 const xy = V.SquareToCoords(av.substr(2));
33 move[i == 0 ? "appear" : "vanish"].push(
34 new PiPo({
35 x: xy.x,
36 y: xy.y,
37 c: av[0],
38 p: av[1]
39 })
40 );
41 });
42 }
61656127
BA
43 });
44 this.amoves.push(move);
c7550017 45 }
7b53b5a7 46 this.subTurn = 1;
a443d256
BA
47 // Stack "first moves" (on subTurn 1) to merge and check opposite moves
48 this.firstMove = [];
c7550017
BA
49 }
50
61656127
BA
51 static ParseFen(fen) {
52 return Object.assign(
53 ChessRules.ParseFen(fen),
93ce6119 54 { amove: fen.split(" ")[4] }
61656127
BA
55 );
56 }
57
58 static IsGoodFen(fen) {
59 if (!ChessRules.IsGoodFen(fen)) return false;
60 const fenParts = fen.split(" ");
ad1e629e
BA
61 if (fenParts.length != 5) return false;
62 if (fenParts[4] != "-") {
63 // TODO: a single regexp instead.
64 // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-'
65 const amoveParts = fenParts[4].split("/");
66 if (amoveParts.length != 2) return false;
67 for (let part of amoveParts) {
68 if (part != "-") {
69 for (let psq of part.split("."))
70 if (!psq.match(/^[a-r]{3}[1-8]$/)) return false;
71 }
72 }
73 }
61656127
BA
74 return true;
75 }
76
93ce6119
BA
77 getFen() {
78 return super.getFen() + " " + this.getAmoveFen();
61656127
BA
79 }
80
93ce6119
BA
81 getFenForRepeat() {
82 return super.getFenForRepeat() + "_" + this.getAmoveFen();
83 }
84
85 getAmoveFen() {
86 const L = this.amoves.length;
7b53b5a7 87 if (L == 0) return "-";
93ce6119
BA
88 return (
89 ["appear","vanish"].map(
90 mpart => {
156986e6 91 if (this.amoves[L-1][mpart].length == 0) return "-";
93ce6119
BA
92 return (
93 this.amoves[L-1][mpart].map(
94 av => {
95 const square = V.CoordsToSquare({ x: av.x, y: av.y });
96 return av.c + av.p + square;
97 }
98 ).join(".")
99 );
100 }
101 ).join("/")
102 );
61656127
BA
103 }
104
105 canTake() {
106 // Captures don't occur (only pulls & pushes)
107 return false;
108 }
109
b2655276
BA
110 // Step is right, just add (push/pull) moves in this direction
111 // Direction is assumed normalized.
112 getMovesInDirection([x, y], [dx, dy], nbSteps) {
113 nbSteps = nbSteps || 8; //max 8 steps anyway
114 let [i, j] = [x + dx, y + dy];
61656127 115 let moves = [];
b2655276
BA
116 const color = this.getColor(x, y);
117 const piece = this.getPiece(x, y);
118 const lastRank = (color == 'w' ? 0 : 7);
7b53b5a7 119 let counter = 1;
b2655276
BA
120 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
121 if (i == lastRank && piece == V.PAWN) {
122 // Promotion by push or pull
123 V.PawnSpecs.promotions.forEach(p => {
124 let move = super.getBasicMove([x, y], [i, j], { c: color, p: p });
125 moves.push(move);
126 });
127 }
128 else moves.push(super.getBasicMove([x, y], [i, j]));
7b53b5a7
BA
129 if (++counter > nbSteps) break;
130 i += dx;
131 j += dy;
b2655276
BA
132 }
133 if (!V.OnBoard(i, j) && piece != V.KING) {
134 // Add special "exit" move, by "taking king"
135 moves.push(
136 new Move({
137 start: { x: x, y: y },
7b53b5a7 138 end: { x: this.kingPos[color][0], y: this.kingPos[color][1] },
b2655276
BA
139 appear: [],
140 vanish: [{ x: x, y: y, c: color, p: piece }]
141 })
142 );
143 }
61656127
BA
144 return moves;
145 }
146
b2655276
BA
147 // Normalize direction to know the step
148 getNormalizedDirection([dx, dy]) {
149 const absDir = [Math.abs(dx), Math.abs(dy)];
150 let divisor = 0;
151 if (absDir[0] != 0 && absDir[1] != 0 && absDir[0] != absDir[1])
152 // Knight
153 divisor = Math.min(absDir[0], absDir[1]);
154 else
155 // Standard slider (or maybe a pawn or king: same)
156 divisor = Math.max(absDir[0], absDir[1]);
157 return [dx / divisor, dy / divisor];
158 }
159
ad1e629e
BA
160 // There was something on x2,y2, maybe our color, pushed/pulled.
161 // Also, the pushed/pulled piece must exit the board.
162 isAprioriValidExit([x1, y1], [x2, y2], color2) {
b2655276 163 const color1 = this.getColor(x1, y1);
b2655276 164 const pawnShift = (color1 == 'w' ? -1 : 1);
ad1e629e 165 const lastRank = (color1 == 'w' ? 0 : 7);
b2655276
BA
166 const deltaX = Math.abs(x1 - x2);
167 const deltaY = Math.abs(y1 - y2);
ad1e629e
BA
168 const checkSlider = () => {
169 const dir = this.getNormalizedDirection([x2 - x1, y2 - y1]);
170 let [i, j] = [x1 + dir[0], y1 + dir[1]];
171 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
172 i += dir[0];
173 j += dir[1];
174 }
175 return !V.OnBoard(i, j);
176 };
b2655276
BA
177 switch (this.getPiece(x1, y1)) {
178 case V.PAWN:
179 return (
ad1e629e 180 x1 + pawnShift == x2 &&
b2655276 181 (
ad1e629e
BA
182 (color1 == color2 && x2 == lastRank && y1 == y2) ||
183 (color1 != color2 && deltaY == 1 && !V.OnBoard(x2, 2 * y2 - y1))
b2655276
BA
184 )
185 );
186 case V.ROOK:
ad1e629e
BA
187 if (x1 != x2 && y1 != y2) return false;
188 return checkSlider();
189 case V.KNIGHT:
190 return (
191 deltaX + deltaY == 3 &&
192 (deltaX == 1 || deltaY == 1) &&
193 !V.OnBoard(2 * x2 - x1, 2 * y2 - y1)
194 );
b2655276 195 case V.BISHOP:
ad1e629e
BA
196 if (deltaX != deltaY) return false;
197 return checkSlider();
b2655276 198 case V.QUEEN:
ad1e629e
BA
199 if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) return false;
200 return checkSlider();
201 case V.KING:
b2655276 202 return (
ad1e629e
BA
203 deltaX <= 1 &&
204 deltaY <= 1 &&
205 !V.OnBoard(2 * x2 - x1, 2 * y2 - y1)
b2655276 206 );
93ce6119 207 }
b2655276 208 return false;
b866a62a
BA
209 }
210
05d37cc7
BA
211 isAprioriValidVertical([x1, y1], x2) {
212 const piece = this.getPiece(x1, y1);
213 const deltaX = Math.abs(x1 - x2);
214 const startRank = (this.getColor(x1, y1) == 'w' ? 6 : 1);
215 return (
216 [V.QUEEN, V.ROOK].includes(piece) ||
217 (
218 [V.KING, V.PAWN].includes(piece) &&
219 (
220 deltaX == 1 ||
221 (deltaX == 2 && piece == V.PAWN && x1 == startRank)
222 )
223 )
224 );
225 }
226
93ce6119 227 // NOTE: for pushes, play the pushed piece first.
61656127 228 // for pulls: play the piece doing the action first
b2655276 229 // NOTE: to push a piece out of the board, make it slide until its king
b866a62a
BA
230 getPotentialMovesFrom([x, y]) {
231 const color = this.turn;
93ce6119 232 if (this.subTurn == 1) {
b2655276
BA
233 const getMoveHash = (m) => {
234 return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end);
235 };
236 const addMoves = (dir, nbSteps) => {
237 const newMoves =
238 this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps)
239 .filter(m => !movesHash[getMoveHash(m)]);
7b53b5a7 240 newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; });
b2655276
BA
241 Array.prototype.push.apply(moves, newMoves);
242 };
8c267d0c 243 // Free to play any move (if piece of my color):
cfceecba 244 let moves =
8c267d0c
BA
245 this.getColor(x, y) == color
246 ? super.getPotentialMovesFrom([x, y])
247 : [];
cfceecba
BA
248 // There may be several suicide moves: keep only one
249 let hasExit = false;
250 moves = moves.filter(m => {
251 const suicide = (m.appear.length == 0);
252 if (suicide) {
253 if (hasExit) return false;
254 hasExit = true;
255 }
256 return true;
257 });
b2655276
BA
258 const pawnShift = (color == 'w' ? -1 : 1);
259 const pawnStartRank = (color == 'w' ? 6 : 1);
7b53b5a7
BA
260 // Structure to avoid adding moves twice (can be action & move)
261 let movesHash = {};
262 moves.forEach(m => { movesHash[getMoveHash(m)] = true; });
b2655276
BA
263 // [x, y] is pushed by 'color'
264 for (let step of V.steps[V.KNIGHT]) {
265 const [i, j] = [x + step[0], y + step[1]];
7b53b5a7
BA
266 if (
267 V.OnBoard(i, j) &&
268 this.board[i][j] != V.EMPTY &&
269 this.getColor(i, j) == color &&
270 this.getPiece(i, j) == V.KNIGHT
271 ) {
272 addMoves(step, 1);
b2655276
BA
273 }
274 }
7b53b5a7 275 for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
b2655276
BA
276 let [i, j] = [x + step[0], y + step[1]];
277 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
278 i += step[0];
279 j += step[1];
280 }
281 if (
282 V.OnBoard(i, j) &&
283 this.board[i][j] != V.EMPTY &&
284 this.getColor(i, j) == color
285 ) {
286 const deltaX = Math.abs(i - x);
287 const deltaY = Math.abs(j - y);
b2655276
BA
288 switch (this.getPiece(i, j)) {
289 case V.PAWN:
ad1e629e
BA
290 if (
291 (x - i) / deltaX == pawnShift &&
292 deltaX <= 2 &&
293 deltaY <= 1
294 ) {
b2655276
BA
295 const pColor = this.getColor(x, y);
296 if (pColor == color && deltaY == 0) {
297 // Pushed forward
298 const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1);
299 addMoves(step, maxSteps);
300 }
301 else if (pColor != color && deltaY == 1 && deltaX == 1)
302 // Pushed diagonally
303 addMoves(step, 1);
304 }
305 break;
306 case V.ROOK:
307 if (deltaX == 0 || deltaY == 0) addMoves(step);
308 break;
b2655276
BA
309 case V.BISHOP:
310 if (deltaX == deltaY) addMoves(step);
311 break;
312 case V.QUEEN:
cfceecba
BA
313 // All steps are valid for a queen:
314 addMoves(step);
b2655276
BA
315 break;
316 case V.KING:
317 if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1);
318 break;
319 }
320 }
321 }
322 return moves;
b866a62a 323 }
93ce6119 324 // If subTurn == 2 then we should have a first move,
b2655276
BA
325 // which restrict what we can play now: only in the first move direction
326 // NOTE: no need for knight or pawn checks, because the move will be
327 // naturally limited in those cases.
93ce6119
BA
328 const L = this.firstMove.length;
329 const fm = this.firstMove[L-1];
b2655276
BA
330 if (fm.appear.length == 2 && fm.vanish.length == 2)
331 // Castle: no real move playable then.
332 return [];
333 if (fm.appear.length == 0) {
334 // Piece at subTurn 1 just exited the board.
335 // Can I be a piece which caused the exit?
ad1e629e
BA
336 if (
337 this.isAprioriValidExit(
338 [x, y],
339 [fm.start.x, fm.start.y],
340 fm.vanish[0].c
341 )
342 ) {
b2655276
BA
343 // Seems so:
344 const dir = this.getNormalizedDirection(
345 [fm.start.x - x, fm.start.y - y]);
346 return this.getMovesInDirection([x, y], dir);
347 }
93ce6119
BA
348 }
349 else {
b2655276
BA
350 const dirM = this.getNormalizedDirection(
351 [fm.end.x - fm.start.x, fm.end.y - fm.start.y]);
352 const dir = this.getNormalizedDirection(
353 [fm.start.x - x, fm.start.y - y]);
ad1e629e
BA
354 // Normalized directions should match
355 if (dir[0] == dirM[0] && dir[1] == dirM[1]) {
05d37cc7
BA
356 // If first move is a pawn move, only a queen, rook, or maybe king or
357 // pawn can follow (need vertical movement option).
358 if (
359 fm.vanish[0].p == V.PAWN &&
360 fm.vanish[0].c == color &&
361 !this.isAprioriValidVertical([x, y], fm.start.x)
362 ) {
363 return [];
364 }
ad1e629e
BA
365 // And nothing should stand between [x, y] and the square fm.start
366 let [i, j] = [x + dir[0], y + dir[1]];
367 while (
368 (i != fm.start.x || j != fm.start.y) &&
369 this.board[i][j] == V.EMPTY
370 ) {
371 i += dir[0];
372 j += dir[1];
373 }
374 if (i == fm.start.x && j == fm.start.y)
375 return this.getMovesInDirection([x, y], dir);
376 }
93ce6119 377 }
b2655276 378 return [];
61656127
BA
379 }
380
cfceecba
BA
381 getSlideNJumpMoves([x, y], steps, oneStep) {
382 let moves = [];
383 outerLoop: for (let step of steps) {
384 let i = x + step[0];
385 let j = y + step[1];
386 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
387 moves.push(this.getBasicMove([x, y], [i, j]));
388 if (oneStep) continue outerLoop;
389 i += step[0];
390 j += step[1];
391 }
392 if (V.OnBoard(i, j)) {
393 if (this.canTake([x, y], [i, j]))
394 moves.push(this.getBasicMove([x, y], [i, j]));
395 }
396 else {
397 // Add potential board exit (suicide), except for the king
398 const piece = this.getPiece(x, y);
399 if (piece != V.KING) {
400 const c = this.getColor(x, y);
401 moves.push({
402 start: { x: x, y: y},
403 end: { x: this.kingPos[c][0], y: this.kingPos[c][1] },
404 appear: [],
405 vanish: [
406 new PiPo({
407 x: x,
408 y: y,
409 c: c,
410 p: piece
411 })
412 ]
413 });
414 }
415 }
416 }
417 return moves;
418 }
419
61656127
BA
420 // Does m2 un-do m1 ? (to disallow undoing actions)
421 oppositeMoves(m1, m2) {
422 const isEqual = (av1, av2) => {
423 // Precondition: av1 and av2 length = 2
424 for (let av of av1) {
425 const avInAv2 = av2.find(elt => {
426 return (
427 elt.x == av.x &&
428 elt.y == av.y &&
429 elt.c == av.c &&
430 elt.p == av.p
431 );
432 });
433 if (!avInAv2) return false;
434 }
435 return true;
436 };
437 return (
61656127
BA
438 m1.appear.length == 2 &&
439 m2.appear.length == 2 &&
440 m1.vanish.length == 2 &&
441 m2.vanish.length == 2 &&
442 isEqual(m1.appear, m2.vanish) &&
443 isEqual(m1.vanish, m2.appear)
444 );
445 }
446
93ce6119
BA
447 getAmove(move1, move2) {
448 // Just merge (one is action one is move, one may be empty)
449 return {
450 appear: move1.appear.concat(move2.appear),
451 vanish: move1.vanish.concat(move2.vanish)
452 }
453 }
454
61656127 455 filterValid(moves) {
93ce6119
BA
456 const color = this.turn;
457 if (this.subTurn == 1) {
458 return moves.filter(m => {
459 // A move is valid either if it doesn't result in a check,
460 // or if a second move is possible to counter the check
461 // (not undoing a potential move + action of the opponent)
462 this.play(m);
463 let res = this.underCheck(color);
464 if (res) {
465 const moves2 = this.getAllPotentialMoves();
ad1e629e 466 for (let m2 of moves2) {
93ce6119
BA
467 this.play(m2);
468 const res2 = this.underCheck(color);
469 this.undo(m2);
470 if (!res2) {
471 res = false;
472 break;
473 }
474 }
475 }
476 this.undo(m);
477 return !res;
478 });
479 }
480 const Lf = this.firstMove.length;
481 const La = this.amoves.length;
482 if (La == 0) return super.filterValid(moves);
a443d256 483 return (
93ce6119 484 super.filterValid(
a443d256
BA
485 moves.filter(m => {
486 // Move shouldn't undo another:
93ce6119
BA
487 const amove = this.getAmove(this.firstMove[Lf-1], m);
488 return !this.oppositeMoves(this.amoves[La-1], amove);
a443d256
BA
489 })
490 )
491 );
b866a62a
BA
492 }
493
c7550017
BA
494 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
495 for (let step of steps) {
496 let rx = x + step[0],
497 ry = y + step[1];
498 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
499 rx += step[0];
500 ry += step[1];
501 }
502 if (
503 V.OnBoard(rx, ry) &&
504 this.getPiece(rx, ry) == piece &&
505 this.getColor(rx, ry) == color
506 ) {
8c267d0c
BA
507 // Continue some steps in the same direction (pull)
508 rx += step[0];
509 ry += step[1];
510 while (
511 V.OnBoard(rx, ry) &&
512 this.board[rx][ry] == V.EMPTY &&
513 !oneStep
514 ) {
515 rx += step[0];
516 ry += step[1];
517 }
518 if (!V.OnBoard(rx, ry)) return true;
519 // Step in the other direction (push)
c7550017
BA
520 rx = x - step[0];
521 ry = y - step[1];
2c5d7b20
BA
522 while (
523 V.OnBoard(rx, ry) &&
524 this.board[rx][ry] == V.EMPTY &&
525 !oneStep
526 ) {
c7550017
BA
527 rx -= step[0];
528 ry -= step[1];
529 }
530 if (!V.OnBoard(rx, ry)) return true;
531 }
532 }
533 return false;
534 }
535
536 isAttackedByPawn([x, y], color) {
537 const lastRank = (color == 'w' ? 0 : 7);
ad1e629e 538 if (x != lastRank)
c7550017
BA
539 // The king can be pushed out by a pawn only on last rank
540 return false;
541 const pawnShift = (color == "w" ? 1 : -1);
542 for (let i of [-1, 1]) {
543 if (
544 y + i >= 0 &&
545 y + i < V.size.y &&
546 this.getPiece(x + pawnShift, y + i) == V.PAWN &&
547 this.getColor(x + pawnShift, y + i) == color
548 ) {
549 return true;
550 }
551 }
552 return false;
553 }
61656127 554
156986e6
BA
555 // No consideration of color: all pieces could be played
556 getAllPotentialMoves() {
557 let potentialMoves = [];
558 for (let i = 0; i < V.size.x; i++) {
559 for (let j = 0; j < V.size.y; j++) {
560 if (this.board[i][j] != V.EMPTY) {
561 Array.prototype.push.apply(
562 potentialMoves,
563 this.getPotentialMovesFrom([i, j])
564 );
565 }
566 }
567 }
568 return potentialMoves;
569 }
570
61656127
BA
571 getCurrentScore() {
572 if (this.subTurn == 2)
573 // Move not over
574 return "*";
575 return super.getCurrentScore();
576 }
577
93ce6119 578 doClick(square) {
b2655276
BA
579 // If subTurn == 2 && square is empty && !underCheck,
580 // then return an empty move, allowing to "pass" subTurn2
93ce6119
BA
581 if (
582 this.subTurn == 2 &&
7b53b5a7 583 this.board[square[0]][square[1]] == V.EMPTY &&
b2655276 584 !this.underCheck(this.turn)
93ce6119
BA
585 ) {
586 return {
7b53b5a7
BA
587 start: { x: -1, y: -1 },
588 end: { x: -1, y: -1 },
93ce6119
BA
589 appear: [],
590 vanish: []
591 };
592 }
593 return null;
594 }
595
61656127
BA
596 play(move) {
597 move.flags = JSON.stringify(this.aggregateFlags());
598 V.PlayOnBoard(this.board, move);
7ddfec38 599 if (this.subTurn == 2) {
7b53b5a7
BA
600 const L = this.firstMove.length;
601 this.amoves.push(this.getAmove(this.firstMove[L-1], move));
61656127 602 this.turn = V.GetOppCol(this.turn);
7ddfec38 603 this.movesCount++;
61656127 604 }
a443d256 605 else this.firstMove.push(move);
7ddfec38 606 this.subTurn = 3 - this.subTurn;
61656127
BA
607 this.postPlay(move);
608 }
609
7b53b5a7
BA
610 postPlay(move) {
611 if (move.start.x < 0) return;
612 for (let a of move.appear)
613 if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y];
614 this.updateCastleFlags(move);
615 }
616
617 updateCastleFlags(move) {
618 const firstRank = { 'w': V.size.x - 1, 'b': 0 };
7ddfec38 619 for (let v of move.vanish) {
7b53b5a7
BA
620 if (v.p == V.KING) this.castleFlags[v.c] = [V.size.y, V.size.y];
621 else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) {
622 const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1);
623 this.castleFlags[v.c][flagIdx] = V.size.y;
7ddfec38 624 }
61656127
BA
625 }
626 }
627
628 undo(move) {
629 this.disaggregateFlags(JSON.parse(move.flags));
630 V.UndoOnBoard(this.board, move);
7ddfec38 631 if (this.subTurn == 1) {
61656127 632 this.turn = V.GetOppCol(this.turn);
7ddfec38 633 this.movesCount--;
61656127 634 }
a443d256 635 else this.firstMove.pop();
7ddfec38 636 this.subTurn = 3 - this.subTurn;
61656127
BA
637 this.postUndo(move);
638 }
7b53b5a7
BA
639
640 postUndo(move) {
641 // (Potentially) Reset king position
642 for (let v of move.vanish)
643 if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y];
644 }
645
ad1e629e
BA
646 getComputerMove() {
647 let moves = this.getAllValidMoves();
648 if (moves.length == 0) return null;
649 // "Search" at depth 1 for now
650 const maxeval = V.INFINITY;
651 const color = this.turn;
652 const emptyMove = {
653 start: { x: -1, y: -1 },
654 end: { x: -1, y: -1 },
655 appear: [],
656 vanish: []
657 };
658 moves.forEach(m => {
659 this.play(m);
660 m.eval = (color == "w" ? -1 : 1) * maxeval;
661 const moves2 = this.getAllValidMoves().concat([emptyMove]);
662 m.next = moves2[0];
663 moves2.forEach(m2 => {
664 this.play(m2);
665 const score = this.getCurrentScore();
666 let mvEval = 0;
667 if (score != "1/2") {
668 if (score != "*") mvEval = (score == "1-0" ? 1 : -1) * maxeval;
669 else mvEval = this.evalPosition();
670 }
671 if (
672 (color == 'w' && mvEval > m.eval) ||
673 (color == 'b' && mvEval < m.eval)
674 ) {
675 m.eval = mvEval;
676 m.next = m2;
677 }
678 this.undo(m2);
679 });
680 this.undo(m);
681 });
682 moves.sort((a, b) => {
683 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
684 });
685 let candidates = [0];
686 for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++)
687 candidates.push(i);
688 const mIdx = candidates[randInt(candidates.length)];
689 const move2 = moves[mIdx].next;
690 delete moves[mIdx]["next"];
691 return [moves[mIdx], move2];
692 }
693
7b53b5a7
BA
694 getNotation(move) {
695 if (move.start.x < 0)
696 // A second move is always required, but may be empty
697 return "-";
698 const initialSquare = V.CoordsToSquare(move.start);
699 const finalSquare = V.CoordsToSquare(move.end);
700 if (move.appear.length == 0)
701 // Pushed or pulled out of the board
702 return initialSquare + "R";
703 return move.appear[0].p.toUpperCase() + initialSquare + finalSquare;
704 }
0d5335de 705};