class PiPo //Piece+Position
{
// o: {piece[p], color[c], posX[x], posY[y]}
class PiPo //Piece+Position
{
// o: {piece[p], color[c], posX[x], posY[y]}
class Move
{
// o: {appear, vanish, [start,] [end,]}
class Move
{
// o: {appear, vanish, [start,] [end,]}
}
initVariables(fen)
{
this.INIT_COL_KING = {'w':-1, 'b':-1};
this.INIT_COL_ROOK = {'w':[-1,-1], 'b':[-1,-1]};
}
initVariables(fen)
{
this.INIT_COL_KING = {'w':-1, 'b':-1};
this.INIT_COL_ROOK = {'w':[-1,-1], 'b':[-1,-1]};
- this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; //respective squares of white and black king
+ this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; //squares of white and black king
const fenParts = fen.split(" ");
const position = fenParts[0].split("/");
for (let i=0; i<position.length; i++)
{
const fenParts = fen.split(" ");
const position = fenParts[0].split("/");
for (let i=0; i<position.length; i++)
{
- 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 epSq = undefined;
- if (fenParts[2] != "-")
- {
- const digits = fenParts[2].split(","); //3,2 ...
- epSq = { x:Number.parseInt(digits[0]), y:Number.parseInt(digits[1]) };
- }
+ const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
}
// Turn diagram fen into double array ["wb","wp","bk",...]
static GetBoard(fen)
{
let rows = fen.split(" ")[0].split("/");
}
// Turn diagram fen into double array ["wb","wp","bk",...]
static GetBoard(fen)
{
let rows = fen.split(" ")[0].split("/");
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
static get size() { return [8,8]; }
// Two next functions return 'undefined' if called on empty square
getColor(i,j) { return this.board[i][j].charAt(0); }
static get size() { return [8,8]; }
// Two next functions return 'undefined' if called on empty square
getColor(i,j) { return this.board[i][j].charAt(0); }
'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<sizeX && j>=0 && j<sizeY && this.canTake([x,y], [i,j]))
+ moves.push(this.getBasicMove([x,y], [i,j]));
- // What are the pawn moves from square x,y considering color "color" ?
- getPotentialPawnMoves(x, y, color)
+ // What are the pawn moves from square x,y ?
+ getPotentialPawnMoves([x,y])
- 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] = V.size;
+ const shift = (color == "w" ? -1 : 1);
+ const firstRank = (color == 'w' ? sizeX-1 : 0);
+ const startRank = (color == "w" ? sizeX-2 : 1);
+ const lastRank = (color == "w" ? 0 : sizeX-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 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))
+ if (y>0 && this.canTake([x,y], [x+shift,y-1])
- moves.push(this.getBasicMove(x, y, x+shift, y-1));
+ 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))
+ if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
- moves.push(this.getBasicMove(x, y, x+shift, y+1));
+ moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
promotionPieces.forEach(p => {
// Normal move
if (this.board[x+shift][y] == V.EMPTY)
let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
promotionPieces.forEach(p => {
// Normal move
if (this.board[x+shift][y] == V.EMPTY)
- moves.push(this.getBasicMove(x, y, x+shift, y, p));
+ moves.push(this.getBasicMove([x,y], [x+shift,y], {c:pawnColor,p:p}));
- if (y>0 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y-1))
+ if (y>0 && this.canTake([x,y], [x+shift,y-1])
- moves.push(this.getBasicMove(x, y, x+shift, y-1, p));
+ moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p}));
- if (y<sizeY-1 && this.canTake(this.getColor(x,y), this.getColor(x+shift,y+1))
+ if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
- moves.push(this.getBasicMove(x, y, x+shift, y+1, p));
+ moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:pawnColor,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]);
- x, y, color, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
+ 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])
+ const c = this.getColor(x,y);
+ const [sizeX,sizeY] = VariantRules.size;
+ if (x != (c=="w" ? sizeX-1 : 0) || y != this.INIT_COL_KING[c])
- const finalSquares = [ [2,3], [6,5] ]; //king, then rook
+ const finalSquares = [ [2,3], [sizeY-2,sizeY-3] ]; //king, then rook
let step = finalSquares[castleSide][0] < y ? -1 : 1;
for (i=y; i!=finalSquares[castleSide][0]; i+=step)
{
let step = finalSquares[castleSide][0] < y ? -1 : 1;
for (i=y; i!=finalSquares[castleSide][0]; i+=step)
{
// NOTE: next check is enough, because of chessboard constraints
(this.getColor(x,i) != c || ![V.KING,V.ROOK].includes(this.getPiece(x,i)))))
{
// NOTE: next check is enough, because of chessboard constraints
(this.getColor(x,i) != c || ![V.KING,V.ROOK].includes(this.getPiece(x,i)))))
{
- return ((color=='w' && this.movesCount%2==0)
- || (color=='b' && this.movesCount%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); });
- var potentialMoves = [];
- let [sizeX,sizeY] = VariantRules.size;
- for (var i=0; i<sizeX; i++)
+ let potentialMoves = [];
+ const [sizeX,sizeY] = VariantRules.size;
+ for (let i=0; i<sizeX; i++)
if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
Array.prototype.push.apply(potentialMoves, this.getPotentialMovesFrom([i,j]));
}
if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
Array.prototype.push.apply(potentialMoves, this.getPotentialMovesFrom([i,j]));
}
// 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);
}
- let [sizeX,sizeY] = VariantRules.size;
- for (var i=0; i<sizeX; i++)
+ const [sizeX,sizeY] = VariantRules.size;
+ for (let i=0; i<sizeX; i++)
{
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));
- // Is square x,y attacked by pawns of color c ?
- isAttackedByPawn([x,y], c)
+ // Is square x,y attacked by 'colors' pawns ?
+ isAttackedByPawn([x,y], colors)
- // Is square x,y attacked by rooks of color c ?
- isAttackedByRook(sq, color)
+ // Is square x,y attacked by 'colors' rooks ?
+ isAttackedByRook(sq, colors)
- // Is square x,y attacked by knights of color c ?
- isAttackedByKnight(sq, color)
+ // Is square x,y attacked by 'colors' knights ?
+ isAttackedByKnight(sq, colors)
- // Is square x,y attacked by bishops of color c ?
- isAttackedByBishop(sq, color)
+ // Is square x,y attacked by 'colors' bishops ?
+ isAttackedByBishop(sq, colors)
- // Is square x,y attacked by queens of color c ?
- isAttackedByQueen(sq, color)
+ // Is square x,y attacked by 'colors' queens ?
+ isAttackedByQueen(sq, colors)
- 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]));
- // Is square x,y attacked by king of color c ?
- isAttackedByKing(sq, color)
+ // Is square x,y attacked by 'colors' king(s) ?
+ isAttackedByKing(sq, colors)
- 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");
- // Generic method for non-pawn pieces ("sliding or jumping"): is x,y attacked by piece != color ?
- isAttackedBySlideNJump([x,y], c,piece,steps,oneStep)
+ // Generic method for non-pawn pieces ("sliding or jumping"):
+ // is x,y attacked by a piece of color in array 'colors' ?
+ isAttackedBySlideNJump([x,y], colors, piece, steps, oneStep)
- if (rx>=0 && rx<8 && ry>=0 && ry<8 && this.board[rx][ry] != VariantRules.EMPTY
- && this.getPiece(rx,ry) == piece && this.getColor(rx,ry) == c)
+ if (rx>=0 && rx<sizeX && ry>=0 && ry<sizeY
+ && this.board[rx][ry] != VariantRules.EMPTY
+ && this.getPiece(rx,ry) == piece && colors.includes(this.getColor(rx,ry)))
- 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!
updateVariables(move)
{
const piece = this.getPiece(move.start.x,move.start.y);
const c = this.getColor(move.start.x,move.start.y);
updateVariables(move)
{
const piece = this.getPiece(move.start.x,move.start.y);
const c = this.getColor(move.start.x,move.start.y);
// Update king position + flags
if (piece == VariantRules.KING && move.appear.length > 0)
{
this.kingPos[c][0] = move.appear[0].x;
this.kingPos[c][1] = move.appear[0].y;
// Update king position + flags
if (piece == VariantRules.KING && move.appear.length > 0)
{
this.kingPos[c][0] = move.appear[0].x;
this.kingPos[c][1] = move.appear[0].y;
- 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;
+ // After move is undo-ed, un-update variables (flags are reset)
+ // TODO: more symmetry, by storing flags increment in move...
+ unupdateVariables(move)
+ {
+ // (Potentially) Reset king position
+ const c = this.getColor(move.start.x,move.start.y);
+ if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING)
+ this.kingPos[c] = [move.start.x, move.start.y];
+ }
+
+ // Hash of position+flags+turn after a move is played (to detect repetitions)
+ getHashState()
+ {
+ return hex_md5(this.getFen() + " " + this.turn);
+ }
+
- // Save flags (for undo)
- move.flags = JSON.stringify(this.flags); //TODO: less costly
- this.updateVariables(move);
+ // DEBUG:
+// if (!this.states) this.states = [];
+// if (!ingame) this.states.push(JSON.stringify(this.board));
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
- this.movesCount--;
-
- if (!!ingame)
- this.moves.pop();
-
- // Update king position, and reset stored/computed flags
- const c = this.getColor(move.start.x,move.start.y);
- if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING)
- this.kingPos[c] = [move.start.x, move.start.y];
-
- this.flags = JSON.parse(move.flags);
+ this.moves.pop();
+ this.unupdateVariables(move);
+ this.parseFlags(JSON.parse(move.flags));
+
+ // DEBUG:
+// if (JSON.stringify(this.board) != this.states[this.states.length-1])
+// debugger;
+// this.states.pop();
- // Check for 3 repetitions
- if (this.moves.length >= 8)
+ if (!this.hashStates)
+ this.hashStates = {};
+ const startIndex =
+ Object.values(this.hashStates).reduce((a,b) => { return a+b; }, 0)
+ // Update this.hashStates with last move (or all moves if continuation)
+ // NOTE: redundant storage, but faster and moderate size
+ for (let i=startIndex; i<this.moves.length; i++)
- // NOTE: crude detection, only moves repetition
- const L = this.moves.length;
- if (_.isEqual(this.moves[L-1], this.moves[L-5]) &&
- _.isEqual(this.moves[L-2], this.moves[L-6]) &&
- _.isEqual(this.moves[L-3], this.moves[L-7]) &&
- _.isEqual(this.moves[L-4], this.moves[L-8]))
- {
- return "1/2 (repetition)";
- }
+ const move = this.moves[i];
+ if (!this.hashStates[move.hash])
+ this.hashStates[move.hash] = 1;
+ else
+ this.hashStates[move.hash]++;
- if (this.atLeastOneMove(color))
- {
- // game not over
+ // Is game over ? And if yes, what is the score ?
+ checkGameOver()
+ {
+ if (this.checkRepetition())
+ return "1/2";
+
+ if (this.atLeastOneMove()) // game not over
- // Rank moves using a min-max at depth 2
- let moves1 = this.getAllValidMoves(color);
+ static get THRESHOLD_MATE() {
+ // At this value or above, the game is over
+ return VariantRules.INFINITY;
+ }
- for (let i=0; i<moves1.length; i++)
+ static get SEARCH_DEPTH() {
+ return 3; //2 for high branching factor, 4 for small (Loser chess)
+ }
+
+ // Assumption: at least one legal move
+ // NOTE: works also for extinction chess because depth is 3...
+ getComputerMove()
+ {
+ const maxeval = VariantRules.INFINITY;
+ const color = this.turn;
+ // Some variants may show a bigger moves list to the human (Switching),
+ // thus the argument "computer" below (which is generally ignored)
+ let moves1 = this.getAllValidMoves("computer");
+
+ // Can I mate in 1 ? (for Magnetic & Extinction)
+ for (let i of _.shuffle(_.range(moves1.length)))
- // Second half-move:
- let moves2 = this.getAllValidMoves(oppCol);
- // If no possible moves AND underCheck, eval2 is correct.
- // If !underCheck, eval2 is 0 (stalemate).
- if (moves2.length == 0 && this.checkGameEnd(oppCol) == "1/2")
- eval2 = 0;
- for (let j=0; j<moves2.length; j++)
- {
- this.play(moves2[j]);
- let evalPos = this.evalPosition();
- if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > eval2))
- eval2 = evalPos;
- this.undo(moves2[j]);
- }
- if ((color=="w" && eval2 > moves1[i].eval) || (color=="b" && eval2 < moves1[i].eval))
- moves1[i].eval = eval2;
+ const finish = (Math.abs(this.evalPosition()) >= VariantRules.THRESHOLD_MATE);
- // 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);
+ let eval2 = undefined;
+ if (this.atLeastOneMove())
+ {
+ eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value
+ // Second half-move:
+ let moves2 = this.getAllValidMoves("computer");
+ for (let j=0; j<moves2.length; j++)
+ {
+ this.play(moves2[j]);
+ let evalPos = undefined;
+ if (this.atLeastOneMove())
+ evalPos = this.evalPosition()
+ else
+ {
+ // Work with scores for Loser variant
+ const score = this.checkGameEnd();
+ evalPos = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
+ }
+ if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > eval2))
+ eval2 = evalPos;
+ this.undo(moves2[j]);
+ }
+ }
+ else
+ {
+ const score = this.checkGameEnd();
+ eval2 = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
+ }
+ if ((color=="w" && eval2 > moves1[i].eval)
+ || (color=="b" && eval2 < moves1[i].eval))
+ {
+ moves1[i].eval = eval2;
+ }
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);
+ // From here, depth >= 3: may take a while, so we control time
+ const timeStart = Date.now();
+
+ // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
+ if (VariantRules.SEARCH_DEPTH >= 3
+ && Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
+ {
+ for (let i=0; i<moves1.length; i++)
+ {
+ if (Date.now()-timeStart >= 5000) //more than 5 seconds
+ return currentBest; //depth 2 at least
+ 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(VariantRules.SEARCH_DEPTH-1, -maxeval, maxeval);
+ this.undo(moves1[i]);
+ }
+ moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
+ }
+ else
+ return currentBest;
- 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));
- const L = this.epSquares.length;
- const epSq = this.epSquares[L-1]===undefined
- ? "-"
- : this.epSquares[L-1].x+","+this.epSquares[L-1].y;
- return this.getBaseFen() + " " + this.getFlagsFen()
- + " " + epSq + " " + this.movesCount;
+ return this.getBaseFen() + " " + this.getFlagsFen();
+ // Complete the usual notation, may be required for de-ambiguification
+ getLongNotation(move)
+ {
+ const startSquare =
+ String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
+ const finalSquare =
+ String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
+ return startSquare + finalSquare; //not encoding move. But short+long is enough
+ }
+
- pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]<br>';
- pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]<br>';
- pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]<br>';
- pgn += '[Fen "' + fenStart + '"]<br>';
+ const opponent = mode=="human" ? "Anonymous" : "Computer";
+ pgn += '[Variant "' + variant + '"]<br>';
+ pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) +
+ '-' + zeroPad(d.getDate()) + '"]<br>';
+ pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]<br>';
+ pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]<br>';
+ pgn += '[FenStart "' + fenStart + '"]<br>';
+ pgn += '[Fen "' + this.getFen() + '"]<br>';