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