Correct checkmate-checking. Pawns become queens.

Began adding en passant framework. Pawns should have the option of
becoming knights, too.
This commit is contained in:
Sage Vaillancourt 2020-12-29 22:44:48 -05:00
parent e59e1a68f5
commit 51efbdfd4e
1 changed files with 45 additions and 33 deletions

View File

@ -88,6 +88,9 @@ class Piece {
this.color = color;
this.type = type;
}
setType(type) {
this.type = type;
}
}
class Board extends React.Component {
@ -100,6 +103,7 @@ class Board extends React.Component {
heldPiece: null,
},
};
this.passantable = false;
}
setHand(hand) {
@ -111,7 +115,6 @@ class Board extends React.Component {
}
reset() {
console.log("RESET");
let whiteRow = [
new Piece(WHITE, ROOK),
new Piece(WHITE, KNIGHT),
@ -132,11 +135,15 @@ class Board extends React.Component {
new Piece(BLACK, KNIGHT),
new Piece(BLACK, ROOK)
];
let squares = whiteRow.concat(
Array(8).fill(new Piece(WHITE, PAWN)),
Array(32).fill(null),
Array(8).fill(new Piece(BLACK, PAWN)),
blackRow);
let squares = whiteRow;
for(var i = 0; i < 8; i++) {
squares.push(new Piece(WHITE, PAWN));
}
squares = squares.concat(Array(32).fill(null));
for(i = 0; i < 8; i++) {
squares.push(new Piece(BLACK, PAWN));
}
squares = squares.concat(blackRow);
return squares;
}
@ -149,7 +156,7 @@ class Board extends React.Component {
pieceAt(x, y) {
let i = x + (y * 8);
if (i < 64 && x < 8 && y < 8) {
if (i < 64 && x < 8 && y < 8 && x >= 0 && y >= 0) {
return this.state.squares[x + (y * 8)];
} else {
return null;
@ -181,7 +188,7 @@ class Board extends React.Component {
var i;
var moves = [];
let tryAddMove = (x, y) => {
if (this.pieceAt(x, y) == null) {
if (x >= 0 && x < 8 && y >= 0 && y < 8 && this.pieceAt(x, y) == null) {
moves.push([x, y]);
// Keep searching
return 0;
@ -197,14 +204,25 @@ class Board extends React.Component {
let shift = pieceIsBlack ? -1 : 1;
let startLine = pieceIsBlack ? 6 : 1;
if (this.passantable) {
let left = this.pieceAt(x - 1, y);
let right = this.pieceAt(x + 1, y);
if(left != null && left.color !== piece.color) {
moves.push([x - 1, y + shift]/*.passant=[x - 1, y]*/)
}
if(right != null && right.color !== piece.color) {
moves.push([x + 1, y + shift]/*.passant=[x + 1, y]*/)
}
}
if (this.pieceAt(x, y + shift) == null) {
moves.push([x, y + shift]);
if (y === startLine && this.pieceAt(x, y + (shift * 2)) == null) {
moves.push([x, y + (shift * 2)]);
moves.push([x, y + (shift * 2)]/*.passantable=true*/);
}
}
[x + 1, x - 1].forEach(x => {
if (this.isEnemyOf(piece, x, y + shift)) {
if (x >= 0 && x < 8 && this.isEnemyOf(piece, x, y + shift)) {
moves.push([x, y + shift]);
}
});
@ -280,11 +298,9 @@ class Board extends React.Component {
}
findIndex(piece) {
console.log("founding");
for(var i = 0; i < this.squareCount(); i++) {
let check = this.state.squares[i];
if(check && check.type === piece.type && check.color === piece.color) {
console.log("found");
return i;
}
}
@ -339,29 +355,22 @@ class Board extends React.Component {
checkmate() {
let checkedKing = this.whoInCheck();
if (checkedKing != null) {
// For piece in board
// If piece is on the checked team,
// Copy the board
// Make one of the piece's moves on the board
// Return false if the king is no longer in check
// For each square
console.log("Square count: " + this.squareCount());
for(var i = 0; i < this.squareCount(); i++) {
let piece = this.squareAt(i);
// If that piece is on the checked team
if (piece != null && isBlack(piece) === isBlack(checkedKing)) {
console.log("Trying moves for " + Images[piece])
// Copy the board
var board = new Board();
board.state.squares = this.state.squares.slice();
// For each move of the above piece
let moves = board.getValidMoves(i)
for(var j = 0; j < moves.length; j++) {
let [from, to] = moves[j];
board.state.squares[to] = board.state.squares[from];
board.state.squares[from] = null;
if (board.inCheck(checkedKing) !== checkedKing) {
let [x, y] = moves[j];
board.state.squares[x + (y * 8)] = board.state.squares[i];
board.state.squares[i] = null;
let check = board.inCheck(checkedKing);
if (check == null || check.color !== checkedKing.color) {
return false;
}
}
@ -383,19 +392,14 @@ class Board extends React.Component {
isValidMove(source, dest) {
let [destX, destY] = this.getXandY(dest);
let validMoves = this.getValidMoves(source);
if (validMoves == null) {
return false;
}
for (var move of this.getValidMoves(source)) {
let [x, y] = move;
if (destX === x && destY === y) {
return true;
return move;
}
}
return false;
return null;
}
isHoldingPiece() {
@ -408,9 +412,17 @@ class Board extends React.Component {
makeMove(from, to) {
const squares = this.state.squares.slice();
if (this.isValidMove(from, to)) {
let move = this.isValidMove(from, to)
if (move) {
if (move.passant != null) {
squares[move.passant[0] + (move.passant[1] * 8)] = null;
}
let y = this.getXandY(to)[1];
squares[to] = squares[from];
squares[from] = null;
if (squares[to].type === PAWN && (y === 0 || y === 7)) {
squares[to].setType(QUEEN);
}
this.setState({
squares: squares,
blackIsNext: !this.state.blackIsNext,
@ -481,7 +493,7 @@ class Board extends React.Component {
var namedPlayer = isCheckmate ?
!this.state.blackIsNext : this.state.blackIsNext
let color = namedPlayer ? 'Black' : 'White';
const status = this.checkmate() ? "Checkmate! " + color + " Wins!" :
const status = isCheckmate ? "Checkmate! " + color + " Wins!" :
checkMsg + color + "'s Turn";
var texttext =