Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Grasshopper.js
CommitLineData
a97bdbda
BA
1import { ChessRules } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class GrasshopperRules extends ChessRules {
7e8a7ea1 6
b627d118
BA
7 static get HasEnpassant() {
8 return false;
9 }
10
32f6285e
BA
11 static get PawnSpecs() {
12 return Object.assign(
13 {},
14 ChessRules.PawnSpecs,
1c15969e 15 { promotions: ChessRules.PawnSpecs.promotions.concat([V.GRASSHOPPER]) }
32f6285e
BA
16 );
17 }
18
a97bdbda
BA
19 static get GRASSHOPPER() {
20 return "g";
21 }
22
23 static get PIECES() {
24 return ChessRules.PIECES.concat([V.GRASSHOPPER]);
25 }
26
27 getPpath(b) {
28 return (b[1] == V.GRASSHOPPER ? "Grasshopper/" : "") + b;
29 }
30
31 getPotentialMovesFrom([x, y]) {
32 switch (this.getPiece(x, y)) {
33 case V.GRASSHOPPER:
34 return this.getPotentialGrasshopperMoves([x, y]);
35 default:
36 return super.getPotentialMovesFrom([x, y]);
37 }
38 }
39
40 getPotentialGrasshopperMoves([x, y]) {
41 let moves = [];
42 // Look in every direction until an obstacle (to jump) is met
43 for (const step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
44 let i = x + step[0];
45 let j = y + step[1];
46 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
47 i += step[0];
48 j += step[1];
49 }
50 // Move is valid if the next square is empty or occupied by enemy
51 const nextSq = [i+step[0], j+step[1]];
52 if (V.OnBoard(nextSq[0], nextSq[1]) && this.canTake([x, y], nextSq))
53 moves.push(this.getBasicMove([x, y], nextSq));
54 }
55 return moves;
56 }
57
68e19a44 58 isAttacked(sq, color) {
a97bdbda 59 return (
68e19a44
BA
60 super.isAttacked(sq, color) ||
61 this.isAttackedByGrasshopper(sq, color)
a97bdbda
BA
62 );
63 }
64
68e19a44 65 isAttackedByGrasshopper([x, y], color) {
a97bdbda
BA
66 // Reversed process: is there an adjacent obstacle,
67 // and a grasshopper next in the same line?
68 for (const step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
69 const nextSq = [x+step[0], y+step[1]];
70 if (
71 V.OnBoard(nextSq[0], nextSq[1]) &&
72 this.board[nextSq[0]][nextSq[1]] != V.EMPTY
73 ) {
74 let i = nextSq[0] + step[0];
75 let j = nextSq[1] + step[1];
76 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
77 i += step[0];
78 j += step[1];
79 }
80 if (
81 V.OnBoard(i, j) &&
82 this.getPiece(i, j) == V.GRASSHOPPER &&
68e19a44 83 this.getColor(i, j) == color
a97bdbda
BA
84 ) {
85 return true;
86 }
87 }
88 }
89 return false;
90 }
91
92 static get VALUES() {
93 return Object.assign(
1eb6c9dd
BA
94 // TODO: grasshoppers power decline with less pieces on board...
95 { g: 2 },
a97bdbda
BA
96 ChessRules.VALUES
97 );
98 }
99
b83a675a
BA
100 static get SEARCH_DEPTH() {
101 return 2;
102 }
103
4313762d
BA
104 static GenRandInitFen(options) {
105 return ChessRules.GenRandInitFen(options)
3a2a7b5f 106 .slice(0, -2)
1eb6c9dd
BA
107 .replace(
108 "/pppppppp/8/8/8/8/PPPPPPPP/",
109 "/gggggggg/pppppppp/8/8/PPPPPPPP/GGGGGGGG/"
110 );
a97bdbda 111 }
7e8a7ea1 112
a97bdbda 113};