Small 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
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, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1);
83 }
84
85 isAttacked(sq, color) {
86 return (
87 super.isAttacked(sq, color) ||
88 this.isAttackedByAntiking(sq, color)
89 );
90 }
91
92 isAttackedByKing([x, y], color) {
93 // Antiking is not attacked by king:
94 if (this.getPiece(x, y) == V.ANTIKING) return false;
95 return super.isAttackedByKing([x, y], color);
96 }
97
98 isAttackedByAntiking([x, y], color) {
99 // (Anti)King is not attacked by antiking
100 if ([V.KING, V.ANTIKING].includes(this.getPiece(x, y))) return false;
101 return this.isAttackedBySlideNJump(
102 [x, y], color, V.ANTIKING, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1);
103 }
104
105 underCheck(color) {
106 const oppCol = V.GetOppCol(color);
107 let res =
108 this.isAttacked(this.kingPos[color], oppCol) ||
109 !this.isAttacked(this.antikingPos[color], oppCol);
110 return res;
111 }
112
113 getCheckSquares() {
114 const color = this.turn;
115 let res = [];
116 const oppCol = V.GetOppCol(color);
117 if (this.isAttacked(this.kingPos[color], oppCol))
118 res.push(JSON.parse(JSON.stringify(this.kingPos[color])));
119 if (!this.isAttacked(this.antikingPos[color], oppCol))
120 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
121 return res;
122 }
123
124 postPlay(move) {
125 super.postPlay(move);
126 const piece = move.vanish[0].p;
127 const c = move.vanish[0].c;
128 // Update antiking position
129 if (piece == V.ANTIKING) {
130 this.antikingPos[c][0] = move.appear[0].x;
131 this.antikingPos[c][1] = move.appear[0].y;
132 }
133 }
134
135 postUndo(move) {
136 super.postUndo(move);
137 const c = move.vanish[0].c;
138 if (move.vanish[0].p == V.ANTIKING)
139 this.antikingPos[c] = [move.start.x, move.start.y];
140 }
141
142 static get VALUES() {
143 return Object.assign(
144 { a: 1000 },
145 ChessRules.VALUES
146 );
147 }
148
149 static GenRandInitFen(options) {
150 if (options.randomness == 0)
151 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
152
153 let pieces = { w: new Array(8), b: new Array(8) };
154 let flags = "";
155 let antikingPos = { w: -1, b: -1 };
156 for (let c of ["w", "b"]) {
157 if (c == 'b' && options.randomness == 1) {
158 pieces['b'] = pieces['w'];
159 antikingPos['b'] = antikingPos['w'];
160 flags += flags;
161 break;
162 }
163
164 let positions = ArrayFun.range(8);
165
166 // Get random squares for bishops, but avoid corners; because,
167 // if an antiking blocks a cornered bishop, it can never be checkmated
168 let randIndex = 2 * randInt(1, 4);
169 const bishop1Pos = positions[randIndex];
170 let randIndex_tmp = 2 * randInt(3) + 1;
171 const bishop2Pos = positions[randIndex_tmp];
172 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
173 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
174
175 randIndex = randInt(6);
176 const knight1Pos = positions[randIndex];
177 positions.splice(randIndex, 1);
178 randIndex = randInt(5);
179 const knight2Pos = positions[randIndex];
180 positions.splice(randIndex, 1);
181
182 randIndex = randInt(4);
183 const queenPos = positions[randIndex];
184 positions.splice(randIndex, 1);
185
186 const rook1Pos = positions[0];
187 const kingPos = positions[1];
188 const rook2Pos = positions[2];
189
190 // Random squares for antikings
191 antikingPos[c] = randInt(8);
192
193 pieces[c][rook1Pos] = "r";
194 pieces[c][knight1Pos] = "n";
195 pieces[c][bishop1Pos] = "b";
196 pieces[c][queenPos] = "q";
197 pieces[c][kingPos] = "k";
198 pieces[c][bishop2Pos] = "b";
199 pieces[c][knight2Pos] = "n";
200 pieces[c][rook2Pos] = "r";
201 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
202 }
203 const ranks23_black =
204 "pppppppp/" +
205 (antikingPos["w"] > 0 ? antikingPos["w"] : "") +
206 "A" +
207 (antikingPos["w"] < 7 ? 7 - antikingPos["w"] : "");
208 const ranks23_white =
209 (antikingPos["b"] > 0 ? antikingPos["b"] : "") +
210 "a" +
211 (antikingPos["b"] < 7 ? 7 - antikingPos["b"] : "") +
212 "/PPPPPPPP";
213 return (
214 pieces["b"].join("") +
215 "/" +
216 ranks23_black +
217 "/8/8/" +
218 ranks23_white +
219 "/" +
220 pieces["w"].join("").toUpperCase() +
221 " w 0 " + flags + " -"
222 );
223 }
224
225 static get SEARCH_DEPTH() {
226 return 2;
227 }
228
229 };