0fd7647e62414ec5edf60f38604a9a640d422ee5
[vchess.git] / public / javascripts / variants / Atomic.js
1 class AtomicRules extends ChessRules
2 {
3 getPotentialMovesFrom([x,y], c, lastMove)
4 {
5 let moves = super.getPotentialMovesFrom([x,y], c, lastMove);
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
31 getPotentialKingMoves(x, y, c)
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)
42 moves.push(this.getBasicMove(x, y, i, j));
43 }
44 return moves.concat(this.getCastleMoves(x,y,c));
45 }
46
47 isAttacked(sq, color)
48 {
49 if (this.getPiece(sq[0],sq[1]) == VariantRules.KING && this.isAttackedByKing(sq, color))
50 return false; //king cannot take...
51 return (this.isAttackedByPawn(sq, color)
52 || this.isAttackedByRook(sq, color)
53 || this.isAttackedByKnight(sq, color)
54 || this.isAttackedByBishop(sq, color)
55 || this.isAttackedByQueen(sq, color));
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];
75 this.flags[c] = [false,false];
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];
83 this.flags[oppCol] = [false,false];
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)
91 this.flags[oppCol][0] = false;
92 if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][1]) <= 1)
93 this.flags[oppCol][1] = false;
94 }
95 }
96 }
97 }
98
99 unupdateVariables(move)
100 {
101 super.unupdateVariables(move);
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
115 underCheck(move, c)
116 {
117 const oppCol = this.getOppCol(c);
118 this.play(move);
119 let res = undefined;
120 // If our king disappeared, move is not valid
121 if (this.kingPos[c][0] < 0)
122 res = true;
123 // If opponent king disappeared, move is valid
124 else if (this.kingPos[oppCol][0] < 0)
125 res = false;
126 // Otherwise, if we remain under check, move is not valid
127 else
128 res = this.isAttacked(this.kingPos[c], oppCol);
129 this.undo(move);
130 return res;
131 }
132
133 getCheckSquares(move, c)
134 {
135 const saveKingPos = this.kingPos[c]; //king might explode
136 this.play(move);
137 let res = [ ];
138 if (this.kingPos[c][0] < 0)
139 res = [saveKingPos];
140 else if (this.isAttacked(this.kingPos[c], this.getOppCol(c)))
141 res = [ this.kingPos[c] ]
142 this.undo(move);
143 return res;
144 }
145
146 checkGameEnd(color)
147 {
148 const kp = this.kingPos[color];
149 if (kp[0] < 0) //king disappeared
150 return color == "w" ? "0-1" : "1-0";
151 if (!this.isAttacked(kp, this.getOppCol(color)))
152 return "1/2";
153 // Checkmate
154 return color == "w" ? "0-1" : "1-0";
155 }
156 }