f42d593f4a43448d6e9dbf97dda2120eb385aa89
[vchess.git] / client / src / variants / Atomic.js
1 import { ChessRules, PiPo } from "@/base_rules";
2
3 export const VariantRules = class AtomicRules extends ChessRules
4 {
5 getPotentialMovesFrom([x,y])
6 {
7 let moves = super.getPotentialMovesFrom([x,y]);
8
9 // Handle explosions
10 moves.forEach(m => {
11 if (m.vanish.length > 1 && m.appear.length <= 1) //avoid castles
12 {
13 // Explosion! TODO(?): drop moves which explode our king here
14 let steps = [ [-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1] ];
15 for (let step of steps)
16 {
17 let x = m.end.x + step[0];
18 let y = m.end.y + step[1];
19 if (V.OnBoard(x,y) && this.board[x][y] != V.EMPTY
20 && this.getPiece(x,y) != V.PAWN)
21 {
22 m.vanish.push(
23 new PiPo({p:this.getPiece(x,y),c:this.getColor(x,y),x:x,y:y}));
24 }
25 }
26 m.end = {x:m.appear[0].x, y:m.appear[0].y};
27 m.appear.pop(); //Nothin appears in this case
28 }
29 });
30
31 return moves;
32 }
33
34 getPotentialKingMoves([x,y])
35 {
36 // King cannot capture:
37 let moves = [];
38 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
39 for (let step of steps)
40 {
41 const i = x + step[0];
42 const j = y + step[1];
43 if (V.OnBoard(i,j) && this.board[i][j] == V.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]) == V.KING && this.isAttackedByKing(sq, colors))
52 return false; //king cannot take...
53 return (this.isAttackedByPawn(sq, colors)
54 || this.isAttackedByRook(sq, colors)
55 || this.isAttackedByKnight(sq, colors)
56 || this.isAttackedByBishop(sq, colors)
57 || this.isAttackedByQueen(sq, colors));
58 }
59
60 updateVariables(move)
61 {
62 super.updateVariables(move);
63 const color = move.vanish[0].c;
64 if (move.appear.length == 0) //capture
65 {
66 const firstRank = {"w": 7, "b": 0};
67 for (let c of ["w","b"])
68 {
69 // Did we explode king of color c ? (TODO: remove move earlier)
70 if (Math.abs(this.kingPos[c][0]-move.end.x) <= 1
71 && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
72 {
73 this.kingPos[c] = [-1,-1];
74 this.castleFlags[c] = [false,false];
75 }
76 else
77 {
78 // Now check if init rook(s) exploded
79 if (Math.abs(move.end.x-firstRank[c]) <= 1)
80 {
81 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][0]) <= 1)
82 this.castleFlags[c][0] = false;
83 if (Math.abs(move.end.y-this.INIT_COL_ROOK[c][1]) <= 1)
84 this.castleFlags[c][1] = false;
85 }
86 }
87 }
88 }
89 }
90
91 unupdateVariables(move)
92 {
93 super.unupdateVariables(move);
94 const c = move.vanish[0].c;
95 const oppCol = V.GetOppCol(c);
96 if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
97 {
98 // There is a chance that last move blowed some king away..
99 for (let psq of move.vanish)
100 {
101 if (psq.p == 'k')
102 this.kingPos[psq.c==c ? c : oppCol] = [psq.x, psq.y];
103 }
104 }
105 }
106
107 underCheck(color)
108 {
109 const oppCol = V.GetOppCol(color);
110 let res = undefined;
111 // If our king disappeared, move is not valid
112 if (this.kingPos[color][0] < 0)
113 res = true;
114 // If opponent king disappeared, move is valid
115 else if (this.kingPos[oppCol][0] < 0)
116 res = false;
117 // Otherwise, if we remain under check, move is not valid
118 else
119 res = this.isAttacked(this.kingPos[color], [oppCol]);
120 return res;
121 }
122
123 getCheckSquares(color)
124 {
125 let res = [ ];
126 if (this.kingPos[color][0] >= 0 //king might have exploded
127 && this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
128 {
129 res = [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
130 }
131 return res;
132 }
133
134 getCurrentScore()
135 {
136 const color = this.turn;
137 const kp = this.kingPos[color];
138 if (kp[0] < 0) //king disappeared
139 return color == "w" ? "0-1" : "1-0";
140 if (this.atLeastOneMove()) // game not over
141 return "*";
142 if (!this.isAttacked(kp, [V.GetOppCol(color)]))
143 return "1/2";
144 return color == "w" ? "0-1" : "1-0"; //checkmate
145 }
146 }