Early draft of TitanChess
[vchess.git] / client / src / variants / Balaklava.js
1 import { ChessRules } from "@/base_rules";
2
3 export class BalaklavaRules extends ChessRules {
4 static get PawnSpecs() {
5 return Object.assign(
6 {},
7 ChessRules.PawnSpecs,
8 { promotions: [V.ROOK, V.MAMMOTH, V.BISHOP, V.QUEEN] }
9 );
10 }
11
12 static get HasEnpassant() {
13 return false;
14 }
15
16 getPpath(b) {
17 return (b[1] == V.MAMMOTH ? "Balaklava/" : "") + b;
18 }
19
20 // Alfil + Dabbaba:
21 static get MAMMOTH() {
22 return "m";
23 }
24
25 static get PIECES() {
26 return [V.PAWN, V.ROOK, V.MAMMOTH, V.BISHOP, V.QUEEN, V.KING];
27 }
28
29 static get steps() {
30 return Object.assign(
31 {},
32 ChessRules.steps,
33 {
34 m: [
35 [-2, -2],
36 [-2, 0],
37 [-2, 2],
38 [0, -2],
39 [0, 2],
40 [2, -2],
41 [2, 0],
42 [2, 2],
43 ]
44 }
45 );
46 }
47
48 static GenRandInitFen(randomness) {
49 // No collision between 'n' and castle flags, so next replacement is fine
50 return (
51 ChessRules.GenRandInitFen(randomness)
52 .replace(/n/g, 'm').replace(/N/g, 'M')
53 );
54 }
55
56 getPotentialMovesFrom([x, y]) {
57 const piece = this.getPiece(x, y);
58 let moves =
59 piece == V.MAMMOTH
60 ? this.getPotentialMammothMoves([x, y])
61 : super.getPotentialMovesFrom([x, y]);
62 if (piece != V.KING) {
63 // Add non-capturing knight movements
64 const color = this.turn;
65 const lastRank = (color == 'w' ? 0 : 7);
66 V.steps[V.KNIGHT].forEach(step => {
67 // Pawns cannot go backward:
68 if (
69 piece == V.PAWN &&
70 (
71 (color == 'w' && step[0] > 0) ||
72 (color == 'b' && step[0] < 0)
73 )
74 ) {
75 return;
76 }
77 const [i, j] = [x + step[0], y + step[1]];
78 if (
79 V.OnBoard(i, j) &&
80 this.board[i][j] == V.EMPTY &&
81 // Pawns don't promote with a knight move
82 (piece != V.PAWN || i != lastRank)
83 ) {
84 moves.push(this.getBasicMove([x, y], [i, j]));
85 }
86 });
87 }
88 return moves;
89 }
90
91 getPotentialMammothMoves(sq) {
92 return this.getSlideNJumpMoves(sq, V.steps[V.MAMMOTH], "oneStep");
93 }
94
95 isAttacked(sq, color) {
96 return (
97 super.isAttacked(sq, color) ||
98 this.isAttackedByMammoth(sq, color)
99 );
100 }
101
102 isAttackedByMammoth(sq, color) {
103 return (
104 this.isAttackedBySlideNJump(
105 sq, color, V.MAMMOTH, V.steps[V.MAMMOTH], "oneStep")
106 );
107 }
108
109 static get SEARCH_DEPTH() {
110 return 2;
111 }
112
113 static get VALUES() {
114 return Object.assign(
115 // A mammoth is probably worth a little more than a knight
116 { m: 4 },
117 ChessRules.VALUES
118 );
119 }
120 };