Draft Ball variant + some fixes, enhancements and code cleaning
[vchess.git] / client / src / variants / Antiking2.js
CommitLineData
0c3fe8a6 1import { ChessRules } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
0c3fe8a6
BA
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class Antiking2Rules extends ChessRules {
6808d7a1
BA
6 static get ANTIKING() {
7 return "a";
8 }
dac39588 9
6808d7a1 10 static get PIECES() {
dac39588
BA
11 return ChessRules.PIECES.concat([V.ANTIKING]);
12 }
13
241bf8f2
BA
14 getPpath(b) {
15 return b[1] == "a" ? "Antiking/" + b : b;
16 }
17
6f2f9437
BA
18 static IsGoodPosition(position) {
19 if (!ChessRules.IsGoodPosition(position)) return false;
20 const rows = position.split("/");
21 // Check that exactly one antiking of each color is there:
22 let antikings = { 'a': 0, 'A': 0 };
23 for (let row of rows) {
24 for (let i = 0; i < row.length; i++)
25 if (['A','a'].includes(row[i])) antikings[row[i]]++;
26 }
27 if (Object.values(antikings).some(v => v != 1)) return false;
28 return true;
29 }
30
6808d7a1 31 setOtherVariables(fen) {
dac39588 32 super.setOtherVariables(fen);
6808d7a1 33 this.antikingPos = { w: [-1, -1], b: [-1, -1] };
dac39588 34 const rows = V.ParseFen(fen).position.split("/");
6808d7a1 35 for (let i = 0; i < rows.length; i++) {
dac39588 36 let k = 0;
6808d7a1
BA
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];
dac39588 41 break;
6808d7a1
BA
42 case "A":
43 this.antikingPos["w"] = [i, k];
dac39588 44 break;
6808d7a1 45 default: {
dac39588 46 const num = parseInt(rows[i].charAt(j));
6808d7a1
BA
47 if (!isNaN(num)) k += num - 1;
48 }
dac39588
BA
49 }
50 k++;
51 }
52 }
53 }
54
6808d7a1
BA
55 canTake([x1, y1], [x2, y2]) {
56 const piece1 = this.getPiece(x1, y1);
57 const piece2 = this.getPiece(x2, y2);
58 const color1 = this.getColor(x1, y1);
59 const color2 = this.getColor(x2, y2);
60 return (
61 piece2 != "a" &&
c583ef1c
BA
62 (
63 (piece1 != "a" && color1 != color2) ||
64 (piece1 == "a" && color1 == color2)
65 )
6808d7a1 66 );
dac39588
BA
67 }
68
6808d7a1
BA
69 getPotentialMovesFrom([x, y]) {
70 switch (this.getPiece(x, y)) {
dac39588 71 case V.ANTIKING:
6808d7a1 72 return this.getPotentialAntikingMoves([x, y]);
dac39588 73 default:
6808d7a1 74 return super.getPotentialMovesFrom([x, y]);
dac39588
BA
75 }
76 }
77
6808d7a1 78 getPotentialAntikingMoves(sq) {
c583ef1c 79 // The antiking moves like a king (only captured colors differ)
6808d7a1
BA
80 return this.getSlideNJumpMoves(
81 sq,
82 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
83 "oneStep"
84 );
dac39588
BA
85 }
86
68e19a44 87 isAttacked(sq, color) {
6808d7a1 88 return (
68e19a44
BA
89 super.isAttacked(sq, color) ||
90 this.isAttackedByAntiking(sq, color)
6808d7a1 91 );
dac39588
BA
92 }
93
68e19a44
BA
94 isAttackedByKing([x, y], color) {
95 // Antiking is not attacked by king:
96 if (this.getPiece(x, y) == V.ANTIKING) return false;
c583ef1c 97 return super.isAttackedByKing([x, y], color);
dac39588
BA
98 }
99
68e19a44
BA
100 isAttackedByAntiking([x, y], color) {
101 // (Anti)King is not attacked by antiking
102 if ([V.KING, V.ANTIKING].includes(this.getPiece(x, y))) return false;
6808d7a1
BA
103 return this.isAttackedBySlideNJump(
104 [x, y],
68e19a44 105 color,
6808d7a1
BA
106 V.ANTIKING,
107 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
108 "oneStep"
109 );
dac39588
BA
110 }
111
6808d7a1 112 underCheck(color) {
dac39588 113 const oppCol = V.GetOppCol(color);
6808d7a1 114 let res =
68e19a44
BA
115 this.isAttacked(this.kingPos[color], oppCol) ||
116 !this.isAttacked(this.antikingPos[color], oppCol);
dac39588
BA
117 return res;
118 }
119
6808d7a1 120 getCheckSquares(color) {
c583ef1c
BA
121 let res = [];
122 const oppCol = V.GetOppCol(color);
123 if (this.isAttacked(this.kingPos[color], oppCol))
124 res.push(JSON.parse(JSON.stringify(this.kingPos[color])));
125 if (!this.isAttacked(this.antikingPos[color], oppCol))
dac39588
BA
126 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
127 return res;
128 }
129
3a2a7b5f
BA
130 postPlay(move) {
131 super.postPlay(move);
dac39588
BA
132 const piece = move.vanish[0].p;
133 const c = move.vanish[0].c;
134 // Update antiking position
6808d7a1 135 if (piece == V.ANTIKING) {
dac39588
BA
136 this.antikingPos[c][0] = move.appear[0].x;
137 this.antikingPos[c][1] = move.appear[0].y;
138 }
139 }
140
3a2a7b5f
BA
141 postUndo(move) {
142 super.postUndo(move);
dac39588
BA
143 const c = move.vanish[0].c;
144 if (move.vanish[0].p == V.ANTIKING)
145 this.antikingPos[c] = [move.start.x, move.start.y];
146 }
147
dac39588 148 static get VALUES() {
a97bdbda
BA
149 return Object.assign(
150 { a: 1000 },
151 ChessRules.VALUES
152 );
dac39588
BA
153 }
154
7ba4a5bc 155 static GenRandInitFen(randomness) {
7ba4a5bc 156 if (randomness == 0)
3a2a7b5f 157 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
7ba4a5bc 158
6808d7a1 159 let pieces = { w: new Array(8), b: new Array(8) };
3a2a7b5f 160 let flags = "";
6808d7a1
BA
161 let antikingPos = { w: -1, b: -1 };
162 for (let c of ["w", "b"]) {
7ba4a5bc
BA
163 if (c == 'b' && randomness == 1) {
164 pieces['b'] = pieces['w'];
42a92848 165 antikingPos['b'] = antikingPos['w'];
3a2a7b5f 166 flags += flags;
7ba4a5bc
BA
167 break;
168 }
169
dac39588
BA
170 let positions = ArrayFun.range(8);
171
172 // Get random squares for bishops, but avoid corners; because,
173 // if an antiking blocks a cornered bishop, it can never be checkmated
6808d7a1 174 let randIndex = 2 * randInt(1, 4);
dac39588
BA
175 const bishop1Pos = positions[randIndex];
176 let randIndex_tmp = 2 * randInt(3) + 1;
177 const bishop2Pos = positions[randIndex_tmp];
6808d7a1
BA
178 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
179 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
dac39588
BA
180
181 randIndex = randInt(6);
182 const knight1Pos = positions[randIndex];
183 positions.splice(randIndex, 1);
184 randIndex = randInt(5);
185 const knight2Pos = positions[randIndex];
186 positions.splice(randIndex, 1);
187
188 randIndex = randInt(4);
189 const queenPos = positions[randIndex];
190 positions.splice(randIndex, 1);
191
192 const rook1Pos = positions[0];
193 const kingPos = positions[1];
194 const rook2Pos = positions[2];
195
196 // Random squares for antikings
197 antikingPos[c] = randInt(8);
198
6808d7a1
BA
199 pieces[c][rook1Pos] = "r";
200 pieces[c][knight1Pos] = "n";
201 pieces[c][bishop1Pos] = "b";
202 pieces[c][queenPos] = "q";
203 pieces[c][kingPos] = "k";
204 pieces[c][bishop2Pos] = "b";
205 pieces[c][knight2Pos] = "n";
206 pieces[c][rook2Pos] = "r";
3a2a7b5f 207 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
dac39588 208 }
6808d7a1
BA
209 const ranks23_black =
210 "pppppppp/" +
211 (antikingPos["w"] > 0 ? antikingPos["w"] : "") +
212 "A" +
213 (antikingPos["w"] < 7 ? 7 - antikingPos["w"] : "");
214 const ranks23_white =
215 (antikingPos["b"] > 0 ? antikingPos["b"] : "") +
216 "a" +
217 (antikingPos["b"] < 7 ? 7 - antikingPos["b"] : "") +
218 "/PPPPPPPP";
219 return (
220 pieces["b"].join("") +
221 "/" +
222 ranks23_black +
dac39588 223 "/8/8/" +
6808d7a1
BA
224 ranks23_white +
225 "/" +
226 pieces["w"].join("").toUpperCase() +
3a2a7b5f 227 " w 0 " + flags + " -"
6808d7a1 228 );
dac39588 229 }
b83a675a
BA
230
231 static get SEARCH_DEPTH() {
232 return 2;
233 }
6808d7a1 234};