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