Draft of Ultima chess rules; almost OK HalfChess
[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 (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(
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 const V = VariantRules;
35 // King cannot capture:
36 let moves = [];
37 let [sizeX,sizeY] = V.size;
38 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
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)
44 moves.push(this.getBasicMove([x,y], [i,j]));
45 }
46 return moves.concat(this.getCastleMoves([x,y]));
47 }
48
49 isAttacked(sq, colors)
50 {
51 if (this.getPiece(sq[0],sq[1]) == VariantRules.KING
52 && this.isAttackedByKing(sq, colors))
53 {
54 return false; //king cannot take...
55 }
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));
61 }
62
63 updateVariables(move)
64 {
65 super.updateVariables(move);
66 const color = this.getColor(move.start.x,move.start.y);
67 if (move.appear.length == 0) //capture
68 {
69 const firstRank = {"w": 7, "b": 0};
70 for (let c of ["w","b"])
71 {
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)
75 {
76 this.kingPos[c] = [-1,-1];
77 this.castleFlags[c] = [false,false];
78 }
79 else
80 {
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 }
89 }
90 }
91 }
92 }
93
94 unupdateVariables(move)
95 {
96 super.unupdateVariables(move);
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
110 underCheck(move)
111 {
112 const c = this.turn;
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
124 res = this.isAttacked(this.kingPos[c], [oppCol]);
125 this.undo(move);
126 return res;
127 }
128
129 getCheckSquares(move)
130 {
131 const c = this.getOppCol(this.turn);
132 // King might explode:
133 const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
134 this.play(move);
135 let res = [ ];
136 if (this.kingPos[c][0] < 0)
137 res = [saveKingPos];
138 else if (this.isAttacked(this.kingPos[c], [this.getOppCol(c)]))
139 res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
140 this.undo(move);
141 return res;
142 }
143
144 checkGameEnd()
145 {
146 const color = this.turn;
147 const kp = this.kingPos[color];
148 if (kp[0] < 0) //king disappeared
149 return color == "w" ? "0-1" : "1-0";
150 if (!this.isAttacked(kp, [this.getOppCol(color)]))
151 return "1/2";
152 return color == "w" ? "0-1" : "1-0"; //checkmate
153 }
154 }