Improve 'help' modal style on large screens
[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);
1147d895 62 const color = this.getColor(move.start.x,move.start.y);
ffd4c71f 63 if (move.appear.length == 0) //capture
1d184b4c 64 {
1147d895
BA
65 const firstRank = {"w": 7, "b": 0};
66 for (let c of ["w","b"])
1d184b4c 67 {
1147d895
BA
68 // Did we explode king of color c ? (TODO: remove move earlier)
69 if (Math.abs(this.kingPos[c][0]-move.end.x) <= 1
70 && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
383387d2 71 {
1147d895
BA
72 this.kingPos[c] = [-1,-1];
73 this.castleFlags[c] = [false,false];
383387d2 74 }
1147d895 75 else
1d184b4c 76 {
1147d895
BA
77 // Now check if init rook(s) exploded
78 if (Math.abs(move.end.x-firstRank[c]) <= 1)
79 {
80 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][0]) <= 1)
81 this.castleFlags[c][0] = false;
82 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][1]) <= 1)
83 this.castleFlags[c][1] = false;
84 }
1d184b4c
BA
85 }
86 }
87 }
88 }
89
d3334c3a 90 unupdateVariables(move)
1d184b4c 91 {
d3334c3a 92 super.unupdateVariables(move);
1d184b4c
BA
93 const c = this.getColor(move.start.x,move.start.y);
94 const oppCol = this.getOppCol(c);
95 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
96 {
97 // There is a chance that last move blowed some king away..
98 for (let psq of move.vanish)
99 {
100 if (psq.p == 'k')
101 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
102 }
103 }
104 }
105
46302e64 106 underCheck(move)
1d184b4c 107 {
46302e64 108 const c = this.turn;
1d184b4c
BA
109 const oppCol = this.getOppCol(c);
110 this.play(move);
111 let res = undefined;
112 // If our king disappeared, move is not valid
113 if (this.kingPos[c][0] < 0)
114 res = true;
115 // If opponent king disappeared, move is valid
116 else if (this.kingPos[oppCol][0] < 0)
117 res = false;
118 // Otherwise, if we remain under check, move is not valid
119 else
cf130369 120 res = this.isAttacked(this.kingPos[c], [oppCol]);
1d184b4c
BA
121 this.undo(move);
122 return res;
123 }
124
46302e64 125 getCheckSquares(move)
4b5fe306 126 {
46302e64 127 const c = this.getOppCol(this.turn);
1af36beb
BA
128 // King might explode:
129 const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
4b5fe306
BA
130 this.play(move);
131 let res = [ ];
132 if (this.kingPos[c][0] < 0)
133 res = [saveKingPos];
cf130369 134 else if (this.isAttacked(this.kingPos[c], [this.getOppCol(c)]))
1af36beb 135 res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
4b5fe306
BA
136 this.undo(move);
137 return res;
138 }
139
46302e64 140 checkGameEnd()
1d184b4c 141 {
46302e64 142 const color = this.turn;
1d184b4c
BA
143 const kp = this.kingPos[color];
144 if (kp[0] < 0) //king disappeared
145 return color == "w" ? "0-1" : "1-0";
cf130369 146 if (!this.isAttacked(kp, [this.getOppCol(color)]))
1d184b4c
BA
147 return "1/2";
148 // Checkmate
149 return color == "w" ? "0-1" : "1-0";
150 }
151}