Commit | Line | Data |
---|---|---|
0c3fe8a6 | 1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
6808d7a1 | 2 | import { ArrayFun } from "@/utils/array"; |
0c3fe8a6 | 3 | |
32f6285e | 4 | export class CrazyhouseRules extends ChessRules { |
7e8a7ea1 | 5 | |
6808d7a1 BA |
6 | static IsGoodFen(fen) { |
7 | if (!ChessRules.IsGoodFen(fen)) return false; | |
dac39588 BA |
8 | const fenParsed = V.ParseFen(fen); |
9 | // 5) Check reserves | |
10 | if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{10,10}$/)) | |
11 | return false; | |
12 | // 6) Check promoted array | |
6808d7a1 BA |
13 | if (!fenParsed.promoted) return false; |
14 | if (fenParsed.promoted == "-") return true; //no promoted piece on board | |
dac39588 | 15 | const squares = fenParsed.promoted.split(","); |
6808d7a1 | 16 | for (let square of squares) { |
dac39588 BA |
17 | const c = V.SquareToCoords(square); |
18 | if (c.y < 0 || c.y > V.size.y || isNaN(c.x) || c.x < 0 || c.x > V.size.x) | |
19 | return false; | |
20 | } | |
21 | return true; | |
22 | } | |
2d7194bd | 23 | |
6808d7a1 | 24 | static ParseFen(fen) { |
dac39588 | 25 | const fenParts = fen.split(" "); |
6f2f9437 BA |
26 | return Object.assign( |
27 | ChessRules.ParseFen(fen), | |
28 | { | |
29 | reserve: fenParts[5], | |
30 | promoted: fenParts[6] | |
31 | } | |
32 | ); | |
dac39588 | 33 | } |
fb6ceeff | 34 | |
7ba4a5bc BA |
35 | static GenRandInitFen(randomness) { |
36 | return ChessRules.GenRandInitFen(randomness) + " 0000000000 -"; | |
dac39588 | 37 | } |
2d7194bd | 38 | |
6808d7a1 BA |
39 | getFen() { |
40 | return ( | |
90e814b6 BA |
41 | super.getFen() + " " + |
42 | this.getReserveFen() + " " + | |
43 | this.getPromotedFen() | |
6808d7a1 | 44 | ); |
dac39588 | 45 | } |
2d7194bd | 46 | |
f9c36b2d BA |
47 | getFenForRepeat() { |
48 | return ( | |
90e814b6 | 49 | super.getFenForRepeat() + "_" + |
f9c36b2d BA |
50 | this.getReserveFen() + "_" + |
51 | this.getPromotedFen() | |
52 | ); | |
53 | } | |
54 | ||
6808d7a1 | 55 | getReserveFen() { |
dac39588 | 56 | let counts = new Array(10); |
6808d7a1 BA |
57 | for ( |
58 | let i = 0; | |
59 | i < V.PIECES.length - 1; | |
60 | i++ //-1: no king reserve | |
61 | ) { | |
dac39588 | 62 | counts[i] = this.reserve["w"][V.PIECES[i]]; |
6808d7a1 | 63 | counts[5 + i] = this.reserve["b"][V.PIECES[i]]; |
dac39588 BA |
64 | } |
65 | return counts.join(""); | |
66 | } | |
2d7194bd | 67 | |
6808d7a1 | 68 | getPromotedFen() { |
dac39588 | 69 | let res = ""; |
6808d7a1 BA |
70 | for (let i = 0; i < V.size.x; i++) { |
71 | for (let j = 0; j < V.size.y; j++) { | |
c292ebb2 | 72 | if (this.promoted[i][j]) res += V.CoordsToSquare({ x: i, y: j }) + ","; |
dac39588 BA |
73 | } |
74 | } | |
c292ebb2 | 75 | // Remove last comma: |
6808d7a1 | 76 | if (res.length > 0) res = res.slice(0, -1); |
6808d7a1 | 77 | else res = "-"; |
dac39588 BA |
78 | return res; |
79 | } | |
2d7194bd | 80 | |
6808d7a1 | 81 | setOtherVariables(fen) { |
dac39588 | 82 | super.setOtherVariables(fen); |
ded43c88 | 83 | const fenParsed = V.ParseFen(fen); |
dac39588 | 84 | // Also init reserves (used by the interface to show landable pieces) |
ded43c88 | 85 | const reserve = fenParsed.reserve.split("").map(x => parseInt(x, 10)); |
6808d7a1 BA |
86 | this.reserve = { |
87 | w: { | |
e50a8025 BA |
88 | [V.PAWN]: reserve[0], |
89 | [V.ROOK]: reserve[1], | |
90 | [V.KNIGHT]: reserve[2], | |
91 | [V.BISHOP]: reserve[3], | |
92 | [V.QUEEN]: reserve[4] | |
dac39588 | 93 | }, |
6808d7a1 | 94 | b: { |
e50a8025 BA |
95 | [V.PAWN]: reserve[5], |
96 | [V.ROOK]: reserve[6], | |
97 | [V.KNIGHT]: reserve[7], | |
98 | [V.BISHOP]: reserve[8], | |
99 | [V.QUEEN]: reserve[9] | |
dac39588 BA |
100 | } |
101 | }; | |
102 | this.promoted = ArrayFun.init(V.size.x, V.size.y, false); | |
6808d7a1 BA |
103 | if (fenParsed.promoted != "-") { |
104 | for (let square of fenParsed.promoted.split(",")) { | |
c292ebb2 BA |
105 | const coords = V.SquareToCoords(square); |
106 | this.promoted[coords.x][coords.y] = true; | |
dac39588 BA |
107 | } |
108 | } | |
109 | } | |
5c42c64e | 110 | |
6808d7a1 BA |
111 | getColor(i, j) { |
112 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; | |
dac39588 BA |
113 | return this.board[i][j].charAt(0); |
114 | } | |
2d7194bd | 115 | |
6808d7a1 BA |
116 | getPiece(i, j) { |
117 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; | |
dac39588 BA |
118 | return this.board[i][j].charAt(1); |
119 | } | |
a6abf094 | 120 | |
dac39588 | 121 | // Used by the interface: |
241bf8f2 | 122 | getReservePpath(index, color) { |
dac39588 BA |
123 | return color + V.RESERVE_PIECES[index]; |
124 | } | |
9d4a0218 BA |
125 | // // Version if some day I have pieces with numbers printed on it: |
126 | // getReservePpath(index, color) { | |
127 | // return ( | |
128 | // "Crazyhouse/" + | |
129 | // color + V.RESERVE_PIECES[index] + | |
130 | // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] | |
131 | // ); | |
132 | // } | |
a6abf094 | 133 | |
dac39588 | 134 | // Ordering on reserve pieces |
6808d7a1 BA |
135 | static get RESERVE_PIECES() { |
136 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]; | |
dac39588 | 137 | } |
1221ac47 | 138 | |
6808d7a1 | 139 | getReserveMoves([x, y]) { |
dac39588 BA |
140 | const color = this.turn; |
141 | const p = V.RESERVE_PIECES[y]; | |
6808d7a1 | 142 | if (this.reserve[color][p] == 0) return []; |
dac39588 | 143 | let moves = []; |
6808d7a1 BA |
144 | const pawnShift = p == V.PAWN ? 1 : 0; |
145 | for (let i = pawnShift; i < V.size.x - pawnShift; i++) { | |
146 | for (let j = 0; j < V.size.y; j++) { | |
147 | if (this.board[i][j] == V.EMPTY) { | |
dac39588 BA |
148 | let mv = new Move({ |
149 | appear: [ | |
150 | new PiPo({ | |
151 | x: i, | |
152 | y: j, | |
153 | c: color, | |
154 | p: p | |
155 | }) | |
156 | ], | |
157 | vanish: [], | |
6808d7a1 BA |
158 | start: { x: x, y: y }, //a bit artificial... |
159 | end: { x: i, y: j } | |
dac39588 BA |
160 | }); |
161 | moves.push(mv); | |
162 | } | |
163 | } | |
164 | } | |
165 | return moves; | |
166 | } | |
a6abf094 | 167 | |
6808d7a1 | 168 | getPotentialMovesFrom([x, y]) { |
1e8a8386 | 169 | if (x >= V.size.x) |
dac39588 | 170 | // Reserves, outside of board: x == sizeX(+1) |
6808d7a1 | 171 | return this.getReserveMoves([x, y]); |
dac39588 | 172 | // Standard moves |
6808d7a1 | 173 | return super.getPotentialMovesFrom([x, y]); |
dac39588 | 174 | } |
a6abf094 | 175 | |
6808d7a1 | 176 | getAllValidMoves() { |
0e001022 | 177 | let moves = super.getAllPotentialMoves(); |
dac39588 | 178 | const color = this.turn; |
0e001022 | 179 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
6808d7a1 BA |
180 | moves = moves.concat( |
181 | this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i]) | |
182 | ); | |
0e001022 | 183 | } |
dac39588 BA |
184 | return this.filterValid(moves); |
185 | } | |
a6abf094 | 186 | |
6808d7a1 | 187 | atLeastOneMove() { |
d982fffc BA |
188 | if (super.atLeastOneMove()) return true; |
189 | // Search one reserve move | |
190 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
191 | const moves = this.filterValid( | |
192 | this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) | |
193 | ); | |
194 | if (moves.length > 0) return true; | |
dac39588 | 195 | } |
d982fffc | 196 | return false; |
dac39588 | 197 | } |
a6abf094 | 198 | |
3a2a7b5f BA |
199 | postPlay(move) { |
200 | super.postPlay(move); | |
2c5d7b20 BA |
201 | // Skip castle: |
202 | if (move.vanish.length == 2 && move.appear.length == 2) return; | |
dac39588 | 203 | const color = move.appear[0].c; |
6808d7a1 | 204 | if (move.vanish.length == 0) { |
dac39588 BA |
205 | this.reserve[color][move.appear[0].p]--; |
206 | return; | |
207 | } | |
208 | move.movePromoted = this.promoted[move.start.x][move.start.y]; | |
6808d7a1 | 209 | move.capturePromoted = this.promoted[move.end.x][move.end.y]; |
dac39588 | 210 | this.promoted[move.start.x][move.start.y] = false; |
6808d7a1 BA |
211 | this.promoted[move.end.x][move.end.y] = |
212 | move.movePromoted || | |
213 | (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN); | |
214 | if (move.capturePromoted) this.reserve[color][V.PAWN]++; | |
215 | else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]++; | |
dac39588 | 216 | } |
1221ac47 | 217 | |
3a2a7b5f BA |
218 | postUndo(move) { |
219 | super.postUndo(move); | |
6808d7a1 | 220 | if (move.vanish.length == 2 && move.appear.length == 2) return; |
dac39588 | 221 | const color = this.turn; |
6808d7a1 | 222 | if (move.vanish.length == 0) { |
dac39588 BA |
223 | this.reserve[color][move.appear[0].p]++; |
224 | return; | |
225 | } | |
6808d7a1 | 226 | if (move.movePromoted) this.promoted[move.start.x][move.start.y] = true; |
dac39588 | 227 | this.promoted[move.end.x][move.end.y] = move.capturePromoted; |
6808d7a1 BA |
228 | if (move.capturePromoted) this.reserve[color][V.PAWN]--; |
229 | else if (move.vanish.length == 2) this.reserve[color][move.vanish[1].p]--; | |
dac39588 | 230 | } |
a6abf094 | 231 | |
6808d7a1 BA |
232 | static get SEARCH_DEPTH() { |
233 | return 2; | |
78d64531 | 234 | } |
a6abf094 | 235 | |
6808d7a1 | 236 | evalPosition() { |
dac39588 BA |
237 | let evaluation = super.evalPosition(); |
238 | // Add reserves: | |
6808d7a1 | 239 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
dac39588 BA |
240 | const p = V.RESERVE_PIECES[i]; |
241 | evaluation += this.reserve["w"][p] * V.VALUES[p]; | |
242 | evaluation -= this.reserve["b"][p] * V.VALUES[p]; | |
243 | } | |
244 | return evaluation; | |
245 | } | |
6752407b | 246 | |
6808d7a1 BA |
247 | getNotation(move) { |
248 | if (move.vanish.length > 0) return super.getNotation(move); | |
dac39588 BA |
249 | // Rebirth: |
250 | const piece = | |
6808d7a1 | 251 | move.appear[0].p != V.PAWN ? move.appear[0].p.toUpperCase() : ""; |
dac39588 BA |
252 | return piece + "@" + V.CoordsToSquare(move.end); |
253 | } | |
7e8a7ea1 | 254 | |
6808d7a1 | 255 | }; |