- // Undo on sideboards
- undoSide(move, sideBoard)
- {
- const pieces = Object.keys(V.ALICE_CODES);
- move.appear.forEach(psq => {
- const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
- sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY;
- });
- move.vanish.forEach(psq => {
- const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
- const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]);
- sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
- if (piece == V.KING)
- this.kingPos[psq.c] = [psq.x,psq.y];
- });
- }
+ filterValid(moves, sideBoard) {
+ if (moves.length == 0) return [];
+ 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 res = !this.underCheck(color, sideBoard);
+ this.undoSide(m, sideBoard);
+ return res;
+ });
+ }
+
+ getAllValidMoves() {
+ const color = this.turn;
+ 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) {
+ Array.prototype.push.apply(
+ potentialMoves,
+ this.getPotentialMovesFrom([i, j], sideBoard)
+ );
+ }
+ }
+ }
+ return this.filterValid(potentialMoves, sideBoard);
+ }
+
+ // Play on sideboards [TODO: only one sideBoard required]
+ playSide(move, sideBoard) {
+ const pieces = Object.keys(V.ALICE_CODES);
+ move.vanish.forEach(psq => {
+ const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
+ sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
+ });
+ move.appear.forEach(psq => {
+ const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
+ const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
+ sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
+ if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
+ });
+ }
+
+ // Undo on sideboards
+ undoSide(move, sideBoard) {
+ const pieces = Object.keys(V.ALICE_CODES);
+ move.appear.forEach(psq => {
+ const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
+ sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
+ });
+ move.vanish.forEach(psq => {
+ const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
+ const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
+ sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
+ if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
+ });
+ }