TODO: fix draw logic
[vchess.git] / client / src / variants / Atomic_OLD.js
CommitLineData
e2732923
BA
1import { ChessRules } from "@/base_rules";
2export const VariantRules = class AtomicRules extends ChessRules
1d184b4c 3{
46302e64 4 getPotentialMovesFrom([x,y])
1d184b4c 5 {
46302e64 6 let moves = super.getPotentialMovesFrom([x,y]);
1d184b4c
BA
7
8 // Handle explosions
9 moves.forEach(m => {
10 if (m.vanish.length > 1 && m.appear.length <= 1) //avoid castles
11 {
92342261 12 // Explosion! TODO(?): drop moves which explode our king here
1d184b4c
BA
13 let steps = [ [-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1] ];
14 for (let step of steps)
15 {
16 let x = m.end.x + step[0];
17 let y = m.end.y + step[1];
0b7d99ec
BA
18 if (V.OnBoard(x,y) && this.board[x][y] != V.EMPTY
19 && this.getPiece(x,y) != V.PAWN)
1d184b4c 20 {
92342261
BA
21 m.vanish.push(
22 new PiPo({p:this.getPiece(x,y),c:this.getColor(x,y),x:x,y:y}));
1d184b4c
BA
23 }
24 }
25 m.end = {x:m.appear[0].x, y:m.appear[0].y};
26 m.appear.pop(); //Nothin appears in this case
27 }
28 });
29
30 return moves;
31 }
32
46302e64 33 getPotentialKingMoves([x,y])
1d184b4c
BA
34 {
35 // King cannot capture:
36 let moves = [];
a37076f1 37 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
1d184b4c
BA
38 for (let step of steps)
39 {
0b7d99ec
BA
40 const i = x + step[0];
41 const j = y + step[1];
42 if (V.OnBoard(i,j) && this.board[i][j] == V.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 {
0b7d99ec 50 if (this.getPiece(sq[0],sq[1]) == V.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);
388e4c40 62 const color = move.vanish[0].c;
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);
388e4c40 93 const c = move.vanish[0].c;
26b8e4f7 94 const oppCol = V.GetOppCol(c);
1d184b4c
BA
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
f6dbe8e3 106 underCheck(color)
1d184b4c 107 {
26b8e4f7 108 const oppCol = V.GetOppCol(color);
1d184b4c
BA
109 let res = undefined;
110 // If our king disappeared, move is not valid
f6dbe8e3 111 if (this.kingPos[color][0] < 0)
1d184b4c
BA
112 res = true;
113 // If opponent king disappeared, move is valid
114 else if (this.kingPos[oppCol][0] < 0)
115 res = false;
116 // Otherwise, if we remain under check, move is not valid
117 else
f6dbe8e3 118 res = this.isAttacked(this.kingPos[color], [oppCol]);
1d184b4c
BA
119 return res;
120 }
121
f6dbe8e3 122 getCheckSquares(color)
4b5fe306 123 {
4b5fe306 124 let res = [ ];
f6dbe8e3 125 if (this.kingPos[color][0] >= 0 //king might have exploded
26b8e4f7 126 && this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
f6dbe8e3
BA
127 {
128 res = [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
129 }
4b5fe306
BA
130 return res;
131 }
132
46302e64 133 checkGameEnd()
1d184b4c 134 {
46302e64 135 const color = this.turn;
1d184b4c
BA
136 const kp = this.kingPos[color];
137 if (kp[0] < 0) //king disappeared
138 return color == "w" ? "0-1" : "1-0";
26b8e4f7 139 if (!this.isAttacked(kp, [V.GetOppCol(color)]))
1d184b4c 140 return "1/2";
92342261 141 return color == "w" ? "0-1" : "1-0"; //checkmate
1d184b4c
BA
142 }
143}