import { ChessRules } from "@/base_rules";
+import { ArrayFun} from "@/utils/array";
+
// NOTE: alternative implementation, probably cleaner = use only 1 board
+// TODO? atLeastOneMove() would be more efficient if rewritten here (less sideBoard computations)
export const VariantRules = class AliceRules extends ChessRules
{
static get ALICE_PIECES()
getSideBoard(mirrorSide)
{
// Build corresponding board from complete board
- let sideBoard = doubleArray(V.size.x, V.size.y, "");
+ let sideBoard = ArrayFun.init(V.size.x, V.size.y, "");
for (let i=0; i<V.size.x; i++)
{
for (let j=0; j<V.size.y; j++)
const pieces = Object.keys(V.ALICE_CODES);
const codes = Object.keys(V.ALICE_PIECES);
const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
+ if (!sideBoard)
+ sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
const color = this.getColor(x,y);
// Search valid moves on sideBoard
- let saveBoard = this.board;
- this.board = sideBoard || this.getSideBoard(mirrorSide);
- let moves = super.getPotentialMovesFrom([x,y])
+ const saveBoard = this.board;
+ this.board = sideBoard[mirrorSide-1];
+ const moves = super.getPotentialMovesFrom([x,y])
.filter(m => {
// Filter out king moves which result in under-check position on
// current board (before mirror traversing)
if (m.appear[0].p == V.KING)
{
this.play(m);
- if (this.underCheck(color))
+ if (this.underCheck(color, sideBoard))
aprioriValid = false;
this.undo(m);
}
this.board = saveBoard;
// Finally filter impossible moves
- let res = moves.filter(m => {
+ const res = moves.filter(m => {
if (m.appear.length == 2) //castle
{
// appear[i] must be an empty square on the other board
return res;
}
- filterValid(moves)
+ filterValid(moves, sideBoard)
{
if (moves.length == 0)
return [];
- let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
+ if (!sideBoard)
+ sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
const color = this.turn;
return moves.filter(m => {
this.playSide(m, sideBoard); //no need to track flags
{
const color = this.turn;
const oppCol = V.GetOppCol(color);
- var potentialMoves = [];
- let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
+ let potentialMoves = [];
+ const sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
for (var i=0; i<V.size.x; i++)
{
for (var j=0; j<V.size.y; j++)
{
if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color)
{
- const mirrorSide =
- Object.keys(V.ALICE_CODES).includes(this.getPiece(i,j))
- ? 1
- : 2;
Array.prototype.push.apply(potentialMoves,
- this.getPotentialMovesFrom([i,j], sideBoard[mirrorSide-1]));
+ this.getPotentialMovesFrom([i,j], sideBoard));
}
}
}
});
}
- underCheck(color, sideBoard) //sideBoard arg always provided
+ // sideBoard: arg containing both boards (see getAllValidMoves())
+ underCheck(color, sideBoard)
{
const kp = this.kingPos[color];
const mirrorSide = (sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2);