Some other fixes
[vchess.git] / client / src / variants / Antiking2.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt } from "@/utils/alea";
4
5 export class Antiking2Rules extends ChessRules {
6 static get ANTIKING() {
7 return "a";
8 }
9
10 static get PIECES() {
11 return ChessRules.PIECES.concat([V.ANTIKING]);
12 }
13
14 getPpath(b) {
15 return b[1] == "a" ? "Antiking/" + b : b;
16 }
17
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
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 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" &&
62 (
63 (piece1 != "a" && color1 != color2) ||
64 (piece1 == "a" && color1 == color2)
65 )
66 );
67 }
68
69 getPotentialMovesFrom([x, y]) {
70 switch (this.getPiece(x, y)) {
71 case V.ANTIKING:
72 return this.getPotentialAntikingMoves([x, y]);
73 default:
74 return super.getPotentialMovesFrom([x, y]);
75 }
76 }
77
78 getPotentialAntikingMoves(sq) {
79 // The antiking moves like a king (only captured colors differ)
80 return this.getSlideNJumpMoves(
81 sq,
82 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
83 "oneStep"
84 );
85 }
86
87 isAttacked(sq, color) {
88 return (
89 super.isAttacked(sq, color) ||
90 this.isAttackedByAntiking(sq, color)
91 );
92 }
93
94 isAttackedByKing([x, y], color) {
95 // Antiking is not attacked by king:
96 if (this.getPiece(x, y) == V.ANTIKING) return false;
97 return super.isAttackedByKing([x, y], color);
98 }
99
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;
103 return this.isAttackedBySlideNJump(
104 [x, y],
105 color,
106 V.ANTIKING,
107 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
108 "oneStep"
109 );
110 }
111
112 underCheck(color) {
113 const oppCol = V.GetOppCol(color);
114 let res =
115 this.isAttacked(this.kingPos[color], oppCol) ||
116 !this.isAttacked(this.antikingPos[color], oppCol);
117 return res;
118 }
119
120 getCheckSquares(color) {
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))
126 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
127 return res;
128 }
129
130 postPlay(move) {
131 super.postPlay(move);
132 const piece = move.vanish[0].p;
133 const c = move.vanish[0].c;
134 // Update antiking position
135 if (piece == V.ANTIKING) {
136 this.antikingPos[c][0] = move.appear[0].x;
137 this.antikingPos[c][1] = move.appear[0].y;
138 }
139 }
140
141 postUndo(move) {
142 super.postUndo(move);
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
148 static get VALUES() {
149 return Object.assign(
150 { a: 1000 },
151 ChessRules.VALUES
152 );
153 }
154
155 static GenRandInitFen(randomness) {
156 if (randomness == 0)
157 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
158
159 let pieces = { w: new Array(8), b: new Array(8) };
160 let flags = "";
161 let antikingPos = { w: -1, b: -1 };
162 for (let c of ["w", "b"]) {
163 if (c == 'b' && randomness == 1) {
164 pieces['b'] = pieces['w'];
165 antikingPos['b'] = antikingPos['w'];
166 flags += flags;
167 break;
168 }
169
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
174 let randIndex = 2 * randInt(1, 4);
175 const bishop1Pos = positions[randIndex];
176 let randIndex_tmp = 2 * randInt(3) + 1;
177 const bishop2Pos = positions[randIndex_tmp];
178 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
179 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
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
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";
207 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
208 }
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 +
223 "/8/8/" +
224 ranks23_white +
225 "/" +
226 pieces["w"].join("").toUpperCase() +
227 " w 0 " + flags + " -"
228 );
229 }
230
231 static get SEARCH_DEPTH() {
232 return 2;
233 }
234 };