Some fixes, wrote some rules, implemented Wormhole variant
[vchess.git] / client / src / variants / Shatranj.js
1 // TODO: bishop OK, but queen should move vertical/horizontal and capture diagonally.
2 // ==> then the pawn promotion is a real promotion (enhancement).
3
4 import { ChessRules } from "@/base_rules";
5
6 export const VariantRules = class ShatranjRules extends ChessRules {
7 static get HasFlags() {
8 return false;
9 }
10
11 static get HasEnpassant() {
12 return false;
13 }
14
15 static get ElephantSteps() {
16 return [
17 [-2, -2],
18 [-2, 2],
19 [2, -2],
20 [2, 2]
21 ];
22 }
23
24 static GenRandInitFen() {
25 return ChessRules.GenRandInitFen().replace("w 1111 -", "w");
26 }
27
28 getPotentialPawnMoves([x, y]) {
29 const color = this.turn;
30 let moves = [];
31 const [sizeX, sizeY] = [V.size.x, V.size.y];
32 const shiftX = color == "w" ? -1 : 1;
33 const startRank = color == "w" ? sizeX - 2 : 1;
34 const lastRank = color == "w" ? 0 : sizeX - 1;
35 // Promotion in minister (queen) only:
36 const finalPiece = x + shiftX == lastRank ? V.QUEEN : V.PAWN;
37
38 if (this.board[x + shiftX][y] == V.EMPTY) {
39 // One square forward
40 moves.push(
41 this.getBasicMove([x, y], [x + shiftX, y], {
42 c: color,
43 p: finalPiece
44 })
45 );
46 }
47 // Captures
48 for (let shiftY of [-1, 1]) {
49 if (
50 y + shiftY >= 0 &&
51 y + shiftY < sizeY &&
52 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
53 this.canTake([x, y], [x + shiftX, y + shiftY])
54 ) {
55 moves.push(
56 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
57 c: color,
58 p: finalPiece
59 })
60 );
61 }
62 }
63
64 return moves;
65 }
66
67 getPotentialBishopMoves(sq) {
68 let moves = this.getSlideNJumpMoves(sq, V.ElephantSteps, "oneStep");
69 // Complete with "repositioning moves": like a queen, without capture
70 let repositioningMoves = this.getSlideNJumpMoves(
71 sq,
72 V.steps[V.BISHOP],
73 "oneStep"
74 ).filter(m => m.vanish.length == 1);
75 return moves.concat(repositioningMoves);
76 }
77
78 getPotentialQueenMoves(sq) {
79 return this.getSlideNJumpMoves(
80 sq,
81 V.steps[V.BISHOP],
82 "oneStep"
83 );
84 }
85
86 getPotentialKingMoves(sq) {
87 return this.getSlideNJumpMoves(
88 sq,
89 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
90 "oneStep"
91 );
92 }
93
94 isAttackedByBishop(sq, colors) {
95 return this.isAttackedBySlideNJump(
96 sq,
97 colors,
98 V.BISHOP,
99 V.ElephantSteps,
100 "oneStep"
101 );
102 }
103
104 isAttackedByQueen(sq, colors) {
105 return this.isAttackedBySlideNJump(
106 sq,
107 colors,
108 V.QUEEN,
109 V.steps[V.BISHOP],
110 "oneStep"
111 );
112 }
113
114 static get VALUES() {
115 return {
116 p: 1,
117 r: 5,
118 n: 3,
119 b: 2.5,
120 q: 2,
121 k: 1000
122 };
123 }
124 };