Commit | Line | Data |
---|---|---|
472c0c4f BA |
1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
3 | import { randInt } from "@/utils/alea"; | |
4 | ||
5 | export class OmegaRules extends ChessRules { | |
7e8a7ea1 | 6 | |
472c0c4f BA |
7 | static get PawnSpecs() { |
8 | return Object.assign( | |
9 | {}, | |
10 | ChessRules.PawnSpecs, | |
11 | { | |
12 | initShift: { w: 2, b: 2 }, | |
13 | threeSquares: true, | |
14 | promotions: | |
15 | ChessRules.PawnSpecs.promotions.concat([V.CHAMPION, V.WIZARD]) | |
16 | } | |
17 | ); | |
18 | } | |
19 | ||
157a72c8 BA |
20 | static get DarkBottomRight() { |
21 | return true; | |
22 | } | |
23 | ||
472c0c4f BA |
24 | // For space between corners: |
25 | static get NOTHING() { | |
26 | return "xx"; | |
27 | } | |
28 | ||
29 | static board2fen(b) { | |
30 | if (b[0] == 'x') return 'x'; | |
31 | return ChessRules.board2fen(b); | |
32 | } | |
33 | ||
34 | static fen2board(f) { | |
35 | if (f == 'x') return V.NOTHING; | |
36 | return ChessRules.fen2board(f); | |
37 | } | |
38 | ||
39 | getPpath(b) { | |
40 | if (b[0] == 'x') return "Omega/nothing"; | |
41 | return ([V.CHAMPION, V.WIZARD].includes(b[1]) ? "Omega/" : "") + b; | |
42 | } | |
43 | ||
e90bafa8 | 44 | // TODO: the wall position should be checked too |
c0b036aa BA |
45 | static IsGoodPosition(position) { |
46 | if (position.length == 0) return false; | |
47 | const rows = position.split("/"); | |
48 | if (rows.length != V.size.x) return false; | |
49 | let kings = { "k": 0, "K": 0 }; | |
50 | for (let row of rows) { | |
51 | let sumElts = 0; | |
52 | for (let i = 0; i < row.length; i++) { | |
53 | if (['K','k'].includes(row[i])) kings[row[i]]++; | |
54 | if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++; | |
55 | else { | |
e50a8025 | 56 | const num = parseInt(row[i], 10); |
c0b036aa BA |
57 | if (isNaN(num)) return false; |
58 | sumElts += num; | |
59 | } | |
60 | } | |
61 | if (sumElts != V.size.y) return false; | |
62 | } | |
63 | if (Object.values(kings).some(v => v != 1)) return false; | |
64 | return true; | |
65 | } | |
66 | ||
472c0c4f BA |
67 | // NOTE: keep this extensive check because the board has holes |
68 | static IsGoodEnpassant(enpassant) { | |
69 | if (enpassant != "-") { | |
70 | const squares = enpassant.split(","); | |
71 | if (squares.length > 2) return false; | |
72 | for (let sq of squares) { | |
73 | const ep = V.SquareToCoords(sq); | |
74 | if (isNaN(ep.x) || !V.OnBoard(ep)) return false; | |
75 | } | |
76 | } | |
77 | return true; | |
78 | } | |
79 | ||
80 | static get size() { | |
81 | return { x: 12, y: 12 }; | |
82 | } | |
83 | ||
84 | static OnBoard(x, y) { | |
85 | return ( | |
86 | (x >= 1 && x <= 10 && y >= 1 && y <= 10) || | |
87 | (x == 11 && [0, 11].includes(y)) || | |
88 | (x == 0 && [0, 11].includes(y)) | |
89 | ); | |
90 | } | |
91 | ||
92 | // Dabbabah + alfil + wazir | |
93 | static get CHAMPION() { | |
94 | return "c"; | |
95 | } | |
96 | ||
97 | // Camel + ferz | |
98 | static get WIZARD() { | |
99 | return "w"; | |
100 | } | |
101 | ||
102 | static get PIECES() { | |
103 | return ChessRules.PIECES.concat([V.CHAMPION, V.WIZARD]); | |
104 | } | |
105 | ||
106 | static get steps() { | |
107 | return Object.assign( | |
108 | {}, | |
109 | ChessRules.steps, | |
110 | { | |
111 | w: [ | |
112 | [-3, -1], | |
113 | [-3, 1], | |
114 | [-1, -3], | |
115 | [-1, 3], | |
116 | [1, -3], | |
117 | [1, 3], | |
118 | [3, -1], | |
119 | [3, 1], | |
120 | [-1, -1], | |
121 | [-1, 1], | |
122 | [1, -1], | |
123 | [1, 1] | |
124 | ], | |
125 | c: [ | |
126 | [1, 0], | |
127 | [-1, 0], | |
128 | [0, 1], | |
129 | [0, -1], | |
130 | [2, 2], | |
131 | [2, -2], | |
132 | [-2, 2], | |
133 | [-2, -2], | |
134 | [-2, 0], | |
135 | [0, -2], | |
136 | [2, 0], | |
137 | [0, 2] | |
138 | ] | |
139 | } | |
140 | ); | |
141 | } | |
142 | ||
143 | static GenRandInitFen(randomness) { | |
144 | if (randomness == 0) { | |
145 | return ( | |
146 | "wxxxxxxxxxxw/xcrnbqkbnrcx/xppppppppppx/x91x/x91x/x91x/" + | |
147 | "x91x/x91x/x91x/xPPPPPPPPPPx/xCRNBQKBNRCx/WxxxxxxxxxxW " + | |
148 | "w 0 cjcj -" | |
149 | ); | |
150 | } | |
151 | ||
152 | let pieces = { w: new Array(10), b: new Array(10) }; | |
153 | let flags = ""; | |
154 | // Shuffle pieces on first (and last rank if randomness == 2) | |
155 | for (let c of ["w", "b"]) { | |
156 | if (c == 'b' && randomness == 1) { | |
157 | pieces['b'] = pieces['w']; | |
158 | flags += flags; | |
159 | break; | |
160 | } | |
161 | ||
162 | let positions = ArrayFun.range(10); | |
163 | ||
164 | // Get random squares for bishops | |
165 | let randIndex = 2 * randInt(5); | |
166 | const bishop1Pos = positions[randIndex]; | |
167 | // The second bishop must be on a square of different color | |
168 | let randIndex_tmp = 2 * randInt(5) + 1; | |
169 | const bishop2Pos = positions[randIndex_tmp]; | |
472c0c4f BA |
170 | |
171 | // Get random squares for champions | |
e90bafa8 BA |
172 | let randIndexC = 2 * randInt(4); |
173 | if (randIndexC >= bishop1Pos) randIndexC += 2; | |
174 | const champion1Pos = positions[randIndexC]; | |
472c0c4f | 175 | // The second champion must be on a square of different color |
e90bafa8 BA |
176 | let randIndex_tmpC = 2 * randInt(4) + 1; |
177 | if (randIndex_tmpC >= bishop2Pos) randIndex_tmpC += 2; | |
178 | const champion2Pos = positions[randIndex_tmpC]; | |
179 | ||
180 | let usedIndices = [randIndex, randIndex_tmp, randIndexC, randIndex_tmpC]; | |
181 | usedIndices.sort(); | |
182 | for (let i = 3; i >= 0; i--) positions.splice(usedIndices[i], 1); | |
472c0c4f BA |
183 | |
184 | // Get random squares for other pieces | |
185 | randIndex = randInt(6); | |
186 | const knight1Pos = positions[randIndex]; | |
187 | positions.splice(randIndex, 1); | |
188 | randIndex = randInt(5); | |
189 | const knight2Pos = positions[randIndex]; | |
190 | positions.splice(randIndex, 1); | |
191 | ||
192 | randIndex = randInt(4); | |
193 | const queenPos = positions[randIndex]; | |
194 | positions.splice(randIndex, 1); | |
195 | ||
196 | // Rooks and king positions are now fixed | |
197 | const rook1Pos = positions[0]; | |
198 | const kingPos = positions[1]; | |
199 | const rook2Pos = positions[2]; | |
200 | ||
201 | pieces[c][champion1Pos] = "c"; | |
202 | pieces[c][rook1Pos] = "r"; | |
203 | pieces[c][knight1Pos] = "n"; | |
204 | pieces[c][bishop1Pos] = "b"; | |
205 | pieces[c][queenPos] = "q"; | |
206 | pieces[c][kingPos] = "k"; | |
207 | pieces[c][bishop2Pos] = "b"; | |
208 | pieces[c][knight2Pos] = "n"; | |
209 | pieces[c][rook2Pos] = "r"; | |
210 | pieces[c][champion2Pos] = "c"; | |
e90bafa8 | 211 | flags += V.CoordToColumn(rook1Pos+1) + V.CoordToColumn(rook2Pos+1); |
472c0c4f BA |
212 | } |
213 | // Add turn + flags + enpassant | |
214 | return ( | |
215 | "wxxxxxxxxxxw/" + | |
216 | "x" + pieces["b"].join("") + | |
217 | "x/xppppppppppx/x91x/x91x/x91x/x91x/x91x/x91x/xPPPPPPPPPPx/x" + | |
218 | pieces["w"].join("").toUpperCase() + "x" + | |
219 | "/WxxxxxxxxxxW " + | |
220 | "w 0 " + flags + " -" | |
221 | ); | |
222 | } | |
223 | ||
224 | // There may be 2 enPassant squares (if pawn jump 3 squares) | |
225 | getEnpassantFen() { | |
226 | const L = this.epSquares.length; | |
227 | if (!this.epSquares[L - 1]) return "-"; //no en-passant | |
228 | let res = ""; | |
229 | this.epSquares[L - 1].forEach(sq => { | |
230 | res += V.CoordsToSquare(sq) + ","; | |
231 | }); | |
232 | return res.slice(0, -1); //remove last comma | |
233 | } | |
234 | ||
e0953a57 BA |
235 | canTake([x1, y1], [x2, y2]) { |
236 | return ( | |
237 | // Cannot take wall :) | |
e90bafa8 | 238 | // NOTE: this check is useful only for pawns where OnBoard() isn't used |
e0953a57 BA |
239 | this.board[x2][y2] != V.NOTHING && |
240 | this.getColor(x1, y1) !== this.getColor(x2, y2) | |
241 | ); | |
242 | } | |
243 | ||
472c0c4f BA |
244 | // En-passant after 2-sq or 3-sq jumps |
245 | getEpSquare(moveOrSquare) { | |
246 | if (!moveOrSquare) return undefined; | |
247 | if (typeof moveOrSquare === "string") { | |
248 | const square = moveOrSquare; | |
249 | if (square == "-") return undefined; | |
250 | let res = []; | |
251 | square.split(",").forEach(sq => { | |
252 | res.push(V.SquareToCoords(sq)); | |
253 | }); | |
254 | return res; | |
255 | } | |
256 | // Argument is a move: | |
257 | const move = moveOrSquare; | |
258 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; | |
259 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) { | |
260 | const step = (ex - sx) / Math.abs(ex - sx); | |
261 | let res = [ | |
262 | { | |
263 | x: sx + step, | |
264 | y: sy | |
265 | } | |
266 | ]; | |
267 | if (sx + 2 * step != ex) { | |
268 | // 3-squares jump | |
269 | res.push({ | |
270 | x: sx + 2 * step, | |
271 | y: sy | |
272 | }); | |
273 | } | |
274 | return res; | |
275 | } | |
276 | return undefined; //default | |
277 | } | |
278 | ||
279 | getPotentialMovesFrom([x, y]) { | |
280 | switch (this.getPiece(x, y)) { | |
281 | case V.CHAMPION: | |
282 | return this.getPotentialChampionMoves([x, y]); | |
283 | case V.WIZARD: | |
284 | return this.getPotentialWizardMoves([x, y]); | |
285 | default: | |
286 | return super.getPotentialMovesFrom([x, y]); | |
287 | } | |
288 | } | |
289 | ||
f6c6d0c3 | 290 | getEnpassantCaptures([x, y], shiftX) { |
472c0c4f BA |
291 | const Lep = this.epSquares.length; |
292 | const epSquare = this.epSquares[Lep - 1]; | |
293 | let moves = []; | |
294 | if (!!epSquare) { | |
295 | for (let epsq of epSquare) { | |
296 | // TODO: some redundant checks | |
297 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { | |
298 | let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); | |
299 | // WARNING: the captured pawn may be diagonally behind us, | |
300 | // if it's a 3-squares jump and we take on 1st passing square | |
301 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; | |
302 | enpassantMove.vanish.push({ | |
303 | x: px, | |
304 | y: epsq.y, | |
305 | p: "p", | |
306 | c: this.getColor(px, epsq.y) | |
307 | }); | |
308 | moves.push(enpassantMove); | |
309 | } | |
310 | } | |
311 | } | |
312 | return moves; | |
313 | } | |
314 | ||
b19c6896 BA |
315 | addPawnMoves([x1, y1], [x2, y2], moves, promotions) { |
316 | let finalPieces = [V.PAWN]; | |
317 | const color = this.turn; | |
318 | const lastRank = (color == "w" ? 1 : V.size.x - 2); | |
319 | if (x2 == lastRank) { | |
320 | // promotions arg: special override for Hiddenqueen variant | |
321 | if (!!promotions) finalPieces = promotions; | |
322 | else if (!!V.PawnSpecs.promotions) finalPieces = V.PawnSpecs.promotions; | |
323 | } | |
324 | let tr = null; | |
325 | for (let piece of finalPieces) { | |
326 | tr = (piece != V.PAWN ? { c: color, p: piece } : null); | |
327 | moves.push(this.getBasicMove([x1, y1], [x2, y2], tr)); | |
328 | } | |
329 | } | |
330 | ||
472c0c4f BA |
331 | getPotentialChampionMoves(sq) { |
332 | return this.getSlideNJumpMoves(sq, V.steps[V.CHAMPION], "oneStep"); | |
333 | } | |
334 | ||
335 | getPotentialWizardMoves(sq) { | |
336 | return this.getSlideNJumpMoves(sq, V.steps[V.WIZARD], "oneStep"); | |
337 | } | |
338 | ||
7e8a7ea1 | 339 | getCastleMoves([x, y]) { |
472c0c4f BA |
340 | const finalSquares = [ |
341 | [4, 5], | |
342 | [8, 7] | |
343 | ]; | |
7e8a7ea1 | 344 | return super.getCastleMoves([x, y], finalSquares); |
472c0c4f BA |
345 | } |
346 | ||
347 | isAttacked(sq, color) { | |
348 | return ( | |
349 | super.isAttacked(sq, color) || | |
350 | this.isAttackedByChampion(sq, color) || | |
351 | this.isAttackedByWizard(sq, color) | |
352 | ); | |
353 | } | |
354 | ||
355 | isAttackedByWizard(sq, color) { | |
356 | return ( | |
357 | this.isAttackedBySlideNJump( | |
358 | sq, color, V.WIZARD, V.steps[V.WIZARD], "oneStep") | |
359 | ); | |
360 | } | |
361 | ||
362 | isAttackedByChampion(sq, color) { | |
363 | return ( | |
364 | this.isAttackedBySlideNJump( | |
365 | sq, color, V.CHAMPION, V.steps[V.CHAMPION], "oneStep") | |
366 | ); | |
367 | } | |
368 | ||
369 | updateCastleFlags(move, piece) { | |
370 | const c = V.GetOppCol(this.turn); | |
371 | const firstRank = (c == "w" ? V.size.x - 2 : 1); | |
372 | // Update castling flags if rooks are moved | |
373 | const oppCol = this.turn; | |
374 | const oppFirstRank = V.size.x - 1 - firstRank; | |
375 | if (piece == V.KING) | |
376 | this.castleFlags[c] = [V.size.y, V.size.y]; | |
377 | else if ( | |
378 | move.start.x == firstRank && //our rook moves? | |
379 | this.castleFlags[c].includes(move.start.y) | |
380 | ) { | |
381 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); | |
382 | this.castleFlags[c][flagIdx] = V.size.y; | |
383 | } | |
384 | // NOTE: not "else if" because a rook could take an opposing rook | |
385 | if ( | |
386 | move.end.x == oppFirstRank && //we took opponent rook? | |
387 | this.castleFlags[oppCol].includes(move.end.y) | |
388 | ) { | |
389 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); | |
390 | this.castleFlags[oppCol][flagIdx] = V.size.y; | |
391 | } | |
392 | } | |
393 | ||
394 | static get SEARCH_DEPTH() { | |
395 | return 2; | |
396 | } | |
397 | ||
398 | // Values taken from https://omegachess.com/strategy.htm | |
399 | static get VALUES() { | |
400 | return { | |
401 | p: 1, | |
402 | n: 2, | |
403 | b: 4, | |
404 | r: 6, | |
405 | q: 12, | |
406 | w: 4, | |
407 | c: 4, | |
408 | k: 1000 | |
409 | }; | |
410 | } | |
f92d56d6 BA |
411 | |
412 | evalPosition() { | |
413 | let evaluation = 0; | |
414 | for (let i = 0; i < V.size.x; i++) { | |
415 | for (let j = 0; j < V.size.y; j++) { | |
416 | if (![V.EMPTY,V.NOTHING].includes(this.board[i][j])) { | |
417 | const sign = this.getColor(i, j) == "w" ? 1 : -1; | |
418 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; | |
419 | } | |
420 | } | |
421 | } | |
422 | return evaluation; | |
423 | } | |
7e8a7ea1 | 424 | |
472c0c4f | 425 | }; |