Some code cleaning + clarifying (TODO: work on variables names)
[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 {
92342261 11 // Explosion! TODO(?): drop moves which explode our king here
1d184b4c
BA
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 {
92342261
BA
20 m.vanish.push(
21 new PiPo({p:this.getPiece(x,y),c:this.getColor(x,y),x:x,y:y}));
1d184b4c
BA
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
46302e64 32 getPotentialKingMoves([x,y])
1d184b4c 33 {
a37076f1 34 const V = VariantRules;
1d184b4c
BA
35 // King cannot capture:
36 let moves = [];
a37076f1
BA
37 let [sizeX,sizeY] = V.size;
38 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
1d184b4c
BA
39 for (let step of steps)
40 {
41 var i = x + step[0];
42 var j = y + step[1];
43 if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == VariantRules.EMPTY)
46302e64 44 moves.push(this.getBasicMove([x,y], [i,j]));
1d184b4c 45 }
46302e64 46 return moves.concat(this.getCastleMoves([x,y]));
1d184b4c
BA
47 }
48
46302e64 49 isAttacked(sq, colors)
1d184b4c 50 {
92342261
BA
51 if (this.getPiece(sq[0],sq[1]) == VariantRules.KING
52 && this.isAttackedByKing(sq, colors))
53 {
1d184b4c 54 return false; //king cannot take...
92342261 55 }
46302e64
BA
56 return (this.isAttackedByPawn(sq, colors)
57 || this.isAttackedByRook(sq, colors)
58 || this.isAttackedByKnight(sq, colors)
59 || this.isAttackedByBishop(sq, colors)
60 || this.isAttackedByQueen(sq, colors));
1d184b4c
BA
61 }
62
63 updateVariables(move)
64 {
65 super.updateVariables(move);
1147d895 66 const color = this.getColor(move.start.x,move.start.y);
ffd4c71f 67 if (move.appear.length == 0) //capture
1d184b4c 68 {
1147d895
BA
69 const firstRank = {"w": 7, "b": 0};
70 for (let c of ["w","b"])
1d184b4c 71 {
1147d895
BA
72 // Did we explode king of color c ? (TODO: remove move earlier)
73 if (Math.abs(this.kingPos[c][0]-move.end.x) <= 1
74 && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
383387d2 75 {
1147d895
BA
76 this.kingPos[c] = [-1,-1];
77 this.castleFlags[c] = [false,false];
383387d2 78 }
1147d895 79 else
1d184b4c 80 {
1147d895
BA
81 // Now check if init rook(s) exploded
82 if (Math.abs(move.end.x-firstRank[c]) <= 1)
83 {
84 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][0]) <= 1)
85 this.castleFlags[c][0] = false;
86 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][1]) <= 1)
87 this.castleFlags[c][1] = false;
88 }
1d184b4c
BA
89 }
90 }
91 }
92 }
93
d3334c3a 94 unupdateVariables(move)
1d184b4c 95 {
d3334c3a 96 super.unupdateVariables(move);
1d184b4c
BA
97 const c = this.getColor(move.start.x,move.start.y);
98 const oppCol = this.getOppCol(c);
99 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
100 {
101 // There is a chance that last move blowed some king away..
102 for (let psq of move.vanish)
103 {
104 if (psq.p == 'k')
105 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
106 }
107 }
108 }
109
46302e64 110 underCheck(move)
1d184b4c 111 {
46302e64 112 const c = this.turn;
1d184b4c
BA
113 const oppCol = this.getOppCol(c);
114 this.play(move);
115 let res = undefined;
116 // If our king disappeared, move is not valid
117 if (this.kingPos[c][0] < 0)
118 res = true;
119 // If opponent king disappeared, move is valid
120 else if (this.kingPos[oppCol][0] < 0)
121 res = false;
122 // Otherwise, if we remain under check, move is not valid
123 else
cf130369 124 res = this.isAttacked(this.kingPos[c], [oppCol]);
1d184b4c
BA
125 this.undo(move);
126 return res;
127 }
128
46302e64 129 getCheckSquares(move)
4b5fe306 130 {
46302e64 131 const c = this.getOppCol(this.turn);
1af36beb
BA
132 // King might explode:
133 const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
4b5fe306
BA
134 this.play(move);
135 let res = [ ];
136 if (this.kingPos[c][0] < 0)
137 res = [saveKingPos];
cf130369 138 else if (this.isAttacked(this.kingPos[c], [this.getOppCol(c)]))
1af36beb 139 res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
4b5fe306
BA
140 this.undo(move);
141 return res;
142 }
143
46302e64 144 checkGameEnd()
1d184b4c 145 {
46302e64 146 const color = this.turn;
1d184b4c
BA
147 const kp = this.kingPos[color];
148 if (kp[0] < 0) //king disappeared
149 return color == "w" ? "0-1" : "1-0";
cf130369 150 if (!this.isAttacked(kp, [this.getOppCol(color)]))
1d184b4c 151 return "1/2";
92342261 152 return color == "w" ? "0-1" : "1-0"; //checkmate
1d184b4c
BA
153 }
154}