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 getPotentialPawnMoves([x
, y
]) {
43 const color
= this.turn
;
45 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
46 const shiftX
= color
== "w" ? -1 : 1;
47 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
50 x
+ shiftX
== lastRank
51 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.GRASSHOPPER
]
53 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
55 for (let piece
of finalPieces
) {
57 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
66 for (let shiftY
of [-1, 1]) {
70 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
71 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
73 for (let piece
of finalPieces
) {
75 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
87 getPotentialGrasshopperMoves([x
, y
]) {
89 // Look in every direction until an obstacle (to jump) is met
90 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
93 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
97 // Move is valid if the next square is empty or occupied by enemy
98 const nextSq
= [i
+step
[0], j
+step
[1]];
99 if (V
.OnBoard(nextSq
[0], nextSq
[1]) && this.canTake([x
, y
], nextSq
))
100 moves
.push(this.getBasicMove([x
, y
], nextSq
));
105 isAttacked(sq
, color
) {
107 super.isAttacked(sq
, color
) ||
108 this.isAttackedByGrasshopper(sq
, color
)
112 isAttackedByGrasshopper([x
, y
], color
) {
113 // Reversed process: is there an adjacent obstacle,
114 // and a grasshopper next in the same line?
115 for (const step
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
116 const nextSq
= [x
+step
[0], y
+step
[1]];
118 V
.OnBoard(nextSq
[0], nextSq
[1]) &&
119 this.board
[nextSq
[0]][nextSq
[1]] != V
.EMPTY
121 let i
= nextSq
[0] + step
[0];
122 let j
= nextSq
[1] + step
[1];
123 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
129 this.getPiece(i
, j
) == V
.GRASSHOPPER
&&
130 this.getColor(i
, j
) == color
139 static get VALUES() {
140 return Object
.assign(
141 // TODO: grasshoppers power decline with less pieces on board...
147 static get SEARCH_DEPTH() {
151 static GenRandInitFen(randomness
) {
152 return ChessRules
.GenRandInitFen(randomness
)
155 "/pppppppp/8/8/8/8/PPPPPPPP/",
156 "/gggggggg/pppppppp/8/8/PPPPPPPP/GGGGGGGG/"