3aa02d35f5b8095597bf7a3e8e1a585eee677166
[vchess.git] / client / src / variants / Antiking1.js
1 import { ChessRules } from "@/base_rules";
2 import { BerolinaRules } from "@/variants/Berolina";
3 import { ArrayFun } from "@/utils/array";
4 import { randInt } from "@/utils/alea";
5
6 export class Antiking1Rules extends BerolinaRules {
7 static get PawnSpecs() {
8 return Object.assign(
9 {},
10 ChessRules.PawnSpecs,
11 { twoSquares: false }
12 );
13 }
14
15 static get HasCastle() {
16 return false;
17 }
18
19 static get ANTIKING() {
20 return "a";
21 }
22
23 static get PIECES() {
24 return ChessRules.PIECES.concat([V.ANTIKING]);
25 }
26
27 getPpath(b) {
28 return b[1] == "a" ? "Antiking/" + b : b;
29 }
30
31 setOtherVariables(fen) {
32 super.setOtherVariables(fen);
33 this.antikingPos = { w: [-1, -1], b: [-1, -1] };
34 const rows = V.ParseFen(fen).position.split("/");
35 for (let i = 0; i < rows.length; i++) {
36 let k = 0;
37 for (let j = 0; j < rows[i].length; j++) {
38 switch (rows[i].charAt(j)) {
39 case "a":
40 this.antikingPos["b"] = [i, k];
41 break;
42 case "A":
43 this.antikingPos["w"] = [i, k];
44 break;
45 default: {
46 const num = parseInt(rows[i].charAt(j));
47 if (!isNaN(num)) k += num - 1;
48 }
49 }
50 k++;
51 }
52 }
53 }
54
55 // (Anti)King flags at 1 (true) if they can knight-jump
56 setFlags(fenflags) {
57 this.kingFlags = {
58 // King then antiking
59 w: [...Array(2).fill(false)],
60 b: [...Array(2).fill(false)]
61 };
62 for (let c of ["w", "b"]) {
63 for (let i = 0; i < 2; i++)
64 this.kingFlags[c][i] = fenflags.charAt((c == "w" ? 0 : 2) + i) == "1";
65 }
66 }
67
68 aggregateFlags() {
69 return this.kingFlags;
70 }
71
72 disaggregateFlags(flags) {
73 this.kingFlags = flags;
74 }
75
76 getFlagsFen() {
77 // Return kings flags
78 let flags = "";
79 for (let c of ["w", "b"]) {
80 for (let i = 0; i < 2; i++) flags += this.kingFlags[c][i] ? "1" : "0";
81 }
82 return flags;
83 }
84
85 canTake([x1, y1], [x2, y2]) {
86 const piece1 = this.getPiece(x1, y1);
87 const piece2 = this.getPiece(x2, y2);
88 const color1 = this.getColor(x1, y1);
89 const color2 = this.getColor(x2, y2);
90 return (
91 piece2 != "a" &&
92 ((piece1 != "a" && color1 != color2) ||
93 (piece1 == "a" && color1 == color2))
94 );
95 }
96
97 getPotentialMovesFrom([x, y]) {
98 let moves = [];
99 let addKnightJumps = false;
100 const piece = this.getPiece(x, y);
101 const color = this.getColor(x, y);
102 if (piece == V.ANTIKING) {
103 moves = this.getPotentialAntikingMoves([x, y]);
104 addKnightJumps = this.kingFlags[color][1];
105 } else {
106 moves = super.getPotentialMovesFrom([x, y]);
107 if (piece == V.KING) addKnightJumps = this.kingFlags[color][0];
108 }
109 if (addKnightJumps) {
110 // Add potential knight jump to (anti)kings
111 const knightJumps = super.getPotentialKnightMoves([x, y]);
112 // Remove captures (TODO: could be done more efficiently...)
113 moves = moves.concat(knightJumps.filter(m => m.vanish.length == 1));
114 }
115 return moves;
116 }
117
118 getPotentialAntikingMoves(sq) {
119 // The antiking moves like a king (only captured colors differ)
120 return this.getSlideNJumpMoves(
121 sq,
122 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
123 "oneStep"
124 );
125 }
126
127 isAttacked(sq, color) {
128 return (
129 super.isAttacked(sq, color) ||
130 this.isAttackedByAntiking(sq, color)
131 );
132 }
133
134 isAttackedByKing([x, y], color) {
135 // Antiking is not attacked by king:
136 if (this.getPiece(x, y) == V.ANTIKING) return false;
137 return this.isAttackedBySlideNJump(
138 [x, y],
139 color,
140 V.KING,
141 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
142 "oneStep"
143 );
144 }
145
146 isAttackedByAntiking([x, y], color) {
147 // (Anti)King is not attacked by antiking
148 if ([V.KING, V.ANTIKING].includes(this.getPiece(x, y))) return false;
149 return this.isAttackedBySlideNJump(
150 [x, y],
151 color,
152 V.ANTIKING,
153 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
154 "oneStep"
155 );
156 }
157
158 underCheck(color) {
159 const oppCol = V.GetOppCol(color);
160 let res =
161 this.isAttacked(this.kingPos[color], oppCol) ||
162 !this.isAttacked(this.antikingPos[color], oppCol);
163 return res;
164 }
165
166 getCheckSquares(color) {
167 let res = [];
168 const oppCol = V.GetOppCol(color);
169 if (this.isAttacked(this.kingPos[color], oppCol))
170 res.push(JSON.parse(JSON.stringify(this.kingPos[color])));
171 if (!this.isAttacked(this.antikingPos[color], oppCol))
172 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
173 return res;
174 }
175
176 postPlay(move) {
177 super.postPlay(move);
178 const piece = move.vanish[0].p;
179 const c = move.vanish[0].c;
180 // Update antiking position, and kings flags
181 if (piece == V.ANTIKING) {
182 this.antikingPos[c][0] = move.appear[0].x;
183 this.antikingPos[c][1] = move.appear[0].y;
184 this.kingFlags[c][1] = false;
185 } else if (piece == V.KING) this.kingFlags[c][0] = false;
186 }
187
188 postUndo(move) {
189 super.postUndo(move);
190 const c = move.vanish[0].c;
191 if (move.vanish[0].p == V.ANTIKING)
192 this.antikingPos[c] = [move.start.x, move.start.y];
193 }
194
195 static get VALUES() {
196 return Object.assign(
197 { a: 1000 },
198 ChessRules.VALUES
199 );
200 }
201
202 static GenRandInitFen() {
203 // Always deterministic setup
204 return "2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 1111";
205 }
206
207 static get SEARCH_DEPTH() {
208 return 2;
209 }
210 };