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