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