Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules, PiPo } from "@/base_rules"; |
2 | ||
0fb43db7 | 3 | export class Atomic1Rules extends ChessRules { |
7e8a7ea1 | 4 | |
6808d7a1 BA |
5 | getPotentialMovesFrom([x, y]) { |
6 | let moves = super.getPotentialMovesFrom([x, y]); | |
0c3fe8a6 | 7 | |
ded43c88 BA |
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 | ||
dac39588 BA |
19 | // Handle explosions |
20 | moves.forEach(m => { | |
8b405c81 | 21 | // NOTE: if vanish.length==2 and appear.length==2, this is castle |
6808d7a1 | 22 | if (m.vanish.length > 1 && m.appear.length <= 1) { |
8b405c81 | 23 | // Explosion! (TODO?: drop moves which explode our king here) |
6808d7a1 BA |
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) { | |
dac39588 BA |
35 | let x = m.end.x + step[0]; |
36 | let y = m.end.y + step[1]; | |
6808d7a1 BA |
37 | if ( |
38 | V.OnBoard(x, y) && | |
39 | this.board[x][y] != V.EMPTY && | |
40 | this.getPiece(x, y) != V.PAWN | |
41 | ) { | |
dac39588 | 42 | m.vanish.push( |
6808d7a1 BA |
43 | new PiPo({ |
44 | p: this.getPiece(x, y), | |
45 | c: this.getColor(x, y), | |
46 | x: x, | |
47 | y: y | |
48 | }) | |
49 | ); | |
dac39588 BA |
50 | } |
51 | } | |
6808d7a1 | 52 | m.end = { x: m.appear[0].x, y: m.appear[0].y }; |
dac39588 BA |
53 | m.appear.pop(); //Nothin appears in this case |
54 | } | |
55 | }); | |
0c3fe8a6 | 56 | |
dac39588 BA |
57 | return moves; |
58 | } | |
0c3fe8a6 | 59 | |
6808d7a1 | 60 | getPotentialKingMoves([x, y]) { |
dac39588 BA |
61 | // King cannot capture: |
62 | let moves = []; | |
63 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
6808d7a1 | 64 | for (let step of steps) { |
dac39588 BA |
65 | const i = x + step[0]; |
66 | const j = y + step[1]; | |
6808d7a1 BA |
67 | if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) |
68 | moves.push(this.getBasicMove([x, y], [i, j])); | |
dac39588 | 69 | } |
6808d7a1 | 70 | return moves.concat(this.getCastleMoves([x, y])); |
dac39588 | 71 | } |
0c3fe8a6 | 72 | |
68e19a44 | 73 | isAttacked(sq, color) { |
6808d7a1 BA |
74 | if ( |
75 | this.getPiece(sq[0], sq[1]) == V.KING && | |
68e19a44 BA |
76 | this.isAttackedByKing(sq, color) |
77 | ) { | |
78 | // A king next to the enemy king is immune to attacks | |
79 | return false; | |
80 | } | |
6808d7a1 | 81 | return ( |
68e19a44 BA |
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 | |
6808d7a1 | 88 | ); |
dac39588 | 89 | } |
0c3fe8a6 | 90 | |
3a2a7b5f BA |
91 | postPlay(move) { |
92 | super.postPlay(move); | |
0fb43db7 BA |
93 | // NOTE: (harmless) condition on movesCount for Atomic2 |
94 | if (move.appear.length == 0 && this.movesCount >= 2) { | |
e9b736ee | 95 | // Capture |
6808d7a1 BA |
96 | const firstRank = { w: 7, b: 0 }; |
97 | for (let c of ["w", "b"]) { | |
dac39588 | 98 | // Did we explode king of color c ? (TODO: remove move earlier) |
6808d7a1 BA |
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]; | |
3a2a7b5f | 104 | this.castleFlags[c] = [8, 8]; |
cb1165b4 BA |
105 | } |
106 | else { | |
dac39588 | 107 | // Now check if init rook(s) exploded |
6808d7a1 | 108 | if (Math.abs(move.end.x - firstRank[c]) <= 1) { |
3a2a7b5f BA |
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; | |
dac39588 BA |
113 | } |
114 | } | |
115 | } | |
116 | } | |
117 | } | |
0c3fe8a6 | 118 | |
3a2a7b5f BA |
119 | postUndo(move) { |
120 | super.postUndo(move); | |
121 | const c = this.turn; | |
dac39588 | 122 | const oppCol = V.GetOppCol(c); |
0fb43db7 BA |
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 | ) { | |
dac39588 | 128 | // There is a chance that last move blowed some king away.. |
6808d7a1 BA |
129 | for (let psq of move.vanish) { |
130 | if (psq.p == "k") | |
131 | this.kingPos[psq.c == c ? c : oppCol] = [psq.x, psq.y]; | |
dac39588 BA |
132 | } |
133 | } | |
134 | } | |
0c3fe8a6 | 135 | |
6808d7a1 | 136 | underCheck(color) { |
dac39588 BA |
137 | const oppCol = V.GetOppCol(color); |
138 | let res = undefined; | |
139 | // If our king disappeared, move is not valid | |
6808d7a1 | 140 | if (this.kingPos[color][0] < 0) res = true; |
dac39588 | 141 | // If opponent king disappeared, move is valid |
6808d7a1 | 142 | else if (this.kingPos[oppCol][0] < 0) res = false; |
dac39588 | 143 | // Otherwise, if we remain under check, move is not valid |
68e19a44 | 144 | else res = this.isAttacked(this.kingPos[color], oppCol); |
dac39588 BA |
145 | return res; |
146 | } | |
0c3fe8a6 | 147 | |
af34341d BA |
148 | getCheckSquares() { |
149 | const color = this.turn; | |
6808d7a1 BA |
150 | let res = []; |
151 | if ( | |
152 | this.kingPos[color][0] >= 0 && //king might have exploded | |
68e19a44 | 153 | this.isAttacked(this.kingPos[color], V.GetOppCol(color)) |
6808d7a1 BA |
154 | ) { |
155 | res = [JSON.parse(JSON.stringify(this.kingPos[color]))]; | |
dac39588 BA |
156 | } |
157 | return res; | |
158 | } | |
0c3fe8a6 | 159 | |
6808d7a1 | 160 | getCurrentScore() { |
dac39588 BA |
161 | const color = this.turn; |
162 | const kp = this.kingPos[color]; | |
6808d7a1 | 163 | if (kp[0] < 0) |
e9b736ee | 164 | // King disappeared |
dac39588 | 165 | return color == "w" ? "0-1" : "1-0"; |
bb688df5 | 166 | if (this.atLeastOneMove()) return "*"; |
68e19a44 | 167 | if (!this.isAttacked(kp, V.GetOppCol(color))) return "1/2"; |
dac39588 BA |
168 | return color == "w" ? "0-1" : "1-0"; //checkmate |
169 | } | |
7e8a7ea1 | 170 | |
6808d7a1 | 171 | }; |