1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class GrasshopperRules
extends ChessRules
{
6 static get HasEnpassant() {
10 static get PawnSpecs() {
14 { promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.GRASSHOPPER
]) }
18 static get GRASSHOPPER() {
23 return ChessRules
.PIECES
.concat([V
.GRASSHOPPER
]);
27 return (b
[1] == V
.GRASSHOPPER
? "Grasshopper/" : "") + b
;
30 getPotentialMovesFrom([x
, y
]) {
31 switch (this.getPiece(x
, y
)) {
33 return this.getPotentialGrasshopperMoves([x
, y
]);
35 return super.getPotentialMovesFrom([x
, y
]);
39 getPotentialGrasshopperMoves([x
, y
]) {
41 // Look in every direction until an obstacle (to jump) is met
42 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
45 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
49 // Move is valid if the next square is empty or occupied by enemy
50 const nextSq
= [i
+step
[0], j
+step
[1]];
51 if (V
.OnBoard(nextSq
[0], nextSq
[1]) && this.canTake([x
, y
], nextSq
))
52 moves
.push(this.getBasicMove([x
, y
], nextSq
));
57 isAttacked(sq
, color
) {
59 super.isAttacked(sq
, color
) ||
60 this.isAttackedByGrasshopper(sq
, color
)
64 isAttackedByGrasshopper([x
, y
], color
) {
65 // Reversed process: is there an adjacent obstacle,
66 // and a grasshopper next in the same line?
67 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
68 const nextSq
= [x
+step
[0], y
+step
[1]];
70 V
.OnBoard(nextSq
[0], nextSq
[1]) &&
71 this.board
[nextSq
[0]][nextSq
[1]] != V
.EMPTY
73 let i
= nextSq
[0] + step
[0];
74 let j
= nextSq
[1] + step
[1];
75 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
81 this.getPiece(i
, j
) == V
.GRASSHOPPER
&&
82 this.getColor(i
, j
) == color
93 // TODO: grasshoppers power decline with less pieces on board...
99 static get SEARCH_DEPTH() {
103 static GenRandInitFen(randomness
) {
104 return ChessRules
.GenRandInitFen(randomness
)
107 "/pppppppp/8/8/8/8/PPPPPPPP/",
108 "/gggggggg/pppppppp/8/8/PPPPPPPP/GGGGGGGG/"