87c980979cc8d0cbf870477dfaa3c1196a31b60b
[vchess.git] / public / javascripts / variants / Atomic.js
1 class AtomicRules extends ChessRules
2 {
3 getPotentialMovesFrom([x,y])
4 {
5 let moves = super.getPotentialMovesFrom([x,y]);
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 (V.OnBoard(x,y) && this.board[x][y] != V.EMPTY
18 && this.getPiece(x,y) != V.PAWN)
19 {
20 m.vanish.push(
21 new PiPo({p:this.getPiece(x,y),c:this.getColor(x,y),x:x,y:y}));
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
32 getPotentialKingMoves([x,y])
33 {
34 // King cannot capture:
35 let moves = [];
36 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
37 for (let step of steps)
38 {
39 const i = x + step[0];
40 const j = y + step[1];
41 if (V.OnBoard(i,j) && this.board[i][j] == V.EMPTY)
42 moves.push(this.getBasicMove([x,y], [i,j]));
43 }
44 return moves.concat(this.getCastleMoves([x,y]));
45 }
46
47 isAttacked(sq, colors)
48 {
49 if (this.getPiece(sq[0],sq[1]) == V.KING && this.isAttackedByKing(sq, colors))
50 return false; //king cannot take...
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));
56 }
57
58 updateVariables(move)
59 {
60 super.updateVariables(move);
61 const color = move.vanish[0].c;
62 if (move.appear.length == 0) //capture
63 {
64 const firstRank = {"w": 7, "b": 0};
65 for (let c of ["w","b"])
66 {
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)
70 {
71 this.kingPos[c] = [-1,-1];
72 this.castleFlags[c] = [false,false];
73 }
74 else
75 {
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 }
84 }
85 }
86 }
87 }
88
89 unupdateVariables(move)
90 {
91 super.unupdateVariables(move);
92 const c = move.vanish[0].c;
93 const oppCol = V.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
105 underCheck(color)
106 {
107 const oppCol = V.GetOppCol(color);
108 let res = undefined;
109 // If our king disappeared, move is not valid
110 if (this.kingPos[color][0] < 0)
111 res = true;
112 // If opponent king disappeared, move is valid
113 else if (this.kingPos[oppCol][0] < 0)
114 res = false;
115 // Otherwise, if we remain under check, move is not valid
116 else
117 res = this.isAttacked(this.kingPos[color], [oppCol]);
118 return res;
119 }
120
121 getCheckSquares(color)
122 {
123 let res = [ ];
124 if (this.kingPos[color][0] >= 0 //king might have exploded
125 && this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
126 {
127 res = [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
128 }
129 return res;
130 }
131
132 checkGameEnd()
133 {
134 const color = this.turn;
135 const kp = this.kingPos[color];
136 if (kp[0] < 0) //king disappeared
137 return color == "w" ? "0-1" : "1-0";
138 if (!this.isAttacked(kp, [V.GetOppCol(color)]))
139 return "1/2";
140 return color == "w" ? "0-1" : "1-0"; //checkmate
141 }
142 }