5069e2317a10cd4f119491ac78d2a06e664419e3
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() {
16 promotions: ChessRules
.PawnSpecs
.promotions
.concat([V
.GRASSHOPPER
])
21 static get GRASSHOPPER() {
26 return ChessRules
.PIECES
.concat([V
.GRASSHOPPER
]);
30 return (b
[1] == V
.GRASSHOPPER
? "Grasshopper/" : "") + b
;
33 getPotentialMovesFrom([x
, y
]) {
34 switch (this.getPiece(x
, y
)) {
36 return this.getPotentialGrasshopperMoves([x
, y
]);
38 return super.getPotentialMovesFrom([x
, y
]);
42 getPotentialGrasshopperMoves([x
, y
]) {
44 // Look in every direction until an obstacle (to jump) is met
45 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
48 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
52 // Move is valid if the next square is empty or occupied by enemy
53 const nextSq
= [i
+step
[0], j
+step
[1]];
54 if (V
.OnBoard(nextSq
[0], nextSq
[1]) && this.canTake([x
, y
], nextSq
))
55 moves
.push(this.getBasicMove([x
, y
], nextSq
));
60 isAttacked(sq
, color
) {
62 super.isAttacked(sq
, color
) ||
63 this.isAttackedByGrasshopper(sq
, color
)
67 isAttackedByGrasshopper([x
, y
], color
) {
68 // Reversed process: is there an adjacent obstacle,
69 // and a grasshopper next in the same line?
70 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
71 const nextSq
= [x
+step
[0], y
+step
[1]];
73 V
.OnBoard(nextSq
[0], nextSq
[1]) &&
74 this.board
[nextSq
[0]][nextSq
[1]] != V
.EMPTY
76 let i
= nextSq
[0] + step
[0];
77 let j
= nextSq
[1] + step
[1];
78 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
84 this.getPiece(i
, j
) == V
.GRASSHOPPER
&&
85 this.getColor(i
, j
) == color
96 // TODO: grasshoppers power decline with less pieces on board...
102 static get SEARCH_DEPTH() {
106 static GenRandInitFen(randomness
) {
107 return ChessRules
.GenRandInitFen(randomness
)
110 "/pppppppp/8/8/8/8/PPPPPPPP/",
111 "/gggggggg/pppppppp/8/8/PPPPPPPP/GGGGGGGG/"