1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { sample
, randInt
} from "@/utils/alea";
5 export class WildebeestRules
extends ChessRules
{
7 return { x: 10, y: 11 };
13 static get WILDEBEEST() {
18 return ChessRules
.PIECES
.concat([V
.CAMEL
, V
.WILDEBEEST
]);
41 static IsGoodEnpassant(enpassant
) {
42 if (enpassant
!= "-") return !!enpassant
.match(/^([a-j][0-9]{1,2},?)+$/);
47 return ([V
.CAMEL
, V
.WILDEBEEST
].includes(b
[1]) ? "Wildebeest/" : "") + b
;
50 // There may be 2 enPassant squares (if pawn jump 3 squares)
52 const L
= this.epSquares
.length
;
53 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
55 this.epSquares
[L
- 1].forEach(sq
=> {
56 res
+= V
.CoordsToSquare(sq
) + ",";
58 return res
.slice(0, -1); //remove last comma
61 // En-passant after 2-sq or 3-sq jumps
62 getEpSquare(moveOrSquare
) {
63 if (!moveOrSquare
) return undefined;
64 if (typeof moveOrSquare
=== "string") {
65 const square
= moveOrSquare
;
66 if (square
== "-") return undefined;
68 square
.split(",").forEach(sq
=> {
69 res
.push(V
.SquareToCoords(sq
));
73 // Argument is a move:
74 const move = moveOrSquare
;
75 const [sx
, sy
, ex
] = [move.start
.x
, move.start
.y
, move.end
.x
];
76 if (this.getPiece(sx
, sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2) {
77 const step
= (ex
- sx
) / Math
.abs(ex
- sx
);
84 if (sx
+ 2 * step
!= ex
) {
93 return undefined; //default
96 getPotentialMovesFrom([x
, y
]) {
97 switch (this.getPiece(x
, y
)) {
99 return this.getPotentialCamelMoves([x
, y
]);
101 return this.getPotentialWildebeestMoves([x
, y
]);
103 return super.getPotentialMovesFrom([x
, y
]);
107 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
108 getPotentialPawnMoves([x
, y
]) {
109 const color
= this.turn
;
111 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
112 const shiftX
= color
== "w" ? -1 : 1;
113 const startRanks
= color
== "w" ? [sizeX
- 2, sizeX
- 3] : [1, 2];
114 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
115 const finalPieces
= x
+ shiftX
== lastRank
116 ? [V
.WILDEBEEST
, V
.QUEEN
]
119 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
120 // One square forward
121 for (let piece
of finalPieces
)
123 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], { c: color
, p: piece
})
125 if (startRanks
.includes(x
)) {
126 if (this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
) {
128 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
129 if (x
== startRanks
[0] && this.board
[x
+ 3 * shiftX
][y
] == V
.EMPTY
) {
130 // Three squares jump
131 moves
.push(this.getBasicMove([x
, y
], [x
+ 3 * shiftX
, y
]));
137 for (let shiftY
of [-1, 1]) {
140 y
+ shiftY
< sizeY
&&
141 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
142 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
144 for (let piece
of finalPieces
) {
146 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
156 const Lep
= this.epSquares
.length
;
157 const epSquare
= this.epSquares
[Lep
- 1];
159 for (let epsq
of epSquare
) {
160 // TODO: some redundant checks
161 if (epsq
.x
== x
+ shiftX
&& Math
.abs(epsq
.y
- y
) == 1) {
162 var enpassantMove
= this.getBasicMove([x
, y
], [epsq
.x
, epsq
.y
]);
163 // WARNING: the captured pawn may be diagonally behind us,
164 // if it's a 3-squares jump and we take on 1st passing square
165 const px
= this.board
[x
][epsq
.y
] != V
.EMPTY
? x : x
- shiftX
;
166 enpassantMove
.vanish
.push({
170 c: this.getColor(px
, epsq
.y
)
172 moves
.push(enpassantMove
);
180 // TODO: wildebeest castle
182 getPotentialCamelMoves(sq
) {
183 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.CAMEL
], "oneStep");
186 getPotentialWildebeestMoves(sq
) {
187 return this.getSlideNJumpMoves(
189 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]),
194 isAttacked(sq
, color
) {
196 super.isAttacked(sq
, color
) ||
197 this.isAttackedByCamel(sq
, color
) ||
198 this.isAttackedByWildebeest(sq
, color
)
202 isAttackedByCamel(sq
, color
) {
203 return this.isAttackedBySlideNJump(
212 isAttackedByWildebeest(sq
, color
) {
213 return this.isAttackedBySlideNJump(
217 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]),
223 if (this.atLeastOneMove()) return "*";
224 // No valid move: game is lost (stalemate is a win)
225 return this.turn
== "w" ? "0-1" : "1-0";
228 static get VALUES() {
229 return Object
.assign(
230 { c: 3, w: 7 }, //experimental
235 static get SEARCH_DEPTH() {
239 static GenRandInitFen(randomness
) {
240 if (!randomness
) randomness
= 2;
241 if (randomness
== 0) {
243 "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR " +
248 let pieces
= { w: new Array(11), b: new Array(11) };
250 for (let c
of ["w", "b"]) {
251 if (c
== 'b' && randomness
== 1) {
252 pieces
['b'] = pieces
['w'];
257 let positions
= ArrayFun
.range(11);
259 // Get random squares for bishops + camels (different colors)
260 let randIndexes
= sample(ArrayFun
.range(6), 2).map(i
=> {
263 let bishop1Pos
= positions
[randIndexes
[0]];
264 let camel1Pos
= positions
[randIndexes
[1]];
265 // The second bishop (camel) must be on a square of different color
266 let randIndexes_tmp
= sample(ArrayFun
.range(5), 2).map(i
=> {
269 let bishop2Pos
= positions
[randIndexes_tmp
[0]];
270 let camel2Pos
= positions
[randIndexes_tmp
[1]];
271 for (let idx
of randIndexes
.concat(randIndexes_tmp
).sort((a
, b
) => {
274 // Largest indices first
275 positions
.splice(idx
, 1);
278 let randIndex
= randInt(7);
279 let knight1Pos
= positions
[randIndex
];
280 positions
.splice(randIndex
, 1);
281 randIndex
= randInt(6);
282 let knight2Pos
= positions
[randIndex
];
283 positions
.splice(randIndex
, 1);
285 randIndex
= randInt(5);
286 let queenPos
= positions
[randIndex
];
287 positions
.splice(randIndex
, 1);
289 // Random square for wildebeest
290 randIndex
= randInt(4);
291 let wildebeestPos
= positions
[randIndex
];
292 positions
.splice(randIndex
, 1);
294 let rook1Pos
= positions
[0];
295 let kingPos
= positions
[1];
296 let rook2Pos
= positions
[2];
298 pieces
[c
][rook1Pos
] = "r";
299 pieces
[c
][knight1Pos
] = "n";
300 pieces
[c
][bishop1Pos
] = "b";
301 pieces
[c
][queenPos
] = "q";
302 pieces
[c
][camel1Pos
] = "c";
303 pieces
[c
][camel2Pos
] = "c";
304 pieces
[c
][wildebeestPos
] = "w";
305 pieces
[c
][kingPos
] = "k";
306 pieces
[c
][bishop2Pos
] = "b";
307 pieces
[c
][knight2Pos
] = "n";
308 pieces
[c
][rook2Pos
] = "r";
309 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
312 pieces
["b"].join("") +
313 "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" +
314 pieces
["w"].join("").toUpperCase() +
315 " w 0 " + flags
+ " -"