-// TODO: use indexedDB instead of localStorage? (more flexible: allow several games)
Vue.component('my-game', {
data: function() {
return {
},
[h('img',
{
- attrs: { "src": '/images/pieces/' + VariantRules.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
+ attrs: { "src": '/images/pieces/' +
+ VariantRules.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
'class': { 'choice-piece': true, 'board': true },
on: { "click": e => { this.play(m); this.choices=[]; } },
})
{
'class': {
'piece': true,
- 'ghost': !!this.selectedPiece && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
+ 'ghost': !!this.selectedPiece
+ && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
},
attrs: {
- src: "/images/pieces/" + VariantRules.getPpath(this.vr.board[ci][cj]) + ".svg",
+ src: "/images/pieces/" +
+ VariantRules.getPpath(this.vr.board[ci][cj]) + ".svg",
},
}
)
this.newGame("computer");
},
newGame: function(mode, fenInit, color, oppId, moves, continuation) {
- //const fen = "qrbnkbrn/pppppppp/8/8/8/8/PPPPPPPP/BNNBRKRQ 1111";//fenInit || VariantRules.GenRandInitFen();
+ //const fen = "1n2T1n0/p2pO2p/1s1k1s2/8/3S2p1/2U2cO1/P3PuPP/3K1BR1 0100";
const fen = fenInit || VariantRules.GenRandInitFen();
console.log(fen); //DEBUG
this.score = "*";
}
},
playComputerMove: function() {
+ const timeStart = Date.now();
const compMove = this.vr.getComputerMove();
- // HACK: avoid selecting elements before they appear on page:
- setTimeout(() => this.play(compMove, "animate"), 500);
+ // (first move) HACK: avoid selecting elements before they appear on page:
+ const delay = Math.max(500-(Date.now()-timeStart), 0);
+ setTimeout(() => this.play(compMove, "animate"), delay);
},
// Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y.
getSquareId: function(o) {
+// NOTE: alternative implementation, probably cleaner = use only 1 board
class AliceRules extends ChessRules
{
static get ALICE_PIECES()
}
// NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
- // --> Should be OK as is.
getPotentialMovesFrom([x,y], sideBoard)
{
const pieces = Object.keys(VariantRules.ALICE_CODES);
return res;
}
- // NOTE: alternative implementation, recompute sideBoard's in this function
- filterValid(moves, sideBoard)
+ filterValid(moves)
{
if (moves.length == 0)
return [];
- const pieces = Object.keys(VariantRules.ALICE_CODES);
- return moves.filter(m => {
- // WARNING: for underCheck(), we need the sideBoard of the arrival world !
- const mirrorSide = (pieces.includes(this.getPiece(m.start.x,m.start.y)) ? 2 : 1);
- return !this.underCheck(m, !!sideBoard ? sideBoard[mirrorSide-1] : null);
- });
+ let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
+ return moves.filter(m => { return !this.underCheck(m, sideBoard); });
}
getAllValidMoves()
return this.filterValid(potentialMoves, sideBoard);
}
- underCheck(move, sideBoard)
+ // Play on sideboards [TODO: only one sideBoard required]
+ playSide(move, sideBoard)
+ {
+ const pieces = Object.keys(VariantRules.ALICE_CODES);
+ move.vanish.forEach(psq => {
+ const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
+ sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
+ });
+ move.appear.forEach(psq => {
+ const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
+ const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
+ sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
+ if (piece == VariantRules.KING)
+ this.kingPos[psq.c] = [psq.x,psq.y];
+ });
+ }
+
+ // Undo on sideboards
+ undoSide(move, sideBoard)
{
- const color = this.turn;
- this.play(move);
const pieces = Object.keys(VariantRules.ALICE_CODES);
+ move.appear.forEach(psq => {
+ const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
+ sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
+ });
+ move.vanish.forEach(psq => {
+ const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
+ const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
+ sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
+ if (piece == VariantRules.KING)
+ this.kingPos[psq.c] = [psq.x,psq.y];
+ });
+ }
+
+ underCheck(move, sideBoard) //sideBoard arg always provided
+ {
+ const color = this.turn;
+ this.playSide(move, sideBoard); //no need to track flags
const kp = this.kingPos[color];
- const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
+ const mirrorSide = sideBoard[0][kp[0]][kp[1]] != VariantRules.EMPTY ? 1 : 2;
let saveBoard = this.board;
- this.board = sideBoard || this.getSideBoard(mirrorSide);
- let res = this.isAttacked(this.kingPos[color], this.getOppCol(color));
+ this.board = sideBoard[mirrorSide-1];
+ let res = this.isAttacked(kp, this.getOppCol(color));
this.board = saveBoard;
- this.undo(move);
+ this.undoSide(move, sideBoard);
return res;
}