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