8192fa04d6b4dce8dca6342baad1db1b0a8570a0
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
]);
23 ChessRules
.steps
, //add camel moves:
39 static IsGoodEnpassant(enpassant
) {
40 if (enpassant
!= "-") {
41 const squares
= enpassant
.split(",");
42 if (squares
.length
> 2) return false;
43 for (let sq
of squares
) {
44 const ep
= V
.SquareToCoords(sq
);
45 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
52 return ([V
.CAMEL
, V
.WILDEBEEST
].includes(b
[1]) ? "Wildebeest/" : "") + b
;
55 // There may be 2 enPassant squares (if pawn jump 3 squares)
57 const L
= this.epSquares
.length
;
58 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
60 this.epSquares
[L
- 1].forEach(sq
=> {
61 res
+= V
.CoordsToSquare(sq
) + ",";
63 return res
.slice(0, -1); //remove last comma
66 // En-passant after 2-sq or 3-sq jumps
67 getEpSquare(moveOrSquare
) {
68 if (!moveOrSquare
) return undefined;
69 if (typeof moveOrSquare
=== "string") {
70 const square
= moveOrSquare
;
71 if (square
== "-") return undefined;
73 square
.split(",").forEach(sq
=> {
74 res
.push(V
.SquareToCoords(sq
));
78 // Argument is a move:
79 const move = moveOrSquare
;
80 const [sx
, sy
, ex
] = [move.start
.x
, move.start
.y
, move.end
.x
];
81 if (this.getPiece(sx
, sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2) {
82 const step
= (ex
- sx
) / Math
.abs(ex
- sx
);
89 if (sx
+ 2 * step
!= ex
) {
98 return undefined; //default
101 getPotentialMovesFrom([x
, y
]) {
102 switch (this.getPiece(x
, y
)) {
104 return this.getPotentialCamelMoves([x
, y
]);
106 return this.getPotentialWildebeestMoves([x
, y
]);
108 return super.getPotentialMovesFrom([x
, y
]);
112 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
113 getPotentialPawnMoves([x
, y
]) {
114 const color
= this.turn
;
116 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
117 const shiftX
= color
== "w" ? -1 : 1;
118 const startRanks
= color
== "w" ? [sizeX
- 2, sizeX
- 3] : [1, 2];
119 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
120 const finalPieces
= x
+ shiftX
== lastRank
121 ? [V
.WILDEBEEST
, V
.QUEEN
]
124 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
125 // One square forward
126 for (let piece
of finalPieces
)
128 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], { c: color
, p: piece
})
130 if (startRanks
.includes(x
)) {
131 if (this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
) {
133 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
134 if (x
== startRanks
[0] && this.board
[x
+ 3 * shiftX
][y
] == V
.EMPTY
) {
135 // Three squares jump
136 moves
.push(this.getBasicMove([x
, y
], [x
+ 3 * shiftX
, y
]));
142 for (let shiftY
of [-1, 1]) {
145 y
+ shiftY
< sizeY
&&
146 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
147 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
149 for (let piece
of finalPieces
) {
151 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
161 const Lep
= this.epSquares
.length
;
162 const epSquare
= this.epSquares
[Lep
- 1];
164 for (let epsq
of epSquare
) {
165 // TODO: some redundant checks
166 if (epsq
.x
== x
+ shiftX
&& Math
.abs(epsq
.y
- y
) == 1) {
167 var enpassantMove
= this.getBasicMove([x
, y
], [epsq
.x
, epsq
.y
]);
168 // WARNING: the captured pawn may be diagonally behind us,
169 // if it's a 3-squares jump and we take on 1st passing square
170 const px
= this.board
[x
][epsq
.y
] != V
.EMPTY
? x : x
- shiftX
;
171 enpassantMove
.vanish
.push({
175 c: this.getColor(px
, epsq
.y
)
177 moves
.push(enpassantMove
);
185 // TODO: wildebeest castle
187 getPotentialCamelMoves(sq
) {
188 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.CAMEL
], "oneStep");
191 getPotentialWildebeestMoves(sq
) {
192 return this.getSlideNJumpMoves(
194 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]),
199 isAttacked(sq
, color
) {
201 super.isAttacked(sq
, color
) ||
202 this.isAttackedByCamel(sq
, color
) ||
203 this.isAttackedByWildebeest(sq
, color
)
207 isAttackedByCamel(sq
, color
) {
208 return this.isAttackedBySlideNJump(
217 isAttackedByWildebeest(sq
, color
) {
218 return this.isAttackedBySlideNJump(
222 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]),
228 if (this.atLeastOneMove())
232 // No valid move: game is lost (stalemate is a win)
233 return this.turn
== "w" ? "0-1" : "1-0";
236 static get VALUES() {
237 return Object
.assign(
238 { c: 3, w: 7 }, //experimental
243 static get SEARCH_DEPTH() {
247 static GenRandInitFen(randomness
) {
248 if (!randomness
) randomness
= 2;
250 return "rnccwkqbbnr/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/RNBBQKWCCNR w 0 akak -";
252 let pieces
= { w: new Array(10), b: new Array(10) };
254 for (let c
of ["w", "b"]) {
255 if (c
== 'b' && randomness
== 1) {
256 pieces
['b'] = pieces
['w'];
261 let positions
= ArrayFun
.range(11);
263 // Get random squares for bishops + camels (different colors)
264 let randIndexes
= sample(ArrayFun
.range(6), 2).map(i
=> {
267 let bishop1Pos
= positions
[randIndexes
[0]];
268 let camel1Pos
= positions
[randIndexes
[1]];
269 // The second bishop (camel) must be on a square of different color
270 let randIndexes_tmp
= sample(ArrayFun
.range(5), 2).map(i
=> {
273 let bishop2Pos
= positions
[randIndexes_tmp
[0]];
274 let camel2Pos
= positions
[randIndexes_tmp
[1]];
275 for (let idx
of randIndexes
.concat(randIndexes_tmp
).sort((a
, b
) => {
278 // Largest indices first
279 positions
.splice(idx
, 1);
282 let randIndex
= randInt(7);
283 let knight1Pos
= positions
[randIndex
];
284 positions
.splice(randIndex
, 1);
285 randIndex
= randInt(6);
286 let knight2Pos
= positions
[randIndex
];
287 positions
.splice(randIndex
, 1);
289 randIndex
= randInt(5);
290 let queenPos
= positions
[randIndex
];
291 positions
.splice(randIndex
, 1);
293 // Random square for wildebeest
294 randIndex
= randInt(4);
295 let wildebeestPos
= positions
[randIndex
];
296 positions
.splice(randIndex
, 1);
298 let rook1Pos
= positions
[0];
299 let kingPos
= positions
[1];
300 let rook2Pos
= positions
[2];
302 pieces
[c
][rook1Pos
] = "r";
303 pieces
[c
][knight1Pos
] = "n";
304 pieces
[c
][bishop1Pos
] = "b";
305 pieces
[c
][queenPos
] = "q";
306 pieces
[c
][camel1Pos
] = "c";
307 pieces
[c
][camel2Pos
] = "c";
308 pieces
[c
][wildebeestPos
] = "w";
309 pieces
[c
][kingPos
] = "k";
310 pieces
[c
][bishop2Pos
] = "b";
311 pieces
[c
][knight2Pos
] = "n";
312 pieces
[c
][rook2Pos
] = "r";
313 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
316 pieces
["b"].join("") +
317 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
318 pieces
["w"].join("").toUpperCase() +
319 " w 0 " + flags
+ " -"