i += step[0];
j += step[1];
}
- if (i>=0 && i<8 && j>=0 && j<8 && this.canTake([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]));
}
return 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);
+ const firstRank = (color == 'w' ? sizeX-1 : 0);
+ const startRank = (color == "w" ? sizeX-2 : 1);
+ const lastRank = (color == "w" ? 0 : sizeX-1);
if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
{
let step = finalSquares[castleSide][0] < y ? -1 : 1;
for (i=y; i!=finalSquares[castleSide][0]; i+=step)
{
- if (this.isAttacked([x,i], oppCol) || (this.board[x][i] != V.EMPTY &&
+ if (this.isAttacked([x,i], [oppCol]) || (this.board[x][i] != V.EMPTY &&
// NOTE: next check is enough, because of chessboard constraints
(this.getColor(x,i) != c || ![V.KING,V.ROOK].includes(this.getPiece(x,i)))))
{
// Is square x,y attacked by pawns of color c ?
isAttackedByPawn([x,y], colors)
{
+ const [sizeX,sizeY] = VariantRules.size;
for (let c of colors)
{
let pawnShift = (c=="w" ? 1 : -1);
- if (x+pawnShift>=0 && x+pawnShift<8)
+ if (x+pawnShift>=0 && x+pawnShift<sizeX)
{
for (let i of [-1,1])
{
- if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==VariantRules.PAWN
+ if (y+i>=0 && y+i<sizeY && this.getPiece(x+pawnShift,y+i)==VariantRules.PAWN
&& this.getColor(x+pawnShift,y+i)==c)
{
return true;
// Generic method for non-pawn pieces ("sliding or jumping"): is x,y attacked by piece != color ?
isAttackedBySlideNJump([x,y], colors, piece, steps, oneStep)
{
+ const [sizeX,sizeY] = VariantRules.size;
for (let step of steps)
{
let rx = x+step[0], ry = y+step[1];
- while (rx>=0 && rx<8 && ry>=0 && ry<8 && this.board[rx][ry] == VariantRules.EMPTY
- && !oneStep)
+ while (rx>=0 && rx<sizeX && ry>=0 && ry<sizeY
+ && this.board[rx][ry] == VariantRules.EMPTY && !oneStep)
{
rx += step[0];
ry += step[1];
}
- if (rx>=0 && rx<8 && ry>=0 && ry<8 && this.board[rx][ry] != VariantRules.EMPTY
+ 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)))
{
return true;
{
const color = this.turn;
this.play(move);
- let res = this.isAttacked(this.kingPos[color], this.getOppCol(color));
+ let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
this.undo(move);
return res;
}
{
this.play(move);
const color = this.turn; //opponent
- let res = this.isAttacked(this.kingPos[color], this.getOppCol(color))
+ let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate!
: [ ];
this.undo(move);
{
const color = this.turn;
// No valid move: stalemate or checkmate?
- if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
+ if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
return "1/2";
// OK, checkmate
return color == "w" ? "0-1" : "1-0";
return VariantRules.INFINITY;
}
+ static get SEARCH_DEPTH() {
+ return 3; //2 for high branching factor, 4 for small (Loser chess)
+ }
+
// Assumption: at least one legal move
getComputerMove(moves1) //moves1 might be precomputed (Magnetic chess)
{
+ this.shouldReturn = false;
const maxeval = VariantRules.INFINITY;
const color = this.turn;
if (!moves1)
}
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 currentBest = moves1[_.sample(candidates, 1)];
+
// Skip depth 3 if we found a checkmate (or if we are checkmated in 1...)
- if (Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
+ if (VariantRules.SEARCH_DEPTH >= 3
+ && Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
{
// TODO: show current analyzed move for depth 3, allow stopping eval (return moves1[0])
for (let i=0; i<moves1.length; i++)
{
+ if (this.shouldReturn)
+ return currentBest; //depth-2, minimum
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);
+ 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;
- let candidates = [0]; //indices of candidates moves
+ candidates = [0];
for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
candidates.push(j);
// console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));