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