2020-12-27 20:11:50 -05:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import './index.css';
|
|
|
|
|
2020-12-29 16:38:52 -05:00
|
|
|
const BLACK = 0;
|
|
|
|
const WHITE = 1;
|
|
|
|
|
|
|
|
const PAWN = 0;
|
|
|
|
const ROOK = 1;
|
|
|
|
const KNIGHT = 2;
|
|
|
|
const BISHOP = 3;
|
|
|
|
const QUEEN = 4;
|
|
|
|
const KING = 5;
|
2020-12-27 23:58:51 -05:00
|
|
|
|
2020-12-28 15:12:35 -05:00
|
|
|
const Images = [
|
2020-12-28 16:41:13 -05:00
|
|
|
'./white_pawn.svg',
|
|
|
|
'./white_rook.svg',
|
|
|
|
'./white_knight.svg',
|
|
|
|
'./white_bishop.svg',
|
|
|
|
'./white_queen.svg',
|
|
|
|
'./white_king.svg',
|
|
|
|
|
|
|
|
'./black_pawn.svg',
|
|
|
|
'./black_rook.svg',
|
|
|
|
'./black_knight.svg',
|
|
|
|
'./black_bishop.svg',
|
|
|
|
'./black_queen.svg',
|
|
|
|
'./black_king.svg',
|
2020-12-28 15:12:35 -05:00
|
|
|
];
|
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
function range(n) {
|
|
|
|
return Array.from(Array(n).keys());
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:12:35 -05:00
|
|
|
function isBlack(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.color === BLACK;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isWhite(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.color === WHITE;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isPawn(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === PAWN;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRook(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === ROOK;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isBishop(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === BISHOP;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isQueen(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === QUEEN;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isKnight(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === KNIGHT;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function isKing(piece) {
|
2020-12-29 16:38:52 -05:00
|
|
|
return piece != null && piece.type === KING;
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageFromPiece(piece) {
|
|
|
|
if (piece) {
|
2020-12-29 16:55:35 -05:00
|
|
|
const image = piece.color === WHITE ? piece.type : piece.type + 6;
|
2020-12-29 16:38:52 -05:00
|
|
|
return Images[image];
|
|
|
|
}
|
|
|
|
return null;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
2020-12-27 23:58:51 -05:00
|
|
|
function Square(props) {
|
2020-12-28 22:51:01 -05:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="square"
|
|
|
|
onClick={props.onClick}
|
|
|
|
style={{
|
2020-12-29 16:38:52 -05:00
|
|
|
backgroundImage: `url(${imageFromPiece(props.piece)})`,
|
2020-12-28 22:51:01 -05:00
|
|
|
backgroundSize: `100%`,
|
|
|
|
backgroundColor: props.bgColor,
|
|
|
|
}}
|
2020-12-30 11:11:12 -05:00
|
|
|
title={props.piece == null ? "" : props.piece.getInfoText()}
|
2020-12-28 22:51:01 -05:00
|
|
|
>
|
|
|
|
</button>
|
|
|
|
);
|
2020-12-27 20:11:50 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 16:38:52 -05:00
|
|
|
class Piece {
|
|
|
|
constructor(color, type) {
|
|
|
|
this.color = color;
|
|
|
|
this.type = type;
|
2020-12-30 09:49:29 -05:00
|
|
|
this.passantable = false;
|
2020-12-30 11:11:12 -05:00
|
|
|
this.moves = 0;
|
2020-12-29 16:38:52 -05:00
|
|
|
}
|
2020-12-30 11:11:12 -05:00
|
|
|
|
2020-12-29 22:44:48 -05:00
|
|
|
setType(type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
2020-12-30 11:11:12 -05:00
|
|
|
|
|
|
|
getInfoText() {
|
|
|
|
if(this.moves === 1) {
|
|
|
|
return "Has made 1 move"
|
|
|
|
} else {
|
|
|
|
return "Has made " + this.moves + " moves"
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-12-27 20:11:50 -05:00
|
|
|
class Board extends React.Component {
|
2020-12-27 23:58:51 -05:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
squares: this.reset(),
|
2020-12-28 15:12:35 -05:00
|
|
|
blackIsNext: true,
|
|
|
|
hand: {
|
|
|
|
heldPiece: null,
|
|
|
|
},
|
2020-12-27 23:58:51 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:12:35 -05:00
|
|
|
setHand(hand) {
|
|
|
|
this.setState({
|
|
|
|
squares: this.state.squares,
|
|
|
|
blackIsNext: this.state.blackIsNext,
|
|
|
|
hand: hand,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-30 11:11:12 -05:00
|
|
|
clone() {
|
2020-12-30 13:48:53 -05:00
|
|
|
let board = new Board();
|
2020-12-30 11:11:12 -05:00
|
|
|
board.state.squares = this.state.squares.slice();
|
|
|
|
board.state.blackIsNext = this.state.blackIsNext;
|
|
|
|
board.state.hand = {
|
|
|
|
heldPiece: this.state.hand.heldPiece,
|
|
|
|
};
|
|
|
|
return board;
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:58:51 -05:00
|
|
|
reset() {
|
2020-12-30 13:48:53 -05:00
|
|
|
let squares = [];
|
|
|
|
const mainRow = [ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK];
|
2020-12-30 10:08:18 -05:00
|
|
|
function add(num, color, type) {
|
2020-12-30 13:48:53 -05:00
|
|
|
for(let i = 0; i < num; i++) {
|
|
|
|
if(color != null && type != null) {
|
|
|
|
squares.push(new Piece(color, type));
|
|
|
|
} else {
|
|
|
|
squares.push(null);
|
|
|
|
}
|
2020-12-30 10:08:18 -05:00
|
|
|
}
|
2020-12-29 22:44:48 -05:00
|
|
|
}
|
2020-12-30 13:48:53 -05:00
|
|
|
|
|
|
|
mainRow.forEach(type => add(1, WHITE, type));
|
2020-12-30 10:08:18 -05:00
|
|
|
add(8, WHITE, PAWN);
|
2020-12-30 13:48:53 -05:00
|
|
|
add(32, null, null);
|
2020-12-30 10:08:18 -05:00
|
|
|
add(8, BLACK, PAWN);
|
2020-12-30 13:48:53 -05:00
|
|
|
mainRow.forEach(type => add(1, BLACK, type));
|
2020-12-29 16:55:35 -05:00
|
|
|
|
2020-12-27 23:58:51 -05:00
|
|
|
return squares;
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:12:35 -05:00
|
|
|
getXandY(i) {
|
2020-12-30 13:48:53 -05:00
|
|
|
const x = i % 8;
|
|
|
|
const y = Math.floor(i / 8);
|
2020-12-28 15:12:35 -05:00
|
|
|
return [x, y];
|
|
|
|
}
|
|
|
|
|
2020-12-30 10:08:18 -05:00
|
|
|
getIndex(x, y) {
|
|
|
|
return x + (y * 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidXY(x, y) {
|
|
|
|
return x < 8 && x >=0 && y < 8 && y >= 0;
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:48:53 -05:00
|
|
|
squareCount() {
|
|
|
|
return this.state.squares.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
squareAt(i) {
|
|
|
|
return i >= 0 && i < 64 ? this.state.squares[i] : null;
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:51:01 -05:00
|
|
|
pieceAt(x, y) {
|
2020-12-30 13:48:53 -05:00
|
|
|
if (this.isValidXY(x, y)) {
|
2020-12-30 10:08:18 -05:00
|
|
|
return this.state.squares[this.getIndex(x, y)];
|
2020-12-28 15:12:35 -05:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
whiteAt(x, y) {
|
2020-12-30 13:48:53 -05:00
|
|
|
const square = this.pieceAt(x, y);
|
2020-12-28 15:12:35 -05:00
|
|
|
if (square == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-28 22:51:01 -05:00
|
|
|
return isWhite(square);
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
blackAt(x, y) {
|
2020-12-30 13:48:53 -05:00
|
|
|
const square = this.pieceAt(x, y);
|
2020-12-28 15:12:35 -05:00
|
|
|
if (square == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-28 22:51:01 -05:00
|
|
|
return isBlack(square);
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isEnemyOf(piece, x, y) {
|
2020-12-30 13:48:53 -05:00
|
|
|
return isBlack(piece) ? this.whiteAt(x, y) : this.blackAt(x, y);
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getValidMovesAt(piece, x, y) {
|
2020-12-30 13:48:53 -05:00
|
|
|
let moves = [];
|
|
|
|
const tryAddMove = (x, y) => {
|
2020-12-30 10:08:18 -05:00
|
|
|
if (this.isValidXY(x, y)) {
|
|
|
|
if(this.pieceAt(x, y) == null) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y});
|
2020-12-30 10:08:18 -05:00
|
|
|
// Keep searching
|
|
|
|
return 0;
|
|
|
|
} else if (this.isEnemyOf(piece, x, y)) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y});
|
2020-12-30 10:08:18 -05:00
|
|
|
}
|
|
|
|
// Stop searching
|
|
|
|
return 1;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
};
|
2020-12-30 13:48:53 -05:00
|
|
|
function addBunch(xFunc, yFunc, isUp) {
|
|
|
|
for (let i = 1; i < 8; i++) {
|
|
|
|
if(tryAddMove(xFunc(i), yFunc(i)) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 15:12:35 -05:00
|
|
|
|
|
|
|
if (isPawn(piece)) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const pieceIsBlack = isBlack(piece);
|
|
|
|
const shift = pieceIsBlack ? -1 : 1;
|
|
|
|
const startLine = pieceIsBlack ? 6 : 1;
|
2020-12-28 15:12:35 -05:00
|
|
|
|
2020-12-30 09:49:29 -05:00
|
|
|
// Check for en passant
|
2020-12-30 16:56:57 -05:00
|
|
|
const left = this.pieceAt(x - 1, y);
|
|
|
|
const right = this.pieceAt(x + 1, y);
|
2020-12-30 09:49:29 -05:00
|
|
|
if (left != null && left.passantable && left.color !== piece.color) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x: x - 1, y: y + shift, passant: {x: x - 1, y}})
|
2020-12-30 09:49:29 -05:00
|
|
|
}
|
|
|
|
if (right != null && right.passantable && right.color !== piece.color) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x: x + 1, y: y + shift, passant: {x: x + 1, y}})
|
2020-12-29 22:44:48 -05:00
|
|
|
}
|
|
|
|
|
2020-12-28 22:51:01 -05:00
|
|
|
if (this.pieceAt(x, y + shift) == null) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y: y + shift});
|
|
|
|
// Pawn moving two spaces becomes en-passantable
|
2020-12-28 22:51:01 -05:00
|
|
|
if (y === startLine && this.pieceAt(x, y + (shift * 2)) == null) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y: y + (shift * 2), passantable: true});
|
2020-12-28 18:01:05 -05:00
|
|
|
}
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
2020-12-29 16:38:52 -05:00
|
|
|
[x + 1, x - 1].forEach(x => {
|
2020-12-30 10:08:18 -05:00
|
|
|
if (this.isValidXY(x, y + shift) && this.isEnemyOf(piece, x, y + shift)) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y: y + shift});
|
2020-12-28 18:01:05 -05:00
|
|
|
}
|
|
|
|
});
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isRook(piece)) {
|
2020-12-30 13:48:53 -05:00
|
|
|
addBunch(n => {return x;}, n => {return y + n;});
|
|
|
|
addBunch(n => {return x;}, n => {return y - n;});
|
|
|
|
addBunch(n => {return x + n;}, n => {return y;});
|
|
|
|
addBunch(n => {return x - n;}, n => {return y;});
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isBishop(piece)) {
|
2020-12-30 13:48:53 -05:00
|
|
|
addBunch(n => {return x + n;}, n => {return y + n;});
|
|
|
|
addBunch(n => {return x - n;}, n => {return y + n;});
|
|
|
|
addBunch(n => {return x + n;}, n => {return y - n;});
|
|
|
|
addBunch(n => {return x - n;}, n => {return y - n;});
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isQueen(piece)) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const [rook, bishop] =
|
2020-12-30 13:48:53 -05:00
|
|
|
[new Piece(piece.color, ROOK), new Piece(piece.color, BISHOP)];
|
2020-12-28 15:12:35 -05:00
|
|
|
moves = moves.concat(this.getValidMovesAt(rook, x, y));
|
|
|
|
moves = moves.concat(this.getValidMovesAt(bishop, x, y));
|
|
|
|
} else if (isKnight(piece)) {
|
2020-12-30 16:56:57 -05:00
|
|
|
[
|
2020-12-29 09:56:49 -05:00
|
|
|
[2, 1], [2, -1], [-2, 1], [-2, -1],
|
|
|
|
[1, 2], [1, -2], [-1, 2], [-1, -2],
|
2020-12-30 16:56:57 -05:00
|
|
|
].forEach(delta => tryAddMove(x + delta[0], y + delta[1]));
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isKing(piece)) {
|
2020-12-30 16:56:57 -05:00
|
|
|
[[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]));
|
|
|
|
if (piece.moves === 0) {
|
|
|
|
const [x, y] = this.getXandY(this.findIndex(piece));
|
|
|
|
let leftRook = this.pieceAt(0, y);
|
|
|
|
if(isRook(leftRook) && leftRook.moves === 0) {
|
|
|
|
// Check if between space puts king in check
|
|
|
|
// Check if spaces between rook and king are empty
|
|
|
|
// add move(x: x - 2, y, castle: [x, y])
|
|
|
|
}
|
|
|
|
let rightRook = this.pieceAt(7, y);
|
|
|
|
if(isRook(rightRook) && rightRook.moves === 0) {
|
|
|
|
// Check if between space puts king in check
|
|
|
|
// Check if spaces between rook and king are empty
|
|
|
|
// add move(x: x + 2, y, castle: [x, y])
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:38:52 -05:00
|
|
|
findIndex(piece) {
|
2020-12-30 13:48:53 -05:00
|
|
|
for(let i = 0; i < this.squareCount(); i++) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const check = this.state.squares[i];
|
2020-12-29 16:38:52 -05:00
|
|
|
if(check && check.type === piece.type && check.color === piece.color) {
|
|
|
|
return i;
|
2020-12-28 19:21:13 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:38:52 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
distanceBetween(i1, i2) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const [pos1X, pos1Y] = this.getXandY(i1);
|
|
|
|
const [pos2X, pos2Y] = this.getXandY(i2);
|
2020-12-29 16:38:52 -05:00
|
|
|
|
2020-12-30 13:48:53 -05:00
|
|
|
let a = pos1X - pos2X;
|
2020-12-29 16:38:52 -05:00
|
|
|
a = a * a;
|
|
|
|
|
2020-12-30 13:48:53 -05:00
|
|
|
let b = pos1Y - pos2Y;
|
2020-12-29 16:38:52 -05:00
|
|
|
b = b * b;
|
|
|
|
|
|
|
|
return Math.sqrt(a + b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inCheck(piece) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const kingPos = this.getXandY(this.findIndex(piece));
|
2020-12-28 19:21:13 -05:00
|
|
|
|
2020-12-30 13:48:53 -05:00
|
|
|
for(let i = 0; i < this.squareCount(); i++) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const [x, y] = this.getXandY(i);
|
2020-12-29 08:25:17 -05:00
|
|
|
if(this.isEnemyOf(piece, x, y)) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const moves = this.getValidMoves(i);
|
2020-12-30 13:48:53 -05:00
|
|
|
for(let j = 0; j < moves.length; j++) {
|
2020-12-30 09:49:29 -05:00
|
|
|
if(moves[j].x === kingPos[0] && moves[j].y === kingPos[1]) {
|
2020-12-29 08:25:17 -05:00
|
|
|
return piece;
|
|
|
|
}
|
2020-12-28 19:21:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:51:01 -05:00
|
|
|
return null;
|
2020-12-28 19:21:13 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 08:25:17 -05:00
|
|
|
whoInCheck() {
|
2020-12-30 16:56:57 -05:00
|
|
|
const blackKing = this.inCheck(new Piece(BLACK, KING));
|
2020-12-29 16:38:52 -05:00
|
|
|
return blackKing ? blackKing : this.inCheck(new Piece(WHITE, KING));
|
2020-12-29 08:25:17 -05:00
|
|
|
}
|
|
|
|
|
2020-12-28 19:21:13 -05:00
|
|
|
checkmate() {
|
2020-12-30 16:56:57 -05:00
|
|
|
const checkedKing = this.whoInCheck();
|
2020-12-29 08:25:17 -05:00
|
|
|
if (checkedKing != null) {
|
|
|
|
// For each square
|
2020-12-30 13:48:53 -05:00
|
|
|
for(let i = 0; i < this.squareCount(); i++) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const piece = this.squareAt(i);
|
2020-12-29 08:25:17 -05:00
|
|
|
// If that piece is on the checked team
|
|
|
|
if (piece != null && isBlack(piece) === isBlack(checkedKing)) {
|
|
|
|
// For each move of the above piece
|
2020-12-30 16:56:57 -05:00
|
|
|
const moves = this.getValidMoves(i)
|
|
|
|
for(const move of moves) {
|
2020-12-30 11:11:12 -05:00
|
|
|
// Copy the board
|
2020-12-30 16:56:57 -05:00
|
|
|
const board = this.clone();
|
2020-12-30 11:11:12 -05:00
|
|
|
board.state.squares[board.getIndex(move.x, move.y)] = board.state.squares[i];
|
2020-12-29 22:44:48 -05:00
|
|
|
board.state.squares[i] = null;
|
2020-12-30 16:56:57 -05:00
|
|
|
const check = board.inCheck(checkedKing);
|
2020-12-29 22:44:48 -05:00
|
|
|
if (check == null || check.color !== checkedKing.color) {
|
2020-12-29 08:25:17 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:21:13 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:12:35 -05:00
|
|
|
getValidMoves(source) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const [x, y] = this.getXandY(source);
|
2020-12-28 15:12:35 -05:00
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
const piece = this.squareAt(source);
|
2020-12-28 15:12:35 -05:00
|
|
|
return this.getValidMovesAt(piece, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidMove(source, dest) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const [destX, destY] = this.getXandY(dest);
|
2020-12-28 15:12:35 -05:00
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
for (const move of this.getValidMoves(source)) {
|
2020-12-30 10:08:18 -05:00
|
|
|
if (destX === move.x && destY === move.y) {
|
2020-12-29 22:44:48 -05:00
|
|
|
return move;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 22:44:48 -05:00
|
|
|
return null;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isHoldingPiece() {
|
|
|
|
return this.heldPiece() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
heldPiece() {
|
|
|
|
return this.state.hand.heldPiece;
|
|
|
|
}
|
|
|
|
|
2020-12-29 08:25:17 -05:00
|
|
|
makeMove(from, to) {
|
2020-12-27 23:58:51 -05:00
|
|
|
const squares = this.state.squares.slice();
|
2020-12-30 16:56:57 -05:00
|
|
|
const move = this.isValidMove(from, to)
|
2020-12-29 22:44:48 -05:00
|
|
|
if (move) {
|
2020-12-30 09:49:29 -05:00
|
|
|
if (move.passant) {
|
2020-12-30 11:11:12 -05:00
|
|
|
squares[this.getIndex(move.passant.x, move.passant.y)] = null;
|
2020-12-30 09:49:29 -05:00
|
|
|
}
|
|
|
|
// Remove existing passantable states
|
2020-12-30 11:11:12 -05:00
|
|
|
squares.forEach(square => {
|
2020-12-30 09:49:29 -05:00
|
|
|
if (square) {
|
|
|
|
square.passantable = false;
|
|
|
|
}
|
2020-12-30 11:11:12 -05:00
|
|
|
});
|
2020-12-30 09:49:29 -05:00
|
|
|
if (move.passantable) {
|
|
|
|
squares[from].passantable = true;
|
2020-12-29 22:44:48 -05:00
|
|
|
}
|
2020-12-30 16:56:57 -05:00
|
|
|
const y = this.getXandY(to)[1];
|
2020-12-29 08:25:17 -05:00
|
|
|
squares[to] = squares[from];
|
|
|
|
squares[from] = null;
|
2020-12-29 22:44:48 -05:00
|
|
|
if (squares[to].type === PAWN && (y === 0 || y === 7)) {
|
|
|
|
squares[to].setType(QUEEN);
|
|
|
|
}
|
2020-12-30 11:11:12 -05:00
|
|
|
squares[to].moves++;
|
2020-12-29 08:25:17 -05:00
|
|
|
this.setState({
|
|
|
|
squares: squares,
|
|
|
|
blackIsNext: !this.state.blackIsNext,
|
2020-12-29 09:56:49 -05:00
|
|
|
hand: {
|
|
|
|
heldPiece: null,
|
|
|
|
}
|
2020-12-29 08:25:17 -05:00
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(i) {
|
2020-12-29 09:56:49 -05:00
|
|
|
if (this.checkmate()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-28 15:12:35 -05:00
|
|
|
if (this.isHoldingPiece()) {
|
2020-12-30 00:09:02 -05:00
|
|
|
// Copy the board
|
2020-12-30 13:48:53 -05:00
|
|
|
let board = this.clone();
|
2020-12-30 11:11:12 -05:00
|
|
|
board.state.squares[i] = board.state.squares[board.heldPiece()];
|
2020-12-30 00:09:02 -05:00
|
|
|
board.state.squares[this.heldPiece()] = null;
|
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
const moversKing = this.state.blackIsNext ?
|
2020-12-30 00:09:02 -05:00
|
|
|
new Piece(BLACK, KING) : new Piece(WHITE, KING);
|
|
|
|
if (board.inCheck(moversKing) != null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-29 08:25:17 -05:00
|
|
|
if (this.makeMove(this.heldPiece(), i) !== 0) {
|
2020-12-28 15:12:35 -05:00
|
|
|
this.setHand({
|
|
|
|
heldPiece: null,
|
|
|
|
});
|
|
|
|
}
|
2020-12-30 00:09:02 -05:00
|
|
|
} else if (this.state.squares[i] != null) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const isSquareBlack = isBlack(this.state.squares[i]);
|
2020-12-28 15:12:35 -05:00
|
|
|
if(isSquareBlack === this.state.blackIsNext) {
|
|
|
|
this.setHand({
|
|
|
|
heldPiece: i,
|
|
|
|
});
|
|
|
|
}
|
2020-12-27 23:58:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:11:50 -05:00
|
|
|
renderSquare(i) {
|
2020-12-30 16:56:57 -05:00
|
|
|
const plainBg = (i + (Math.floor(i / 8))) % 2 === 0 ? "white" : "#666";
|
|
|
|
const bgColor = this.heldPiece() === i ? "#5D98E6" : plainBg;
|
2020-12-27 23:58:51 -05:00
|
|
|
return (
|
|
|
|
<Square
|
2020-12-30 13:48:53 -05:00
|
|
|
key={"square-" + i}
|
2020-12-29 16:38:52 -05:00
|
|
|
piece={this.state.squares[i]}
|
2020-12-27 23:58:51 -05:00
|
|
|
onClick={() => this.handleClick(i)}
|
2020-12-28 22:51:01 -05:00
|
|
|
bgColor={bgColor}
|
2020-12-27 23:58:51 -05:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:48:53 -05:00
|
|
|
row(r) {
|
|
|
|
const i = r * 8;
|
2020-12-27 23:58:51 -05:00
|
|
|
return (
|
2020-12-30 16:56:57 -05:00
|
|
|
<div className="board-row" key={"row=" + r}>
|
|
|
|
{range(8).map(n => this.renderSquare(n + i))}
|
|
|
|
</div>
|
2020-12-27 23:58:51 -05:00
|
|
|
);
|
2020-12-27 20:11:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-30 16:56:57 -05:00
|
|
|
const checkMsg = this.whoInCheck() ? "Check! " : "";
|
|
|
|
const isCheckmate = this.checkmate();
|
|
|
|
const namedPlayer = isCheckmate ?
|
2020-12-29 09:56:49 -05:00
|
|
|
!this.state.blackIsNext : this.state.blackIsNext
|
2020-12-30 16:56:57 -05:00
|
|
|
const color = namedPlayer ? 'Black' : 'White';
|
|
|
|
|
2020-12-29 22:44:48 -05:00
|
|
|
const status = isCheckmate ? "Checkmate! " + color + " Wins!" :
|
2020-12-29 09:56:49 -05:00
|
|
|
checkMsg + color + "'s Turn";
|
2020-12-27 20:11:50 -05:00
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
return (
|
2020-12-28 16:41:13 -05:00
|
|
|
<div style={{textAlign: `center`,}}>
|
|
|
|
<div className="status"><h1>{status}</h1></div>
|
2020-12-30 16:56:57 -05:00
|
|
|
{range(8).map(n => this.row(n))}
|
2020-12-27 20:11:50 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Game extends React.Component {
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="game">
|
2020-12-28 16:41:13 -05:00
|
|
|
<div className="game-board">
|
|
|
|
<Board />
|
|
|
|
</div>
|
2020-12-27 20:11:50 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<Game />,
|
|
|
|
document.getElementById('root')
|
|
|
|
);
|