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 | ||
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" && | |
c583ef1c BA |
49 | ( |
50 | (piece1 != "a" && color1 != color2) || | |
51 | (piece1 == "a" && color1 == color2) | |
52 | ) | |
6808d7a1 | 53 | ); |
dac39588 BA |
54 | } |
55 | ||
6808d7a1 BA |
56 | getPotentialMovesFrom([x, y]) { |
57 | switch (this.getPiece(x, y)) { | |
dac39588 | 58 | case V.ANTIKING: |
6808d7a1 | 59 | return this.getPotentialAntikingMoves([x, y]); |
dac39588 | 60 | default: |
6808d7a1 | 61 | return super.getPotentialMovesFrom([x, y]); |
dac39588 BA |
62 | } |
63 | } | |
64 | ||
6808d7a1 | 65 | getPotentialAntikingMoves(sq) { |
c583ef1c | 66 | // The antiking moves like a king (only captured colors differ) |
6808d7a1 BA |
67 | return this.getSlideNJumpMoves( |
68 | sq, | |
69 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
70 | "oneStep" | |
71 | ); | |
dac39588 BA |
72 | } |
73 | ||
68e19a44 | 74 | isAttacked(sq, color) { |
6808d7a1 | 75 | return ( |
68e19a44 BA |
76 | super.isAttacked(sq, color) || |
77 | this.isAttackedByAntiking(sq, color) | |
6808d7a1 | 78 | ); |
dac39588 BA |
79 | } |
80 | ||
68e19a44 BA |
81 | isAttackedByKing([x, y], color) { |
82 | // Antiking is not attacked by king: | |
83 | if (this.getPiece(x, y) == V.ANTIKING) return false; | |
c583ef1c | 84 | return super.isAttackedByKing([x, y], color); |
dac39588 BA |
85 | } |
86 | ||
68e19a44 BA |
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; | |
6808d7a1 BA |
90 | return this.isAttackedBySlideNJump( |
91 | [x, y], | |
68e19a44 | 92 | color, |
6808d7a1 BA |
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 | 101 | let res = |
68e19a44 BA |
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) { |
c583ef1c BA |
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)) | |
dac39588 BA |
113 | res.push(JSON.parse(JSON.stringify(this.antikingPos[color]))); |
114 | return res; | |
115 | } | |
116 | ||
3a2a7b5f BA |
117 | postPlay(move) { |
118 | super.postPlay(move); | |
dac39588 BA |
119 | const piece = move.vanish[0].p; |
120 | const c = move.vanish[0].c; | |
121 | // Update antiking position | |
6808d7a1 | 122 | if (piece == V.ANTIKING) { |
dac39588 BA |
123 | this.antikingPos[c][0] = move.appear[0].x; |
124 | this.antikingPos[c][1] = move.appear[0].y; | |
125 | } | |
126 | } | |
127 | ||
3a2a7b5f BA |
128 | postUndo(move) { |
129 | super.postUndo(move); | |
dac39588 BA |
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 | ||
dac39588 | 135 | static get VALUES() { |
a97bdbda BA |
136 | return Object.assign( |
137 | { a: 1000 }, | |
138 | ChessRules.VALUES | |
139 | ); | |
dac39588 BA |
140 | } |
141 | ||
7ba4a5bc | 142 | static GenRandInitFen(randomness) { |
7ba4a5bc | 143 | if (randomness == 0) |
3a2a7b5f | 144 | return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -"; |
7ba4a5bc | 145 | |
6808d7a1 | 146 | let pieces = { w: new Array(8), b: new Array(8) }; |
3a2a7b5f | 147 | let flags = ""; |
6808d7a1 BA |
148 | let antikingPos = { w: -1, b: -1 }; |
149 | for (let c of ["w", "b"]) { | |
7ba4a5bc BA |
150 | if (c == 'b' && randomness == 1) { |
151 | pieces['b'] = pieces['w']; | |
42a92848 | 152 | antikingPos['b'] = antikingPos['w']; |
3a2a7b5f | 153 | flags += flags; |
7ba4a5bc BA |
154 | break; |
155 | } | |
156 | ||
dac39588 BA |
157 | let positions = ArrayFun.range(8); |
158 | ||
159 | // Get random squares for bishops, but avoid corners; because, | |
160 | // if an antiking blocks a cornered bishop, it can never be checkmated | |
6808d7a1 | 161 | let randIndex = 2 * randInt(1, 4); |
dac39588 BA |
162 | const bishop1Pos = positions[randIndex]; |
163 | let randIndex_tmp = 2 * randInt(3) + 1; | |
164 | const bishop2Pos = positions[randIndex_tmp]; | |
6808d7a1 BA |
165 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
166 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); | |
dac39588 BA |
167 | |
168 | randIndex = randInt(6); | |
169 | const knight1Pos = positions[randIndex]; | |
170 | positions.splice(randIndex, 1); | |
171 | randIndex = randInt(5); | |
172 | const knight2Pos = positions[randIndex]; | |
173 | positions.splice(randIndex, 1); | |
174 | ||
175 | randIndex = randInt(4); | |
176 | const queenPos = positions[randIndex]; | |
177 | positions.splice(randIndex, 1); | |
178 | ||
179 | const rook1Pos = positions[0]; | |
180 | const kingPos = positions[1]; | |
181 | const rook2Pos = positions[2]; | |
182 | ||
183 | // Random squares for antikings | |
184 | antikingPos[c] = randInt(8); | |
185 | ||
6808d7a1 BA |
186 | pieces[c][rook1Pos] = "r"; |
187 | pieces[c][knight1Pos] = "n"; | |
188 | pieces[c][bishop1Pos] = "b"; | |
189 | pieces[c][queenPos] = "q"; | |
190 | pieces[c][kingPos] = "k"; | |
191 | pieces[c][bishop2Pos] = "b"; | |
192 | pieces[c][knight2Pos] = "n"; | |
193 | pieces[c][rook2Pos] = "r"; | |
3a2a7b5f | 194 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
dac39588 | 195 | } |
6808d7a1 BA |
196 | const ranks23_black = |
197 | "pppppppp/" + | |
198 | (antikingPos["w"] > 0 ? antikingPos["w"] : "") + | |
199 | "A" + | |
200 | (antikingPos["w"] < 7 ? 7 - antikingPos["w"] : ""); | |
201 | const ranks23_white = | |
202 | (antikingPos["b"] > 0 ? antikingPos["b"] : "") + | |
203 | "a" + | |
204 | (antikingPos["b"] < 7 ? 7 - antikingPos["b"] : "") + | |
205 | "/PPPPPPPP"; | |
206 | return ( | |
207 | pieces["b"].join("") + | |
208 | "/" + | |
209 | ranks23_black + | |
dac39588 | 210 | "/8/8/" + |
6808d7a1 BA |
211 | ranks23_white + |
212 | "/" + | |
213 | pieces["w"].join("").toUpperCase() + | |
3a2a7b5f | 214 | " w 0 " + flags + " -" |
6808d7a1 | 215 | ); |
dac39588 | 216 | } |
b83a675a BA |
217 | |
218 | static get SEARCH_DEPTH() { | |
219 | return 2; | |
220 | } | |
6808d7a1 | 221 | }; |