Fix introduced bug when preventing to move when choices are shown
[vchess.git] / client / src / variants / Atomic.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules, PiPo } from "@/base_rules";
2
32f6285e 3export class AtomicRules extends ChessRules {
e727fe31 4 getEpSquare(moveOrSquare) {
bbf66837 5 if (typeof moveOrSquare !== "object" || moveOrSquare.appear.length > 0)
e727fe31
BA
6 return super.getEpSquare(moveOrSquare);
7 // Capturing move: no en-passant
8 return undefined;
9 }
10
6808d7a1
BA
11 getPotentialMovesFrom([x, y]) {
12 let moves = super.getPotentialMovesFrom([x, y]);
0c3fe8a6 13
dac39588
BA
14 // Handle explosions
15 moves.forEach(m => {
8b405c81 16 // NOTE: if vanish.length==2 and appear.length==2, this is castle
6808d7a1 17 if (m.vanish.length > 1 && m.appear.length <= 1) {
8b405c81 18 // Explosion! (TODO?: drop moves which explode our king here)
6808d7a1
BA
19 let steps = [
20 [-1, -1],
21 [-1, 0],
22 [-1, 1],
23 [0, -1],
24 [0, 1],
25 [1, -1],
26 [1, 0],
27 [1, 1]
28 ];
29 for (let step of steps) {
dac39588
BA
30 let x = m.end.x + step[0];
31 let y = m.end.y + step[1];
6808d7a1
BA
32 if (
33 V.OnBoard(x, y) &&
34 this.board[x][y] != V.EMPTY &&
35 this.getPiece(x, y) != V.PAWN
36 ) {
dac39588 37 m.vanish.push(
6808d7a1
BA
38 new PiPo({
39 p: this.getPiece(x, y),
40 c: this.getColor(x, y),
41 x: x,
42 y: y
43 })
44 );
dac39588
BA
45 }
46 }
6808d7a1 47 m.end = { x: m.appear[0].x, y: m.appear[0].y };
dac39588
BA
48 m.appear.pop(); //Nothin appears in this case
49 }
50 });
0c3fe8a6 51
dac39588
BA
52 return moves;
53 }
0c3fe8a6 54
6808d7a1 55 getPotentialKingMoves([x, y]) {
dac39588
BA
56 // King cannot capture:
57 let moves = [];
58 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
6808d7a1 59 for (let step of steps) {
dac39588
BA
60 const i = x + step[0];
61 const j = y + step[1];
6808d7a1
BA
62 if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY)
63 moves.push(this.getBasicMove([x, y], [i, j]));
dac39588 64 }
6808d7a1 65 return moves.concat(this.getCastleMoves([x, y]));
dac39588 66 }
0c3fe8a6 67
68e19a44 68 isAttacked(sq, color) {
6808d7a1
BA
69 if (
70 this.getPiece(sq[0], sq[1]) == V.KING &&
68e19a44
BA
71 this.isAttackedByKing(sq, color)
72 ) {
73 // A king next to the enemy king is immune to attacks
74 return false;
75 }
6808d7a1 76 return (
68e19a44
BA
77 this.isAttackedByPawn(sq, color) ||
78 this.isAttackedByRook(sq, color) ||
79 this.isAttackedByKnight(sq, color) ||
80 this.isAttackedByBishop(sq, color) ||
81 this.isAttackedByQueen(sq, color)
82 // No "attackedByKing": it cannot take
6808d7a1 83 );
dac39588 84 }
0c3fe8a6 85
3a2a7b5f
BA
86 postPlay(move) {
87 super.postPlay(move);
6808d7a1 88 if (move.appear.length == 0) {
e9b736ee 89 // Capture
6808d7a1
BA
90 const firstRank = { w: 7, b: 0 };
91 for (let c of ["w", "b"]) {
dac39588 92 // Did we explode king of color c ? (TODO: remove move earlier)
6808d7a1
BA
93 if (
94 Math.abs(this.kingPos[c][0] - move.end.x) <= 1 &&
95 Math.abs(this.kingPos[c][1] - move.end.y) <= 1
96 ) {
97 this.kingPos[c] = [-1, -1];
3a2a7b5f 98 this.castleFlags[c] = [8, 8];
6808d7a1 99 } else {
dac39588 100 // Now check if init rook(s) exploded
6808d7a1 101 if (Math.abs(move.end.x - firstRank[c]) <= 1) {
3a2a7b5f
BA
102 if (Math.abs(move.end.y - this.castleFlags[c][0]) <= 1)
103 this.castleFlags[c][0] = 8;
104 if (Math.abs(move.end.y - this.castleFlags[c][1]) <= 1)
105 this.castleFlags[c][1] = 8;
dac39588
BA
106 }
107 }
108 }
109 }
110 }
0c3fe8a6 111
3a2a7b5f
BA
112 postUndo(move) {
113 super.postUndo(move);
114 const c = this.turn;
dac39588 115 const oppCol = V.GetOppCol(c);
3a2a7b5f 116 if ([this.kingPos[c][0], this.kingPos[oppCol][0]].some(e => e < 0)) {
dac39588 117 // There is a chance that last move blowed some king away..
6808d7a1
BA
118 for (let psq of move.vanish) {
119 if (psq.p == "k")
120 this.kingPos[psq.c == c ? c : oppCol] = [psq.x, psq.y];
dac39588
BA
121 }
122 }
123 }
0c3fe8a6 124
6808d7a1 125 underCheck(color) {
dac39588
BA
126 const oppCol = V.GetOppCol(color);
127 let res = undefined;
128 // If our king disappeared, move is not valid
6808d7a1 129 if (this.kingPos[color][0] < 0) res = true;
dac39588 130 // If opponent king disappeared, move is valid
6808d7a1 131 else if (this.kingPos[oppCol][0] < 0) res = false;
dac39588 132 // Otherwise, if we remain under check, move is not valid
68e19a44 133 else res = this.isAttacked(this.kingPos[color], oppCol);
dac39588
BA
134 return res;
135 }
0c3fe8a6 136
6808d7a1
BA
137 getCheckSquares(color) {
138 let res = [];
139 if (
140 this.kingPos[color][0] >= 0 && //king might have exploded
68e19a44 141 this.isAttacked(this.kingPos[color], V.GetOppCol(color))
6808d7a1
BA
142 ) {
143 res = [JSON.parse(JSON.stringify(this.kingPos[color]))];
dac39588
BA
144 }
145 return res;
146 }
0c3fe8a6 147
6808d7a1 148 getCurrentScore() {
dac39588
BA
149 const color = this.turn;
150 const kp = this.kingPos[color];
6808d7a1 151 if (kp[0] < 0)
e9b736ee 152 // King disappeared
dac39588 153 return color == "w" ? "0-1" : "1-0";
6808d7a1 154 if (this.atLeastOneMove())
0c3fe8a6 155 return "*";
68e19a44 156 if (!this.isAttacked(kp, V.GetOppCol(color))) return "1/2";
dac39588
BA
157 return color == "w" ? "0-1" : "1-0"; //checkmate
158 }
6808d7a1 159};