Added castling

This commit is contained in:
Sage Vaillancourt 2020-12-30 22:50:07 -05:00
parent 4d927e1866
commit bfbb9ef404
1 changed files with 37 additions and 5 deletions

View File

@ -285,18 +285,38 @@ class Board extends React.Component {
[[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]] [[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]]
.forEach(delta => tryAddMove(x + delta[0], y + delta[1])); .forEach(delta => tryAddMove(x + delta[0], y + delta[1]));
if (piece.moves === 0) { if (piece.moves === 0) {
const [x, y] = this.getXandY(this.findIndex(piece)); const kingIndex = this.findIndex(piece);
const [x, y] = this.getXandY(kingIndex);
let leftRook = this.pieceAt(0, y); let leftRook = this.pieceAt(0, y);
if(isRook(leftRook) && leftRook.moves === 0) { if(isRook(leftRook) && leftRook.moves === 0) {
// Check if between space puts king in check
// Check if spaces between rook and king are empty // Check if spaces between rook and king are empty
// add move(x: x - 2, y, castle: [x, y]) if(this.pieceAt(1, y) == null &&
this.pieceAt(2, y) == null &&
this.pieceAt(3, y) == null) {
// Check if between space puts king in check
let board = this.clone();
board.state.squares[board.getIndex(x - 1, y)] = piece;
board.state.squares[kingIndex] = null;
if(board.inCheck(piece) == null) {
moves.push({x: x - 2, y, castle: [x - 1, y]});
}
}
} }
let rightRook = this.pieceAt(7, y); let rightRook = this.pieceAt(7, y);
if(isRook(rightRook) && rightRook.moves === 0) { if(isRook(rightRook) && rightRook.moves === 0) {
// Check if between space puts king in check
// Check if spaces between rook and king are empty // Check if spaces between rook and king are empty
// add move(x: x + 2, y, castle: [x, y]) if(this.pieceAt(5, y) == null &&
this.pieceAt(6, y) == null) {
// Check if between space puts king in check
let board = this.clone();
board.state.squares[board.getIndex(x + 1, y)] = piece;
board.state.squares[kingIndex] = null;
if(board.inCheck(piece) == null) {
moves.push({x: x + 2, y, castle: [x + 1, y]});
}
}
} }
} }
} }
@ -411,6 +431,18 @@ class Board extends React.Component {
if (move.passant) { if (move.passant) {
squares[this.getIndex(move.passant.x, move.passant.y)] = null; squares[this.getIndex(move.passant.x, move.passant.y)] = null;
} }
if (move.castle) {
// .castle holds the position where the rook should end up
// King moved left
const rookX = move.castle[0] > move.x ? 0 : 7;
console.log("Replace ");
console.log(move.castle);
console.log("With ");
console.log([rookX, move.castle[1]]);
squares[this.getIndex(move.castle[0], move.castle[1])] =
squares[this.getIndex(rookX, move.castle[1])];
squares[this.getIndex(rookX, move.castle[1])] = null;
}
// Remove existing passantable states // Remove existing passantable states
squares.forEach(square => { squares.forEach(square => {
if (square) { if (square) {