[cosmetics] Remove scrollbar on main menu
[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 // Handle explosions
9 moves.forEach(m => {
10 // NOTE: if vanish.length==2 and appear.length==2, this is castle
11 if (m.vanish.length > 1 && m.appear.length <= 1) {
12 // Explosion! (TODO?: drop moves which explode our king here)
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) {
24 let x = m.end.x + step[0];
25 let y = m.end.y + step[1];
26 if (
27 V.OnBoard(x, y) &&
28 this.board[x][y] != V.EMPTY &&
29 this.getPiece(x, y) != V.PAWN
30 ) {
31 m.vanish.push(
32 new PiPo({
33 p: this.getPiece(x, y),
34 c: this.getColor(x, y),
35 x: x,
36 y: y
37 })
38 );
39 }
40 }
41 m.end = { x: m.appear[0].x, y: m.appear[0].y };
42 m.appear.pop(); //Nothin appears in this case
43 }
44 });
45
46 return moves;
47 }
48
49 getPotentialKingMoves([x, y]) {
50 // King cannot capture:
51 let moves = [];
52 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
53 for (let step of steps) {
54 const i = x + step[0];
55 const j = y + step[1];
56 if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY)
57 moves.push(this.getBasicMove([x, y], [i, j]));
58 }
59 return moves.concat(this.getCastleMoves([x, y]));
60 }
61
62 isAttacked(sq, color) {
63 if (
64 this.getPiece(sq[0], sq[1]) == V.KING &&
65 this.isAttackedByKing(sq, color)
66 ) {
67 // A king next to the enemy king is immune to attacks
68 return false;
69 }
70 return (
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
77 );
78 }
79
80 postPlay(move) {
81 super.postPlay(move);
82 // NOTE: (harmless) condition on movesCount for Atomic2
83 if (move.appear.length == 0 && this.movesCount >= 2) {
84 // Capture
85 const firstRank = { w: 7, b: 0 };
86 for (let c of ["w", "b"]) {
87 // Did we explode king of color c ? (TODO: remove move earlier)
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];
93 this.castleFlags[c] = [8, 8];
94 } else {
95 // Now check if init rook(s) exploded
96 if (Math.abs(move.end.x - firstRank[c]) <= 1) {
97 if (Math.abs(move.end.y - this.castleFlags[c][0]) <= 1)
98 this.castleFlags[c][0] = 8;
99 if (Math.abs(move.end.y - this.castleFlags[c][1]) <= 1)
100 this.castleFlags[c][1] = 8;
101 }
102 }
103 }
104 }
105 }
106
107 postUndo(move) {
108 super.postUndo(move);
109 const c = this.turn;
110 const oppCol = V.GetOppCol(c);
111 // NOTE: condition on movesCount for Atomic2
112 if (
113 this.movesCount >= 1 &&
114 [this.kingPos[c][0], this.kingPos[oppCol][0]].some(e => e < 0)
115 ) {
116 // There is a chance that last move blowed some king away..
117 for (let psq of move.vanish) {
118 if (psq.p == "k")
119 this.kingPos[psq.c == c ? c : oppCol] = [psq.x, psq.y];
120 }
121 }
122 }
123
124 underCheck(color) {
125 const oppCol = V.GetOppCol(color);
126 let res = undefined;
127 // If our king disappeared, move is not valid
128 if (this.kingPos[color][0] < 0) res = true;
129 // If opponent king disappeared, move is valid
130 else if (this.kingPos[oppCol][0] < 0) res = false;
131 // Otherwise, if we remain under check, move is not valid
132 else res = this.isAttacked(this.kingPos[color], oppCol);
133 return res;
134 }
135
136 getCheckSquares() {
137 const color = this.turn;
138 let res = [];
139 if (
140 this.kingPos[color][0] >= 0 && //king might have exploded
141 this.isAttacked(this.kingPos[color], V.GetOppCol(color))
142 ) {
143 res = [JSON.parse(JSON.stringify(this.kingPos[color]))];
144 }
145 return res;
146 }
147
148 getCurrentScore() {
149 const color = this.turn;
150 const kp = this.kingPos[color];
151 if (kp[0] < 0)
152 // King disappeared
153 return color == "w" ? "0-1" : "1-0";
154 if (this.atLeastOneMove()) return "*";
155 if (!this.isAttacked(kp, V.GetOppCol(color))) return "1/2";
156 return color == "w" ? "0-1" : "1-0"; //checkmate
157 }
158
159 };