Fix Eightpieces
[vchess.git] / client / src / variants / Antiking2.js
CommitLineData
0c3fe8a6 1import { ChessRules } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
0c3fe8a6
BA
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class Antiking2Rules extends ChessRules {
7e8a7ea1 6
6808d7a1
BA
7 static get ANTIKING() {
8 return "a";
9 }
dac39588 10
6808d7a1 11 static get PIECES() {
dac39588
BA
12 return ChessRules.PIECES.concat([V.ANTIKING]);
13 }
14
241bf8f2
BA
15 getPpath(b) {
16 return b[1] == "a" ? "Antiking/" + b : b;
17 }
18
6f2f9437
BA
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
6808d7a1 32 setOtherVariables(fen) {
dac39588 33 super.setOtherVariables(fen);
6808d7a1 34 this.antikingPos = { w: [-1, -1], b: [-1, -1] };
dac39588 35 const rows = V.ParseFen(fen).position.split("/");
6808d7a1 36 for (let i = 0; i < rows.length; i++) {
dac39588 37 let k = 0;
6808d7a1
BA
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];
dac39588 42 break;
6808d7a1
BA
43 case "A":
44 this.antikingPos["w"] = [i, k];
dac39588 45 break;
6808d7a1 46 default: {
e50a8025 47 const num = parseInt(rows[i].charAt(j), 10);
6808d7a1
BA
48 if (!isNaN(num)) k += num - 1;
49 }
dac39588
BA
50 }
51 k++;
52 }
53 }
54 }
55
6808d7a1
BA
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" &&
c583ef1c
BA
63 (
64 (piece1 != "a" && color1 != color2) ||
65 (piece1 == "a" && color1 == color2)
66 )
6808d7a1 67 );
dac39588
BA
68 }
69
6808d7a1
BA
70 getPotentialMovesFrom([x, y]) {
71 switch (this.getPiece(x, y)) {
dac39588 72 case V.ANTIKING:
6808d7a1 73 return this.getPotentialAntikingMoves([x, y]);
dac39588 74 default:
6808d7a1 75 return super.getPotentialMovesFrom([x, y]);
dac39588
BA
76 }
77 }
78
6808d7a1 79 getPotentialAntikingMoves(sq) {
c583ef1c 80 // The antiking moves like a king (only captured colors differ)
6808d7a1 81 return this.getSlideNJumpMoves(
4313762d 82 sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1);
dac39588
BA
83 }
84
68e19a44 85 isAttacked(sq, color) {
6808d7a1 86 return (
68e19a44
BA
87 super.isAttacked(sq, color) ||
88 this.isAttackedByAntiking(sq, color)
6808d7a1 89 );
dac39588
BA
90 }
91
68e19a44
BA
92 isAttackedByKing([x, y], color) {
93 // Antiking is not attacked by king:
94 if (this.getPiece(x, y) == V.ANTIKING) return false;
c583ef1c 95 return super.isAttackedByKing([x, y], color);
dac39588
BA
96 }
97
68e19a44
BA
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;
6808d7a1 101 return this.isAttackedBySlideNJump(
4313762d 102 [x, y], color, V.ANTIKING, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1);
dac39588
BA
103 }
104
6808d7a1 105 underCheck(color) {
dac39588 106 const oppCol = V.GetOppCol(color);
6808d7a1 107 let res =
68e19a44
BA
108 this.isAttacked(this.kingPos[color], oppCol) ||
109 !this.isAttacked(this.antikingPos[color], oppCol);
dac39588
BA
110 return res;
111 }
112
af34341d
BA
113 getCheckSquares() {
114 const color = this.turn;
c583ef1c
BA
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))
dac39588
BA
120 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
121 return res;
122 }
123
3a2a7b5f
BA
124 postPlay(move) {
125 super.postPlay(move);
dac39588
BA
126 const piece = move.vanish[0].p;
127 const c = move.vanish[0].c;
128 // Update antiking position
6808d7a1 129 if (piece == V.ANTIKING) {
dac39588
BA
130 this.antikingPos[c][0] = move.appear[0].x;
131 this.antikingPos[c][1] = move.appear[0].y;
132 }
133 }
134
3a2a7b5f
BA
135 postUndo(move) {
136 super.postUndo(move);
dac39588
BA
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
dac39588 142 static get VALUES() {
a97bdbda
BA
143 return Object.assign(
144 { a: 1000 },
145 ChessRules.VALUES
146 );
dac39588
BA
147 }
148
4313762d
BA
149 static GenRandInitFen(options) {
150 if (options.randomness == 0)
3a2a7b5f 151 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
7ba4a5bc 152
6808d7a1 153 let pieces = { w: new Array(8), b: new Array(8) };
3a2a7b5f 154 let flags = "";
6808d7a1
BA
155 let antikingPos = { w: -1, b: -1 };
156 for (let c of ["w", "b"]) {
4313762d 157 if (c == 'b' && options.randomness == 1) {
7ba4a5bc 158 pieces['b'] = pieces['w'];
42a92848 159 antikingPos['b'] = antikingPos['w'];
3a2a7b5f 160 flags += flags;
7ba4a5bc
BA
161 break;
162 }
163
dac39588
BA
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
6808d7a1 168 let randIndex = 2 * randInt(1, 4);
dac39588
BA
169 const bishop1Pos = positions[randIndex];
170 let randIndex_tmp = 2 * randInt(3) + 1;
171 const bishop2Pos = positions[randIndex_tmp];
6808d7a1
BA
172 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
173 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
dac39588
BA
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
6808d7a1
BA
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";
3a2a7b5f 201 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
dac39588 202 }
6808d7a1
BA
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 +
dac39588 217 "/8/8/" +
6808d7a1
BA
218 ranks23_white +
219 "/" +
220 pieces["w"].join("").toUpperCase() +
3a2a7b5f 221 " w 0 " + flags + " -"
6808d7a1 222 );
dac39588 223 }
b83a675a
BA
224
225 static get SEARCH_DEPTH() {
226 return 2;
227 }
7e8a7ea1 228
6808d7a1 229};