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