Add Wormhole1 + ranem Wormhole --> Wormhole2. Rename Selfabsorption (too long) into...
[vchess.git] / client / src / variants / Atomic1.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules, PiPo } from "@/base_rules";
2
0fb43db7 3export class Atomic1Rules extends ChessRules {
7e8a7ea1 4
6808d7a1
BA
5 getPotentialMovesFrom([x, y]) {
6 let moves = super.getPotentialMovesFrom([x, y]);
0c3fe8a6 7
dac39588
BA
8 // Handle explosions
9 moves.forEach(m => {
8b405c81 10 // NOTE: if vanish.length==2 and appear.length==2, this is castle
6808d7a1 11 if (m.vanish.length > 1 && m.appear.length <= 1) {
8b405c81 12 // Explosion! (TODO?: drop moves which explode our king here)
6808d7a1
BA
13 let steps = [
14 [-1, -1],
15 [-1, 0],
16 [-1, 1],
17 [0, -1],
18 [0, 1],
19 [1, -1],
20 [1, 0],
21 [1, 1]
22 ];
23 for (let step of steps) {
dac39588
BA
24 let x = m.end.x + step[0];
25 let y = m.end.y + step[1];
6808d7a1
BA
26 if (
27 V.OnBoard(x, y) &&
28 this.board[x][y] != V.EMPTY &&
29 this.getPiece(x, y) != V.PAWN
30 ) {
dac39588 31 m.vanish.push(
6808d7a1
BA
32 new PiPo({
33 p: this.getPiece(x, y),
34 c: this.getColor(x, y),
35 x: x,
36 y: y
37 })
38 );
dac39588
BA
39 }
40 }
6808d7a1 41 m.end = { x: m.appear[0].x, y: m.appear[0].y };
dac39588
BA
42 m.appear.pop(); //Nothin appears in this case
43 }
44 });
0c3fe8a6 45
dac39588
BA
46 return moves;
47 }
0c3fe8a6 48
6808d7a1 49 getPotentialKingMoves([x, y]) {
dac39588
BA
50 // King cannot capture:
51 let moves = [];
52 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
6808d7a1 53 for (let step of steps) {
dac39588
BA
54 const i = x + step[0];
55 const j = y + step[1];
6808d7a1
BA
56 if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY)
57 moves.push(this.getBasicMove([x, y], [i, j]));
dac39588 58 }
6808d7a1 59 return moves.concat(this.getCastleMoves([x, y]));
dac39588 60 }
0c3fe8a6 61
68e19a44 62 isAttacked(sq, color) {
6808d7a1
BA
63 if (
64 this.getPiece(sq[0], sq[1]) == V.KING &&
68e19a44
BA
65 this.isAttackedByKing(sq, color)
66 ) {
67 // A king next to the enemy king is immune to attacks
68 return false;
69 }
6808d7a1 70 return (
68e19a44
BA
71 this.isAttackedByPawn(sq, color) ||
72 this.isAttackedByRook(sq, color) ||
73 this.isAttackedByKnight(sq, color) ||
74 this.isAttackedByBishop(sq, color) ||
75 this.isAttackedByQueen(sq, color)
76 // No "attackedByKing": it cannot take
6808d7a1 77 );
dac39588 78 }
0c3fe8a6 79
3a2a7b5f
BA
80 postPlay(move) {
81 super.postPlay(move);
0fb43db7
BA
82 // NOTE: (harmless) condition on movesCount for Atomic2
83 if (move.appear.length == 0 && this.movesCount >= 2) {
e9b736ee 84 // Capture
6808d7a1
BA
85 const firstRank = { w: 7, b: 0 };
86 for (let c of ["w", "b"]) {
dac39588 87 // Did we explode king of color c ? (TODO: remove move earlier)
6808d7a1
BA
88 if (
89 Math.abs(this.kingPos[c][0] - move.end.x) <= 1 &&
90 Math.abs(this.kingPos[c][1] - move.end.y) <= 1
91 ) {
92 this.kingPos[c] = [-1, -1];
3a2a7b5f 93 this.castleFlags[c] = [8, 8];
cb1165b4
BA
94 }
95 else {
dac39588 96 // Now check if init rook(s) exploded
6808d7a1 97 if (Math.abs(move.end.x - firstRank[c]) <= 1) {
3a2a7b5f
BA
98 if (Math.abs(move.end.y - this.castleFlags[c][0]) <= 1)
99 this.castleFlags[c][0] = 8;
100 if (Math.abs(move.end.y - this.castleFlags[c][1]) <= 1)
101 this.castleFlags[c][1] = 8;
dac39588
BA
102 }
103 }
104 }
105 }
106 }
0c3fe8a6 107
3a2a7b5f
BA
108 postUndo(move) {
109 super.postUndo(move);
110 const c = this.turn;
dac39588 111 const oppCol = V.GetOppCol(c);
0fb43db7
BA
112 // NOTE: condition on movesCount for Atomic2
113 if (
114 this.movesCount >= 1 &&
115 [this.kingPos[c][0], this.kingPos[oppCol][0]].some(e => e < 0)
116 ) {
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
af34341d
BA
137 getCheckSquares() {
138 const color = this.turn;
6808d7a1
BA
139 let res = [];
140 if (
141 this.kingPos[color][0] >= 0 && //king might have exploded
68e19a44 142 this.isAttacked(this.kingPos[color], V.GetOppCol(color))
6808d7a1
BA
143 ) {
144 res = [JSON.parse(JSON.stringify(this.kingPos[color]))];
dac39588
BA
145 }
146 return res;
147 }
0c3fe8a6 148
6808d7a1 149 getCurrentScore() {
dac39588
BA
150 const color = this.turn;
151 const kp = this.kingPos[color];
6808d7a1 152 if (kp[0] < 0)
e9b736ee 153 // King disappeared
dac39588 154 return color == "w" ? "0-1" : "1-0";
bb688df5 155 if (this.atLeastOneMove()) 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 }
7e8a7ea1 159
6808d7a1 160};