Fix Atomic when an init rook is taken
[vchess.git] / public / javascripts / variants / Atomic.js
CommitLineData
1d184b4c
BA
1class 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);
383387d2
BA
69 const firstRank = (c == "w" ? 7 : 0);
70 const oppFirstRank = 7 - firstRank;
1d184b4c
BA
71
72 // Did we explode our king ? (TODO: remove move earlier)
73 if (Math.abs(this.kingPos[c][0]-move.end.x) <= 1
74 && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
75 {
76 this.kingPos[c] = [-1,-1];
2526c041 77 this.castleFlags[c] = [false,false];
1d184b4c 78 }
383387d2
BA
79 else
80 {
81 // Now check if our init rook(s) exploded
82 if (Math.abs(move.end.x-firstRank) <= 1)
83 {
84 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][0]) <= 1)
85 this.castleFlags[c][0] = false;
86 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][1]) <= 1)
87 this.castleFlags[c][1] = false;
88 }
89 }
1d184b4c
BA
90
91 // Did we explode opponent king ?
92 if (Math.abs(this.kingPos[oppCol][0]-move.end.x) <= 1
93 && Math.abs(this.kingPos[oppCol][1]-move.end.y) <= 1)
94 {
95 this.kingPos[oppCol] = [-1,-1];
2526c041 96 this.castleFlags[oppCol] = [false,false];
1d184b4c
BA
97 }
98 else
99 {
100 // Now check if opponent init rook(s) exploded
101 if (Math.abs(move.end.x-oppFirstRank) <= 1)
102 {
103 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][0]) <= 1)
2526c041 104 this.castleFlags[oppCol][0] = false;
1d184b4c 105 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][1]) <= 1)
2526c041 106 this.castleFlags[oppCol][1] = false;
1d184b4c
BA
107 }
108 }
109 }
110 }
111
d3334c3a 112 unupdateVariables(move)
1d184b4c 113 {
d3334c3a 114 super.unupdateVariables(move);
1d184b4c
BA
115 const c = this.getColor(move.start.x,move.start.y);
116 const oppCol = this.getOppCol(c);
117 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
118 {
119 // There is a chance that last move blowed some king away..
120 for (let psq of move.vanish)
121 {
122 if (psq.p == 'k')
123 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
124 }
125 }
126 }
127
46302e64 128 underCheck(move)
1d184b4c 129 {
46302e64 130 const c = this.turn;
1d184b4c
BA
131 const oppCol = this.getOppCol(c);
132 this.play(move);
133 let res = undefined;
134 // If our king disappeared, move is not valid
135 if (this.kingPos[c][0] < 0)
136 res = true;
137 // If opponent king disappeared, move is valid
138 else if (this.kingPos[oppCol][0] < 0)
139 res = false;
140 // Otherwise, if we remain under check, move is not valid
141 else
cf130369 142 res = this.isAttacked(this.kingPos[c], [oppCol]);
1d184b4c
BA
143 this.undo(move);
144 return res;
145 }
146
46302e64 147 getCheckSquares(move)
4b5fe306 148 {
46302e64 149 const c = this.getOppCol(this.turn);
1af36beb
BA
150 // King might explode:
151 const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
4b5fe306
BA
152 this.play(move);
153 let res = [ ];
154 if (this.kingPos[c][0] < 0)
155 res = [saveKingPos];
cf130369 156 else if (this.isAttacked(this.kingPos[c], [this.getOppCol(c)]))
1af36beb 157 res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
4b5fe306
BA
158 this.undo(move);
159 return res;
160 }
161
46302e64 162 checkGameEnd()
1d184b4c 163 {
46302e64 164 const color = this.turn;
1d184b4c
BA
165 const kp = this.kingPos[color];
166 if (kp[0] < 0) //king disappeared
167 return color == "w" ? "0-1" : "1-0";
cf130369 168 if (!this.isAttacked(kp, [this.getOppCol(color)]))
1d184b4c
BA
169 return "1/2";
170 // Checkmate
171 return color == "w" ? "0-1" : "1-0";
172 }
173}