A few bug fixes
[vchess.git] / client / src / variants / Balaklava.js
CommitLineData
a0224b03
BA
1import { ChessRules } from "@/base_rules";
2
3export class BalaklavaRules extends ChessRules {
7e8a7ea1 4
a0224b03
BA
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
4313762d 49 static GenRandInitFen(options) {
a0224b03
BA
50 // No collision between 'n' and castle flags, so next replacement is fine
51 return (
4313762d 52 ChessRules.GenRandInitFen(options).replace(/n/g, 'm').replace(/N/g, 'M')
a0224b03
BA
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
f9f67166
BA
64 const color = this.turn;
65 const lastRank = (color == 'w' ? 0 : 7);
a0224b03 66 V.steps[V.KNIGHT].forEach(step => {
f9f67166
BA
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 }
a0224b03
BA
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) {
4313762d 92 return this.getSlideNJumpMoves(sq, V.steps[V.MAMMOTH], 1);
a0224b03
BA
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) {
4313762d
BA
103 return this.isAttackedBySlideNJump(
104 sq, color, V.MAMMOTH, V.steps[V.MAMMOTH], 1);
a0224b03
BA
105 }
106
107 static get SEARCH_DEPTH() {
108 return 2;
109 }
110
111 static get VALUES() {
112 return Object.assign(
113 // A mammoth is probably worth a little more than a knight
114 { m: 4 },
115 ChessRules.VALUES
116 );
117 }
7e8a7ea1 118
a0224b03 119};