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