Add Checkered1 + fix last move highlights
[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() {
121 const color = this.turn;
122 let res = [];
123 const oppCol = V.GetOppCol(color);
124 if (this.isAttacked(this.kingPos[color], oppCol))
125 res.push(JSON.parse(JSON.stringify(this.kingPos[color])));
126 if (!this.isAttacked(this.antikingPos[color], oppCol))
127 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
128 return res;
129 }
130
131 postPlay(move) {
132 super.postPlay(move);
133 const piece = move.vanish[0].p;
134 const c = move.vanish[0].c;
135 // Update antiking position
136 if (piece == V.ANTIKING) {
137 this.antikingPos[c][0] = move.appear[0].x;
138 this.antikingPos[c][1] = move.appear[0].y;
139 }
140 }
141
142 postUndo(move) {
143 super.postUndo(move);
144 const c = move.vanish[0].c;
145 if (move.vanish[0].p == V.ANTIKING)
146 this.antikingPos[c] = [move.start.x, move.start.y];
147 }
148
149 static get VALUES() {
150 return Object.assign(
151 { a: 1000 },
152 ChessRules.VALUES
153 );
154 }
155
156 static GenRandInitFen(randomness) {
157 if (randomness == 0)
158 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
159
160 let pieces = { w: new Array(8), b: new Array(8) };
161 let flags = "";
162 let antikingPos = { w: -1, b: -1 };
163 for (let c of ["w", "b"]) {
164 if (c == 'b' && randomness == 1) {
165 pieces['b'] = pieces['w'];
166 antikingPos['b'] = antikingPos['w'];
167 flags += flags;
168 break;
169 }
170
171 let positions = ArrayFun.range(8);
172
173 // Get random squares for bishops, but avoid corners; because,
174 // if an antiking blocks a cornered bishop, it can never be checkmated
175 let randIndex = 2 * randInt(1, 4);
176 const bishop1Pos = positions[randIndex];
177 let randIndex_tmp = 2 * randInt(3) + 1;
178 const bishop2Pos = positions[randIndex_tmp];
179 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
180 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
181
182 randIndex = randInt(6);
183 const knight1Pos = positions[randIndex];
184 positions.splice(randIndex, 1);
185 randIndex = randInt(5);
186 const knight2Pos = positions[randIndex];
187 positions.splice(randIndex, 1);
188
189 randIndex = randInt(4);
190 const queenPos = positions[randIndex];
191 positions.splice(randIndex, 1);
192
193 const rook1Pos = positions[0];
194 const kingPos = positions[1];
195 const rook2Pos = positions[2];
196
197 // Random squares for antikings
198 antikingPos[c] = randInt(8);
199
200 pieces[c][rook1Pos] = "r";
201 pieces[c][knight1Pos] = "n";
202 pieces[c][bishop1Pos] = "b";
203 pieces[c][queenPos] = "q";
204 pieces[c][kingPos] = "k";
205 pieces[c][bishop2Pos] = "b";
206 pieces[c][knight2Pos] = "n";
207 pieces[c][rook2Pos] = "r";
208 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
209 }
210 const ranks23_black =
211 "pppppppp/" +
212 (antikingPos["w"] > 0 ? antikingPos["w"] : "") +
213 "A" +
214 (antikingPos["w"] < 7 ? 7 - antikingPos["w"] : "");
215 const ranks23_white =
216 (antikingPos["b"] > 0 ? antikingPos["b"] : "") +
217 "a" +
218 (antikingPos["b"] < 7 ? 7 - antikingPos["b"] : "") +
219 "/PPPPPPPP";
220 return (
221 pieces["b"].join("") +
222 "/" +
223 ranks23_black +
224 "/8/8/" +
225 ranks23_white +
226 "/" +
227 pieces["w"].join("").toUpperCase() +
228 " w 0 " + flags + " -"
229 );
230 }
231
232 static get SEARCH_DEPTH() {
233 return 2;
234 }
235 };