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