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