this.moves = moves;
// Use fen string to initialize variables, flags and board
this.board = VariantRules.GetBoard(fen);
this.moves = moves;
// Use fen string to initialize variables, flags and board
this.board = VariantRules.GetBoard(fen);
- this.kingPos['b'] = [i,j];
- this.INIT_COL_KING['b'] = j;
+ this.kingPos['b'] = [i,k];
+ this.INIT_COL_KING['b'] = k;
- this.kingPos['w'] = [i,j];
- this.INIT_COL_KING['w'] = j;
+ this.kingPos['w'] = [i,k];
+ this.INIT_COL_KING['w'] = k;
let board = doubleArray(sizeX, sizeY, "");
for (let i=0; i<rows.length; i++)
{
let board = doubleArray(sizeX, sizeY, "");
for (let i=0; i<rows.length; i++)
{
{
// white a-castle, h-castle, black a-castle, h-castle
{
// white a-castle, h-castle, black a-castle, h-castle
- let flags = {'w': new Array(2), 'b': new Array(2)};
- let fenFlags = fen.split(" ")[1]; //flags right after position
+ this.castleFlags = {'w': new Array(2), 'b': new Array(2)};
+ let flags = fen.split(" ")[1]; //flags right after position
'r': [ [-1,0],[1,0],[0,-1],[0,1] ],
'n': [ [-1,-2],[-1,2],[1,-2],[1,2],[-2,-1],[-2,1],[2,-1],[2,1] ],
'b': [ [-1,-1],[-1,1],[1,-1],[1,1] ],
'r': [ [-1,0],[1,0],[0,-1],[0,1] ],
'n': [ [-1,-2],[-1,2],[1,-2],[1,2],[-2,-1],[-2,1],[2,-1],[2,1] ],
'b': [ [-1,-1],[-1,1],[1,-1],[1,1] ],
- 'q': [ [-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1] ]
- // can color1 take color2?
- canTake(color1, color2)
+ // can thing on square1 take thing on square2
+ canTake([x1,y1], [x2,y2])
// All possible moves from selected square (assumption: color is OK)
getPotentialMovesFrom([x,y])
{
// All possible moves from selected square (assumption: color is OK)
getPotentialMovesFrom([x,y])
{
- getBasicMove(sx, sy, ex, ey, tr)
+ getBasicMove([sx,sy], [ex,ey], tr)
- c: this.getColor(sx,sy),
- p: !!tr ? tr : this.getPiece(sx,sy)
+ c: !!tr ? tr.c : this.getColor(sx,sy),
+ p: !!tr ? tr.p : this.getPiece(sx,sy)
- getSlideNJumpMoves(x, y, color, steps, oneStep)
+ getSlideNJumpMoves([x,y], steps, oneStep)
- var moves = [];
- let [sizeX,sizeY] = VariantRules.size;
+ const color = this.getColor(x,y);
+ let moves = [];
+ const [sizeX,sizeY] = VariantRules.size;
- moves.push(this.getBasicMove(x, y, i, j));
+ moves.push(this.getBasicMove([x,y], [i,j]));
- if (i>=0 && i<8 && j>=0 && j<8 && this.canTake(color, this.getColor(i,j)))
- moves.push(this.getBasicMove(x, y, i, j));
+ if (i>=0 && i<8 && j>=0 && j<8 && this.canTake([x,y], [i,j]))
+ moves.push(this.getBasicMove([x,y], [i,j]));
- var moves = [];
- var V = VariantRules;
- let [sizeX,sizeY] = VariantRules.size;
- let shift = (color == "w" ? -1 : 1);
- let startRank = (color == "w" ? sizeY-2 : 1);
- let lastRank = (color == "w" ? 0 : sizeY-1);
+ const color = this.turn;
+ let moves = [];
+ const V = VariantRules;
+ const [sizeX,sizeY] = VariantRules.size;
+ const shift = (color == "w" ? -1 : 1);
+ const firstRank = (color == 'w' ? sizeY-1 : 0);
+ const startRank = (color == "w" ? sizeY-2 : 1);
+ const lastRank = (color == "w" ? 0 : sizeY-1);
- moves.push(this.getBasicMove(x, y, x+shift, y));
- if (x==startRank && this.board[x+2*shift][y] == V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y]));
+ // Next condition because variants with pawns on 1st rank generally allow them to jump
+ if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY)
- moves.push(this.getBasicMove(x, y, x+2*shift, y));
+ moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
- if (y>0 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y-1))
- && this.board[x+shift][y-1] != V.EMPTY)
- {
- moves.push(this.getBasicMove(x, y, x+shift, y-1));
- }
- if (y<sizeY-1 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y+1))
- && this.board[x+shift][y+1] != V.EMPTY)
- {
- moves.push(this.getBasicMove(x, y, x+shift, y+1));
- }
+ if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
+ if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
- moves.push(this.getBasicMove(x, y, x+shift, y, p));
+ moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
- if (y>0 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y-1))
- && this.board[x+shift][y-1] != V.EMPTY)
- {
- moves.push(this.getBasicMove(x, y, x+shift, y-1, p));
- }
- if (y<sizeY-1 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y+1))
- && this.board[x+shift][y+1] != V.EMPTY)
- {
- moves.push(this.getBasicMove(x, y, x+shift, y+1, p));
- }
+ if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
+ if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
+ moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1)
{
let epStep = epSquare.y - y;
if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1)
{
let epStep = epSquare.y - y;
- var enpassantMove = this.getBasicMove(x, y, x+shift, y+epStep);
+ var enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
- return this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.ROOK]);
+ return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.ROOK]);
- return this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
+ return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
- return this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.BISHOP]);
+ return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.BISHOP]);
- return this.getSlideNJumpMoves(
- x, y, color, VariantRules.steps[VariantRules.QUEEN]);
+ const V = VariantRules;
+ return this.getSlideNJumpMoves(sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
- var moves = this.getSlideNJumpMoves(x, y, c,
- VariantRules.steps[VariantRules.QUEEN], "oneStep");
-
- return moves.concat(this.getCastleMoves(x,y,c));
+ let moves = this.getSlideNJumpMoves(sq,
+ V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
+ return moves.concat(this.getCastleMoves(sq));
if (x != (c=="w" ? 7 : 0) || y != this.INIT_COL_KING[c])
return []; //x isn't first rank, or king has moved (shortcut)
if (x != (c=="w" ? 7 : 0) || y != this.INIT_COL_KING[c])
return []; //x isn't first rank, or king has moved (shortcut)
- return ((color=='w' && this.moves.length%2==0)
- || (color=='b' && this.moves.length%2==1))
- && this.getColor(sq[0], sq[1]) == color;
+ return ((side=='w' && this.moves.length%2==0) || (side=='b' && this.moves.length%2==1))
+ && this.getColor(x,y) == side;
- let color = this.getColor( moves[0].start.x, moves[0].start.y );
- return moves.filter(m => {
- return !this.underCheck(m, color);
- });
+ return moves.filter(m => { return !this.underCheck(m); });
const oppCol = this.getOppCol(color);
var potentialMoves = [];
let [sizeX,sizeY] = VariantRules.size;
const oppCol = this.getOppCol(color);
var potentialMoves = [];
let [sizeX,sizeY] = VariantRules.size;
// No: if happen on last 1/2 move, could lead to forbidden moves, wrong evals
return this.filterValid(potentialMoves);
}
// No: if happen on last 1/2 move, could lead to forbidden moves, wrong evals
return this.filterValid(potentialMoves);
}
const oppCol = this.getOppCol(color);
let [sizeX,sizeY] = VariantRules.size;
const oppCol = this.getOppCol(color);
let [sizeX,sizeY] = VariantRules.size;
{
if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
{
const moves = this.getPotentialMovesFrom([i,j]);
if (moves.length > 0)
{
{
if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
{
const moves = this.getPotentialMovesFrom([i,j]);
if (moves.length > 0)
{
- return (this.isAttackedByPawn(sq, color)
- || this.isAttackedByRook(sq, color)
- || this.isAttackedByKnight(sq, color)
- || this.isAttackedByBishop(sq, color)
- || this.isAttackedByQueen(sq, color)
- || this.isAttackedByKing(sq, color));
+ return (this.isAttackedByPawn(sq, colors)
+ || this.isAttackedByRook(sq, colors)
+ || this.isAttackedByKnight(sq, colors)
+ || this.isAttackedByBishop(sq, colors)
+ || this.isAttackedByQueen(sq, colors)
+ || this.isAttackedByKing(sq, colors));
VariantRules.ROOK, VariantRules.steps[VariantRules.ROOK]);
}
// Is square x,y attacked by knights of color c ?
VariantRules.ROOK, VariantRules.steps[VariantRules.ROOK]);
}
// Is square x,y attacked by knights of color c ?
VariantRules.KNIGHT, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
}
// Is square x,y attacked by bishops of color c ?
VariantRules.KNIGHT, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
}
// Is square x,y attacked by bishops of color c ?
VariantRules.BISHOP, VariantRules.steps[VariantRules.BISHOP]);
}
// Is square x,y attacked by queens of color c ?
VariantRules.BISHOP, VariantRules.steps[VariantRules.BISHOP]);
}
// Is square x,y attacked by queens of color c ?
- return this.isAttackedBySlideNJump(sq, color,
- VariantRules.QUEEN, VariantRules.steps[VariantRules.QUEEN]);
+ const V = VariantRules;
+ return this.isAttackedBySlideNJump(sq, colors, V.QUEEN,
+ V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
- return this.isAttackedBySlideNJump(sq, color,
- VariantRules.KING, VariantRules.steps[VariantRules.QUEEN], "oneStep");
+ const V = VariantRules;
+ return this.isAttackedBySlideNJump(sq, colors, V.KING,
+ V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
- isAttackedBySlideNJump([x,y], c,piece,steps,oneStep)
+ isAttackedBySlideNJump([x,y], colors, piece, steps, oneStep)
- let res = this.isAttacked(this.kingPos[c], this.getOppCol(c))
- ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ] //need to duplicate!
+ const color = this.turn; //opponent
+ let res = this.isAttacked(this.kingPos[color], this.getOppCol(color))
+ ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate!
- const flagIdx = move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1;
- this.flags[c][flagIdx] = false;
+ const flagIdx = (move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1);
+ this.castleFlags[c][flagIdx] = false;
- const flagIdx = move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1;
- this.flags[oppCol][flagIdx] = false;
+ const flagIdx = (move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1);
+ this.castleFlags[oppCol][flagIdx] = false;
this.updateVariables(move);
this.moves.push(move);
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
}
this.updateVariables(move);
this.moves.push(move);
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
}
{
VariantRules.UndoOnBoard(this.board, move);
this.epSquares.pop();
this.moves.pop();
this.unupdateVariables(move);
{
VariantRules.UndoOnBoard(this.board, move);
this.epSquares.pop();
this.moves.pop();
this.unupdateVariables(move);
_.isEqual(this.moves[L-3], this.moves[L-7]) &&
_.isEqual(this.moves[L-4], this.moves[L-8]))
{
_.isEqual(this.moves[L-3], this.moves[L-7]) &&
_.isEqual(this.moves[L-4], this.moves[L-8]))
{
// No valid move: stalemate or checkmate?
if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
return "1/2";
// No valid move: stalemate or checkmate?
if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
return "1/2";
+ static get INFINITY() {
+ return 9999; //"checkmate" (unreachable eval)
+ }
+
+ static get THRESHOLD_MATE() {
+ // At this value or above, the game is over
+ return VariantRules.INFINITY;
+ }
+
- moves1[i].eval = (color=="w" ? -1 : 1) * 1000; //very low, I'm checkmated
- let eval2 = (color=="w" ? 1 : -1) * 1000; //initialized with very high (checkmate) value
+ moves1[i].eval = (color=="w" ? -1 : 1) * maxeval; //very low, I'm checkmated
+ let eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value
// If no possible moves AND underCheck, eval2 is correct.
// If !underCheck, eval2 is 0 (stalemate).
// If no possible moves AND underCheck, eval2 is correct.
// If !underCheck, eval2 is 0 (stalemate).
- // TODO: show current analyzed move for depth 3, allow stopping eval (return moves1[0])
- for (let i=0; i<moves1.length; i++)
+ // Skip depth 3 if we found a checkmate (or if we are checkmated in 1...)
+ if (Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
- this.play(moves1[i]);
- // 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
- moves1[i].eval = 0.1*moves1[i].eval + this.alphabeta(oppCol, color, 2, -1000, 1000);
- this.undo(moves1[i]);
+ // TODO: show current analyzed move for depth 3, allow stopping eval (return moves1[0])
+ for (let i=0; i<moves1.length; i++)
+ {
+ this.play(moves1[i]);
+ // 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
+ moves1[i].eval = 0.1*moves1[i].eval + this.alphabeta(2, -maxeval, maxeval);
+ this.undo(moves1[i]);
+ }
+ moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
let candidates = [0]; //indices of candidates moves
for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
candidates.push(j);
let candidates = [0]; //indices of candidates moves
for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
candidates.push(j);
- alphabeta(color, oppCol, depth, alpha, beta)
+ alphabeta(depth, alpha, beta)
- v = Math.max(v, this.alphabeta(oppCol, color, depth-1, alpha, beta));
+ v = Math.max(v, this.alphabeta(depth-1, alpha, beta));
- v = Math.min(v, this.alphabeta(oppCol, color, depth-1, alpha, beta));
+ v = Math.min(v, this.alphabeta(depth-1, alpha, beta));
- pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]<br>';
- pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]<br>';
- pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]<br>';
+ const opponent = mode=="human" ? "Anonymous" : "Computer";
+ pgn += '[Variant "' + variant + '"]<br>';
+ pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + '"]<br>';
+ pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>';
+ pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>';