Started code review + some fixes (unfinished)
[vchess.git] / client / src / variants / Antiking.js
CommitLineData
0c3fe8a6 1import { ChessRules } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
0c3fe8a6
BA
3import { randInt } from "@/utils/alea";
4
6808d7a1
BA
5export const VariantRules = class AntikingRules extends ChessRules {
6 static getPpath(b) {
7 return b[1] == "a" ? "Antiking/" + b : b;
dac39588
BA
8 }
9
6808d7a1
BA
10 static get ANTIKING() {
11 return "a";
12 }
dac39588 13
6808d7a1 14 static get PIECES() {
dac39588
BA
15 return ChessRules.PIECES.concat([V.ANTIKING]);
16 }
17
6808d7a1 18 setOtherVariables(fen) {
dac39588 19 super.setOtherVariables(fen);
6808d7a1 20 this.antikingPos = { w: [-1, -1], b: [-1, -1] };
dac39588 21 const rows = V.ParseFen(fen).position.split("/");
6808d7a1 22 for (let i = 0; i < rows.length; i++) {
dac39588 23 let k = 0;
6808d7a1
BA
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];
dac39588 28 break;
6808d7a1
BA
29 case "A":
30 this.antikingPos["w"] = [i, k];
dac39588 31 break;
6808d7a1 32 default: {
dac39588 33 const num = parseInt(rows[i].charAt(j));
6808d7a1
BA
34 if (!isNaN(num)) k += num - 1;
35 }
dac39588
BA
36 }
37 k++;
38 }
39 }
40 }
41
6808d7a1
BA
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 ((piece1 != "a" && color1 != color2) ||
50 (piece1 == "a" && color1 == color2))
51 );
dac39588
BA
52 }
53
6808d7a1
BA
54 getPotentialMovesFrom([x, y]) {
55 switch (this.getPiece(x, y)) {
dac39588 56 case V.ANTIKING:
6808d7a1 57 return this.getPotentialAntikingMoves([x, y]);
dac39588 58 default:
6808d7a1 59 return super.getPotentialMovesFrom([x, y]);
dac39588
BA
60 }
61 }
62
6808d7a1
BA
63 getPotentialAntikingMoves(sq) {
64 return this.getSlideNJumpMoves(
65 sq,
66 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
67 "oneStep"
68 );
dac39588
BA
69 }
70
6808d7a1
BA
71 isAttacked(sq, colors) {
72 return (
73 super.isAttacked(sq, colors) || this.isAttackedByAntiking(sq, colors)
74 );
dac39588
BA
75 }
76
6808d7a1
BA
77 isAttackedByKing([x, y], colors) {
78 if (this.getPiece(x, y) == V.ANTIKING) return false; //antiking is not attacked by king
79 return this.isAttackedBySlideNJump(
80 [x, y],
81 colors,
82 V.KING,
83 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
84 "oneStep"
85 );
dac39588
BA
86 }
87
6808d7a1
BA
88 isAttackedByAntiking([x, y], colors) {
89 if ([V.KING, V.ANTIKING].includes(this.getPiece(x, y))) return false; //(anti)king is not attacked by antiking
90 return this.isAttackedBySlideNJump(
91 [x, y],
92 colors,
93 V.ANTIKING,
94 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
95 "oneStep"
96 );
dac39588
BA
97 }
98
6808d7a1 99 underCheck(color) {
dac39588 100 const oppCol = V.GetOppCol(color);
6808d7a1
BA
101 let res =
102 this.isAttacked(this.kingPos[color], [oppCol]) ||
103 !this.isAttacked(this.antikingPos[color], [oppCol]);
dac39588
BA
104 return res;
105 }
106
6808d7a1 107 getCheckSquares(color) {
dac39588
BA
108 let res = super.getCheckSquares(color);
109 if (!this.isAttacked(this.antikingPos[color], [V.GetOppCol(color)]))
110 res.push(JSON.parse(JSON.stringify(this.antikingPos[color])));
111 return res;
112 }
113
6808d7a1 114 updateVariables(move) {
dac39588
BA
115 super.updateVariables(move);
116 const piece = move.vanish[0].p;
117 const c = move.vanish[0].c;
118 // Update antiking position
6808d7a1 119 if (piece == V.ANTIKING) {
dac39588
BA
120 this.antikingPos[c][0] = move.appear[0].x;
121 this.antikingPos[c][1] = move.appear[0].y;
122 }
123 }
124
6808d7a1 125 unupdateVariables(move) {
dac39588
BA
126 super.unupdateVariables(move);
127 const c = move.vanish[0].c;
128 if (move.vanish[0].p == V.ANTIKING)
129 this.antikingPos[c] = [move.start.x, move.start.y];
130 }
131
6808d7a1
BA
132 getCurrentScore() {
133 if (this.atLeastOneMove())
134 // game not over
0c3fe8a6
BA
135 return "*";
136
137 const color = this.turn;
dac39588 138 const oppCol = V.GetOppCol(color);
6808d7a1
BA
139 if (
140 !this.isAttacked(this.kingPos[color], [oppCol]) &&
141 this.isAttacked(this.antikingPos[color], [oppCol])
142 ) {
dac39588
BA
143 return "1/2";
144 }
145 return color == "w" ? "0-1" : "1-0";
146 }
147
148 static get VALUES() {
6808d7a1 149 return Object.assign(ChessRules.VALUES, { a: 1000 });
dac39588
BA
150 }
151
6808d7a1
BA
152 static GenRandInitFen() {
153 let pieces = { w: new Array(8), b: new Array(8) };
154 let antikingPos = { w: -1, b: -1 };
155 for (let c of ["w", "b"]) {
dac39588
BA
156 let positions = ArrayFun.range(8);
157
158 // Get random squares for bishops, but avoid corners; because,
159 // if an antiking blocks a cornered bishop, it can never be checkmated
6808d7a1 160 let randIndex = 2 * randInt(1, 4);
dac39588
BA
161 const bishop1Pos = positions[randIndex];
162 let randIndex_tmp = 2 * randInt(3) + 1;
163 const bishop2Pos = positions[randIndex_tmp];
6808d7a1
BA
164 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
165 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
dac39588
BA
166
167 randIndex = randInt(6);
168 const knight1Pos = positions[randIndex];
169 positions.splice(randIndex, 1);
170 randIndex = randInt(5);
171 const knight2Pos = positions[randIndex];
172 positions.splice(randIndex, 1);
173
174 randIndex = randInt(4);
175 const queenPos = positions[randIndex];
176 positions.splice(randIndex, 1);
177
178 const rook1Pos = positions[0];
179 const kingPos = positions[1];
180 const rook2Pos = positions[2];
181
182 // Random squares for antikings
183 antikingPos[c] = randInt(8);
184
6808d7a1
BA
185 pieces[c][rook1Pos] = "r";
186 pieces[c][knight1Pos] = "n";
187 pieces[c][bishop1Pos] = "b";
188 pieces[c][queenPos] = "q";
189 pieces[c][kingPos] = "k";
190 pieces[c][bishop2Pos] = "b";
191 pieces[c][knight2Pos] = "n";
192 pieces[c][rook2Pos] = "r";
dac39588 193 }
6808d7a1
BA
194 const ranks23_black =
195 "pppppppp/" +
196 (antikingPos["w"] > 0 ? antikingPos["w"] : "") +
197 "A" +
198 (antikingPos["w"] < 7 ? 7 - antikingPos["w"] : "");
199 const ranks23_white =
200 (antikingPos["b"] > 0 ? antikingPos["b"] : "") +
201 "a" +
202 (antikingPos["b"] < 7 ? 7 - antikingPos["b"] : "") +
203 "/PPPPPPPP";
204 return (
205 pieces["b"].join("") +
206 "/" +
207 ranks23_black +
dac39588 208 "/8/8/" +
6808d7a1
BA
209 ranks23_white +
210 "/" +
211 pieces["w"].join("").toUpperCase() +
212 " w 0 1111 -"
213 );
dac39588 214 }
6808d7a1 215};