Move 'anonymousCount' which was computed to a method in Hall. Seems more accurate
[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 const VariantRules = 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 setOtherVariables(fen) {
19 super.setOtherVariables(fen);
20 this.antikingPos = { w: [-1, -1], b: [-1, -1] };
21 const rows = V.ParseFen(fen).position.split("/");
22 for (let i = 0; i < rows.length; i++) {
23 let k = 0;
24 for (let j = 0; j < rows[i].length; j++) {
25 switch (rows[i].charAt(j)) {
26 case "a":
27 this.antikingPos["b"] = [i, k];
28 break;
29 case "A":
30 this.antikingPos["w"] = [i, k];
31 break;
32 default: {
33 const num = parseInt(rows[i].charAt(j));
34 if (!isNaN(num)) k += num - 1;
35 }
36 }
37 k++;
38 }
39 }
40 }
41
42 canTake([x1, y1], [x2, y2]) {
43 const piece1 = this.getPiece(x1, y1);
44 const piece2 = this.getPiece(x2, y2);
45 const color1 = this.getColor(x1, y1);
46 const color2 = this.getColor(x2, y2);
47 return (
48 piece2 != "a" &&
49 (
50 (piece1 != "a" && color1 != color2) ||
51 (piece1 == "a" && color1 == color2)
52 )
53 );
54 }
55
56 getPotentialMovesFrom([x, y]) {
57 switch (this.getPiece(x, y)) {
58 case V.ANTIKING:
59 return this.getPotentialAntikingMoves([x, y]);
60 default:
61 return super.getPotentialMovesFrom([x, y]);
62 }
63 }
64
65 getPotentialAntikingMoves(sq) {
66 // The antiking moves like a king (only captured colors differ)
67 return this.getSlideNJumpMoves(
68 sq,
69 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
70 "oneStep"
71 );
72 }
73
74 isAttacked(sq, color) {
75 return (
76 super.isAttacked(sq, color) ||
77 this.isAttackedByAntiking(sq, color)
78 );
79 }
80
81 isAttackedByKing([x, y], color) {
82 // Antiking is not attacked by king:
83 if (this.getPiece(x, y) == V.ANTIKING) return false;
84 return super.isAttackedByKing([x, y], color);
85 }
86
87 isAttackedByAntiking([x, y], color) {
88 // (Anti)King is not attacked by antiking
89 if ([V.KING, V.ANTIKING].includes(this.getPiece(x, y))) return false;
90 return this.isAttackedBySlideNJump(
91 [x, y],
92 color,
93 V.ANTIKING,
94 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
95 "oneStep"
96 );
97 }
98
99 underCheck(color) {
100 const oppCol = V.GetOppCol(color);
101 let res =
102 this.isAttacked(this.kingPos[color], oppCol) ||
103 !this.isAttacked(this.antikingPos[color], oppCol);
104 return res;
105 }
106
107 getCheckSquares(color) {
108 let res = [];
109 const oppCol = V.GetOppCol(color);
110 if (this.isAttacked(this.kingPos[color], oppCol))
111 res.push(JSON.parse(JSON.stringify(this.kingPos[color])));
112 if (!this.isAttacked(this.antikingPos[color], oppCol))
113 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
114 return res;
115 }
116
117 postPlay(move) {
118 super.postPlay(move);
119 const piece = move.vanish[0].p;
120 const c = move.vanish[0].c;
121 // Update antiking position
122 if (piece == V.ANTIKING) {
123 this.antikingPos[c][0] = move.appear[0].x;
124 this.antikingPos[c][1] = move.appear[0].y;
125 }
126 }
127
128 postUndo(move) {
129 super.postUndo(move);
130 const c = move.vanish[0].c;
131 if (move.vanish[0].p == V.ANTIKING)
132 this.antikingPos[c] = [move.start.x, move.start.y];
133 }
134
135 getCurrentScore() {
136 if (this.atLeastOneMove())
137 return "*";
138
139 const color = this.turn;
140 const oppCol = V.GetOppCol(color);
141 if (
142 !this.isAttacked(this.kingPos[color], oppCol) &&
143 this.isAttacked(this.antikingPos[color], oppCol)
144 ) {
145 return "1/2";
146 }
147 return color == "w" ? "0-1" : "1-0";
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 };