Remove some redundant code [Checkered/Magnetic still buggish]
[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
BA
32 {
33 // King cannot capture:
34 let moves = [];
35 let [sizeX,sizeY] = VariantRules.size;
36 const steps = VariantRules.steps[VariantRules.QUEEN];
37 for (let step of steps)
38 {
39 var i = x + step[0];
40 var j = y + step[1];
41 if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == VariantRules.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 {
46302e64 49 if (this.getPiece(sq[0],sq[1]) == VariantRules.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);
61
62 const c = this.getColor(move.start.x,move.start.y);
63 // Next condition to avoid conflicts with harmless castle moves
64 if (c != this.getColor(move.end.x,move.end.y)
65 && this.board[move.end.x][move.end.y] != VariantRules.EMPTY)
66 {
67 const oppCol = this.getOppCol(c);
68 const oppFirstRank = (oppCol == "w" ? 7 : 0);
69
70 // Did we explode our king ? (TODO: remove move earlier)
71 if (Math.abs(this.kingPos[c][0]-move.end.x) <= 1
72 && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
73 {
74 this.kingPos[c] = [-1,-1];
2526c041 75 this.castleFlags[c] = [false,false];
1d184b4c
BA
76 }
77
78 // Did we explode opponent king ?
79 if (Math.abs(this.kingPos[oppCol][0]-move.end.x) <= 1
80 && Math.abs(this.kingPos[oppCol][1]-move.end.y) <= 1)
81 {
82 this.kingPos[oppCol] = [-1,-1];
2526c041 83 this.castleFlags[oppCol] = [false,false];
1d184b4c
BA
84 }
85 else
86 {
87 // Now check if opponent init rook(s) exploded
88 if (Math.abs(move.end.x-oppFirstRank) <= 1)
89 {
90 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][0]) <= 1)
2526c041 91 this.castleFlags[oppCol][0] = false;
1d184b4c 92 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][1]) <= 1)
2526c041 93 this.castleFlags[oppCol][1] = false;
1d184b4c
BA
94 }
95 }
96 }
97 }
98
d3334c3a 99 unupdateVariables(move)
1d184b4c 100 {
d3334c3a 101 super.unupdateVariables(move);
1d184b4c
BA
102 const c = this.getColor(move.start.x,move.start.y);
103 const oppCol = this.getOppCol(c);
104 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
105 {
106 // There is a chance that last move blowed some king away..
107 for (let psq of move.vanish)
108 {
109 if (psq.p == 'k')
110 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
111 }
112 }
113 }
114
46302e64 115 underCheck(move)
1d184b4c 116 {
46302e64 117 const c = this.turn;
1d184b4c
BA
118 const oppCol = this.getOppCol(c);
119 this.play(move);
120 let res = undefined;
121 // If our king disappeared, move is not valid
122 if (this.kingPos[c][0] < 0)
123 res = true;
124 // If opponent king disappeared, move is valid
125 else if (this.kingPos[oppCol][0] < 0)
126 res = false;
127 // Otherwise, if we remain under check, move is not valid
128 else
129 res = this.isAttacked(this.kingPos[c], oppCol);
130 this.undo(move);
131 return res;
132 }
133
46302e64 134 getCheckSquares(move)
4b5fe306 135 {
46302e64 136 const c = this.getOppCol(this.turn);
1af36beb
BA
137 // King might explode:
138 const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
4b5fe306
BA
139 this.play(move);
140 let res = [ ];
141 if (this.kingPos[c][0] < 0)
142 res = [saveKingPos];
143 else if (this.isAttacked(this.kingPos[c], this.getOppCol(c)))
1af36beb 144 res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
4b5fe306
BA
145 this.undo(move);
146 return res;
147 }
148
46302e64 149 checkGameEnd()
1d184b4c 150 {
46302e64 151 const color = this.turn;
1d184b4c
BA
152 const kp = this.kingPos[color];
153 if (kp[0] < 0) //king disappeared
154 return color == "w" ? "0-1" : "1-0";
155 if (!this.isAttacked(kp, this.getOppCol(color)))
156 return "1/2";
157 // Checkmate
158 return color == "w" ? "0-1" : "1-0";
159 }
160}