First commit
[vchess.git] / public / javascripts / variants / Atomic.js
CommitLineData
1d184b4c
BA
1class 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 undo(move)
100 {
101 super.undo(move);
102
103 const c = this.getColor(move.start.x,move.start.y);
104 const oppCol = this.getOppCol(c);
105 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
106 {
107 // There is a chance that last move blowed some king away..
108 for (let psq of move.vanish)
109 {
110 if (psq.p == 'k')
111 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
112 }
113 }
114 }
115
116 underCheck(move, c)
117 {
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
134 checkGameEnd(color)
135 {
136 const kp = this.kingPos[color];
137 if (kp[0] < 0) //king disappeared
138 return color == "w" ? "0-1" : "1-0";
139 if (!this.isAttacked(kp, this.getOppCol(color)))
140 return "1/2";
141 // Checkmate
142 return color == "w" ? "0-1" : "1-0";
143 }
144}