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