1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class GrasshopperRules
extends ChessRules
{
7 static get HasEnpassant() {
11 static get PawnSpecs() {
15 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.GRASSHOPPER
]) }
19 static get GRASSHOPPER() {
24 return ChessRules
.PIECES
.concat([V
.GRASSHOPPER
]);
28 return (b
[1] == V
.GRASSHOPPER
? "Grasshopper/" : "") + b
;
31 getPotentialMovesFrom([x
, y
]) {
32 switch (this.getPiece(x
, y
)) {
34 return this.getPotentialGrasshopperMoves([x
, y
]);
36 return super.getPotentialMovesFrom([x
, y
]);
40 getPotentialGrasshopperMoves([x
, y
]) {
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
])) {
46 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
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
));
58 isAttacked(sq
, color
) {
60 super.isAttacked(sq
, color
) ||
61 this.isAttackedByGrasshopper(sq
, color
)
65 isAttackedByGrasshopper([x
, y
], color
) {
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]];
71 V
.OnBoard(nextSq
[0], nextSq
[1]) &&
72 this.board
[nextSq
[0]][nextSq
[1]] != V
.EMPTY
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
) {
82 this.getPiece(i
, j
) == V
.GRASSHOPPER
&&
83 this.getColor(i
, j
) == color
94 // TODO: grasshoppers power decline with less pieces on board...
100 static get SEARCH_DEPTH() {
104 static GenRandInitFen(options
) {
105 return ChessRules
.GenRandInitFen(options
)
108 "/pppppppp/8/8/8/8/PPPPPPPP/",
109 "/gggggggg/pppppppp/8/8/PPPPPPPP/GGGGGGGG/"