Implemented a very basic DarkBot + a few fixes + advance on rules
[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];
0b7d99ec
BA
17 if (V.OnBoard(x,y) && this.board[x][y] != V.EMPTY
18 && this.getPiece(x,y) != V.PAWN)
1d184b4c 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
BA
33 {
34 // King cannot capture:
35 let moves = [];
a37076f1 36 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
1d184b4c
BA
37 for (let step of steps)
38 {
0b7d99ec
BA
39 const i = x + step[0];
40 const j = y + step[1];
41 if (V.OnBoard(i,j) && this.board[i][j] == V.EMPTY)
46302e64 42 moves.push(this.getBasicMove([x,y], [i,j]));
1d184b4c 43 }
46302e64 44 return moves.concat(this.getCastleMoves([x,y]));
1d184b4c
BA
45 }
46
46302e64 47 isAttacked(sq, colors)
1d184b4c 48 {
0b7d99ec 49 if (this.getPiece(sq[0],sq[1]) == V.KING && this.isAttackedByKing(sq, colors))
1d184b4c 50 return false; //king cannot take...
46302e64
BA
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));
1d184b4c
BA
56 }
57
58 updateVariables(move)
59 {
60 super.updateVariables(move);
388e4c40 61 const color = move.vanish[0].c;
ffd4c71f 62 if (move.appear.length == 0) //capture
1d184b4c 63 {
1147d895
BA
64 const firstRank = {"w": 7, "b": 0};
65 for (let c of ["w","b"])
1d184b4c 66 {
1147d895
BA
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)
383387d2 70 {
1147d895
BA
71 this.kingPos[c] = [-1,-1];
72 this.castleFlags[c] = [false,false];
383387d2 73 }
1147d895 74 else
1d184b4c 75 {
1147d895
BA
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 }
1d184b4c
BA
84 }
85 }
86 }
87 }
88
d3334c3a 89 unupdateVariables(move)
1d184b4c 90 {
d3334c3a 91 super.unupdateVariables(move);
388e4c40 92 const c = move.vanish[0].c;
1d184b4c
BA
93 const oppCol = this.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
f6dbe8e3 105 underCheck(color)
1d184b4c 106 {
f6dbe8e3 107 const oppCol = this.getOppCol(color);
1d184b4c
BA
108 let res = undefined;
109 // If our king disappeared, move is not valid
f6dbe8e3 110 if (this.kingPos[color][0] < 0)
1d184b4c
BA
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
f6dbe8e3 117 res = this.isAttacked(this.kingPos[color], [oppCol]);
1d184b4c
BA
118 return res;
119 }
120
f6dbe8e3 121 getCheckSquares(color)
4b5fe306 122 {
4b5fe306 123 let res = [ ];
f6dbe8e3
BA
124 if (this.kingPos[color][0] >= 0 //king might have exploded
125 && this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
126 {
127 res = [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
128 }
4b5fe306
BA
129 return res;
130 }
131
46302e64 132 checkGameEnd()
1d184b4c 133 {
46302e64 134 const color = this.turn;
1d184b4c
BA
135 const kp = this.kingPos[color];
136 if (kp[0] < 0) //king disappeared
137 return color == "w" ? "0-1" : "1-0";
cf130369 138 if (!this.isAttacked(kp, [this.getOppCol(color)]))
1d184b4c 139 return "1/2";
92342261 140 return color == "w" ? "0-1" : "1-0"; //checkmate
1d184b4c
BA
141 }
142}
643479f8
BA
143
144const VariantRules = AtomicRules;