return (this.turn == side && this.getColor(x,y) == side);
}
- // On which squares is opponent under check after our move ? (for interface)
- getCheckSquares(move)
+ // On which squares is color under check ? (for interface)
+ getCheckSquares(color)
{
- this.play(move);
- const color = this.turn; //opponent
- let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
+ return this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
: [];
- this.undo(move);
- return res;
}
/////////////
turn: fenParts[1],
};
let nextIdx = 2;
- if (V.hasFlags)
+ if (V.HasFlags)
Object.assign(res, {flags: fenParts[nextIdx++]});
if (V.HasEnpassant)
Object.assign(res, {enpassant: fenParts[nextIdx]});
position += emptyCount;
emptyCount = 0;
}
- fen += V.board2fen(this.board[i][j]);
+ position += V.board2fen(this.board[i][j]);
}
}
if (emptyCount > 0)
// Set flags and enpassant:
const parsedFen = V.ParseFen(fen);
if (V.HasFlags)
- this.setFlags(fenParsed.flags);
+ this.setFlags(parsedFen.flags);
if (V.HasEnpassant)
{
const epSq = parsedFen.enpassant != "-"
const color = this.turn;
let moves = [];
const [sizeX,sizeY] = [V.size.x,V.size.y];
- const shift = (color == "w" ? -1 : 1);
+ const shiftX = (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);
+ const pawnColor = this.getColor(x,y); //can be different for checkered
- if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
+ if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true
{
- // Normal moves
- if (this.board[x+shift][y] == V.EMPTY)
+ const finalPieces = x + shiftX == lastRank
+ ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
+ : [V.PAWN]
+ // One square forward
+ if (this.board[x+shiftX][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)
+ for (let piece of finalPieces)
+ {
+ moves.push(this.getBasicMove([x,y], [x+shiftX,y],
+ {c:pawnColor,p:piece}));
+ }
+ // Next condition because pawns on 1st rank can generally jump
+ if ([startRank,firstRank].includes(x)
+ && this.board[x+2*shiftX][y] == V.EMPTY)
{
// Two squares jump
- moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
+ moves.push(this.getBasicMove([x,y], [x+2*shiftX,y]));
}
}
// Captures
- if (y>0 && this.board[x+shift][y-1] != V.EMPTY
- && this.canTake([x,y], [x+shift,y-1]))
+ for (let shiftY of [-1,1])
{
- moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
- }
- if (y<sizeY-1 && this.board[x+shift][y+1] != V.EMPTY
- && this.canTake([x,y], [x+shift,y+1]))
- {
- moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
- }
- }
-
- if (x+shift == lastRank)
- {
- // Promotion
- const pawnColor = this.getColor(x,y); //can be different for checkered
- 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], {c:pawnColor,p:p}));
- // Captures
- if (y>0 && this.board[x+shift][y-1] != V.EMPTY
- && this.canTake([x,y], [x+shift,y-1]))
- {
- moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p}));
- }
- if (y<sizeY-1 && this.board[x+shift][y+1] != V.EMPTY
- && this.canTake([x,y], [x+shift,y+1]))
+ if (y + shiftY >= 0 && y + shiftY < sizeY
+ && this.board[x+shiftX][y+shiftY] != V.EMPTY
+ && this.canTake([x,y], [x+shiftX,y+shiftY]))
{
- moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:pawnColor,p:p}));
+ for (let piece of finalPieces)
+ {
+ moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
+ {c:pawnColor,p:piece}));
+ }
}
- });
+ }
}
- // En passant
- const Lep = this.epSquares.length;
- const epSquare = this.epSquares[Lep-1]; //always at least one element
- if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1)
+ if (V.HasEnpassant)
{
- const epStep = epSquare.y - y;
- let enpassantMove = this.getBasicMove([x,y], [x+shift,y+epStep]);
- enpassantMove.vanish.push({
- x: x,
- y: y+epStep,
- p: 'p',
- c: this.getColor(x,y+epStep)
- });
- moves.push(enpassantMove);
+ // En passant
+ const Lep = this.epSquares.length;
+ const epSquare = this.epSquares[Lep-1]; //always at least one element
+ if (!!epSquare && epSquare.x == x+shiftX && Math.abs(epSquare.y - y) == 1)
+ {
+ let enpassantMove = this.getBasicMove([x,y], [epSquare.x,epSquare.y]);
+ enpassantMove.vanish.push({
+ x: x,
+ y: epSquare.y,
+ p: 'p',
+ c: this.getColor(x,epSquare.y)
+ });
+ moves.push(enpassantMove);
+ }
}
return moves;
////////////////////
// MOVES VALIDATION
+ // For the interface: possible moves for the current turn from square sq
getPossibleMovesFrom(sq)
{
- // Assuming color is right (already checked)
return this.filterValid( this.getPotentialMovesFrom(sq) );
}
{
if (moves.length == 0)
return [];
- return moves.filter(m => { return !this.underCheck(m); });
+ const color = this.turn;
+ return moves.filter(m => {
+ this.play(m);
+ const res = !this.underCheck(color);
+ this.undo(m);
+ return res;
+ });
}
// Search for all valid moves considering current turn (for engine and game end)
return false;
}
- // Is current player under check after his move ?
- underCheck(move)
+ // Is color under check after his move ?
+ underCheck(color)
{
- const color = this.turn;
- this.play(move);
- let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
- this.undo(move);
- return res;
+ return this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
}
/////////////////
// Before move is played, update variables + flags
updateVariables(move)
{
- const piece = this.getPiece(move.start.x,move.start.y);
- const c = this.turn;
+ const piece = move.vanish[0].p;
+ const c = move.vanish[0].c;
const firstRank = (c == "w" ? V.size.x-1 : 0);
// Update king position + flags
play(move, ingame)
{
// DEBUG:
+// console.log("DO");
// if (!this.states) this.states = [];
// if (!ingame) this.states.push(this.getFen());
if (V.HasFlags)
move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo)
- this.updateVariables(move);
- this.moves.push(move);
if (V.HasEnpassant)
this.epSquares.push( this.getEpSquare(move) );
- this.turn = this.getOppCol(this.turn);
V.PlayOnBoard(this.board, move);
+ this.turn = this.getOppCol(this.turn);
+ this.moves.push(move);
+ this.updateVariables(move);
if (!!ingame)
{
// Hash of current game state *after move*, to detect repetitions
- move.hash = hex_md5(this.getFen();
+ move.hash = hex_md5(this.getFen());
}
}
undo(move)
{
- V.UndoOnBoard(this.board, move);
- this.turn = this.getOppCol(this.turn);
if (V.HasEnpassant)
this.epSquares.pop();
- this.moves.pop();
- this.unupdateVariables(move);
if (V.HasFlags)
this.disaggregateFlags(JSON.parse(move.flags));
+ V.UndoOnBoard(this.board, move);
+ this.turn = this.getOppCol(this.turn);
+ this.moves.pop();
+ this.unupdateVariables(move);
// DEBUG:
+// console.log("UNDO "+this.getNotation(move));
// if (this.getFen() != this.states[this.states.length-1])
// debugger;
// this.states.pop();