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