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
|
|
|
];
|
|
|
|
|
|
|
|
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,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
</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-29 16:38:52 -05:00
|
|
|
}
|
2020-12-29 22:44:48 -05:00
|
|
|
setType(type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
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-27 23:58:51 -05:00
|
|
|
reset() {
|
|
|
|
let whiteRow = [
|
2020-12-29 16:38:52 -05:00
|
|
|
new Piece(WHITE, ROOK),
|
|
|
|
new Piece(WHITE, KNIGHT),
|
|
|
|
new Piece(WHITE, BISHOP),
|
|
|
|
new Piece(WHITE, QUEEN),
|
|
|
|
new Piece(WHITE, KING),
|
|
|
|
new Piece(WHITE, BISHOP),
|
|
|
|
new Piece(WHITE, KNIGHT),
|
|
|
|
new Piece(WHITE, ROOK),
|
2020-12-27 23:58:51 -05:00
|
|
|
];
|
|
|
|
let blackRow = [
|
2020-12-29 16:38:52 -05:00
|
|
|
new Piece(BLACK, ROOK),
|
|
|
|
new Piece(BLACK, KNIGHT),
|
|
|
|
new Piece(BLACK, BISHOP),
|
|
|
|
new Piece(BLACK, QUEEN),
|
|
|
|
new Piece(BLACK, KING),
|
|
|
|
new Piece(BLACK, BISHOP),
|
|
|
|
new Piece(BLACK, KNIGHT),
|
|
|
|
new Piece(BLACK, ROOK)
|
2020-12-27 23:58:51 -05:00
|
|
|
];
|
2020-12-29 22:44:48 -05:00
|
|
|
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);
|
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) {
|
|
|
|
let x = i % 8;
|
|
|
|
let y = Math.floor(i / 8);
|
|
|
|
return [x, y];
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:51:01 -05:00
|
|
|
pieceAt(x, y) {
|
2020-12-28 15:12:35 -05:00
|
|
|
let i = x + (y * 8);
|
2020-12-29 22:44:48 -05:00
|
|
|
if (i < 64 && x < 8 && y < 8 && x >= 0 && y >= 0) {
|
2020-12-28 15:12:35 -05:00
|
|
|
return this.state.squares[x + (y * 8)];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
whiteAt(x, y) {
|
2020-12-28 22:51:01 -05:00
|
|
|
let 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-28 22:51:01 -05:00
|
|
|
let 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) {
|
|
|
|
let pieceIsBlack = isBlack(piece);
|
|
|
|
return pieceIsBlack ? this.whiteAt(x, y) : this.blackAt(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
getValidMovesAt(piece, x, y) {
|
|
|
|
var i;
|
|
|
|
var moves = [];
|
|
|
|
let tryAddMove = (x, y) => {
|
2020-12-29 22:44:48 -05:00
|
|
|
if (x >= 0 && x < 8 && y >= 0 && y < 8 && this.pieceAt(x, y) == null) {
|
2020-12-30 09:49:29 -05:00
|
|
|
moves.push({x: x, y: y});
|
2020-12-28 15:12:35 -05:00
|
|
|
// Keep searching
|
|
|
|
return 0;
|
|
|
|
} else if (this.isEnemyOf(piece, x, y)) {
|
2020-12-30 09:49:29 -05:00
|
|
|
moves.push({x: x, y: y});
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
// Stop searching
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isPawn(piece)) {
|
|
|
|
let pieceIsBlack = isBlack(piece);
|
|
|
|
let shift = pieceIsBlack ? -1 : 1;
|
|
|
|
let startLine = pieceIsBlack ? 6 : 1;
|
|
|
|
|
2020-12-30 09:49:29 -05:00
|
|
|
// Check for en passant
|
|
|
|
let left = this.pieceAt(x - 1, y);
|
|
|
|
let right = this.pieceAt(x + 1, y);
|
|
|
|
if (left != null && left.passantable && left.color !== piece.color) {
|
|
|
|
moves.push({x:x - 1, y:y + shift, passant: {x: x - 1, y: y}})
|
|
|
|
}
|
|
|
|
if (right != null && right.passantable && right.color !== piece.color) {
|
|
|
|
moves.push({x: x + 1, y: y + shift, passant: {x: x + 1, y: y}})
|
2020-12-29 22:44:48 -05:00
|
|
|
}
|
|
|
|
|
2020-12-30 09:49:29 -05:00
|
|
|
// Pawn moving two spaces becomes en-passantable
|
2020-12-28 22:51:01 -05:00
|
|
|
if (this.pieceAt(x, y + shift) == null) {
|
2020-12-30 09:49:29 -05:00
|
|
|
moves.push({x: x, y: y + shift});
|
2020-12-28 22:51:01 -05:00
|
|
|
if (y === startLine && this.pieceAt(x, y + (shift * 2)) == null) {
|
2020-12-30 09:49:29 -05:00
|
|
|
moves.push({x: 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-29 22:44:48 -05:00
|
|
|
if (x >= 0 && x < 8 && this.isEnemyOf(piece, x, y + shift)) {
|
2020-12-30 09:49:29 -05:00
|
|
|
moves.push({x: x, y: y + shift});
|
2020-12-28 18:01:05 -05:00
|
|
|
}
|
|
|
|
});
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isRook(piece)) {
|
|
|
|
// Down
|
|
|
|
for (i = y + 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(x, i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Up
|
|
|
|
for (i = y - 1; i >= 0; i--) {
|
|
|
|
if (tryAddMove(x, i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Right
|
|
|
|
for (i = x + 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(i, y) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Left
|
|
|
|
for (i = x - 1; i >= 0; i--) {
|
|
|
|
if (tryAddMove(i, y) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isBishop(piece)) {
|
|
|
|
// Down-Right
|
|
|
|
for (i = 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(x + i, y + i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Up-Right
|
|
|
|
for (i = 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(x + i, y - i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Down-Left
|
|
|
|
for (i = 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(x - i, y + i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Up-Left
|
|
|
|
for (i = 1; i < 8; i++) {
|
|
|
|
if (tryAddMove(x - i, y - i) !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isQueen(piece)) {
|
|
|
|
let [rook, bishop] = isBlack(piece) ?
|
2020-12-29 16:38:52 -05:00
|
|
|
[new Piece(BLACK, ROOK), new Piece(BLACK, BISHOP)] :
|
|
|
|
[new Piece(WHITE, ROOK), new Piece(WHITE, 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-29 09:56:49 -05:00
|
|
|
let deltas = [
|
|
|
|
[2, 1], [2, -1], [-2, 1], [-2, -1],
|
|
|
|
[1, 2], [1, -2], [-1, 2], [-1, -2],
|
|
|
|
];
|
2020-12-29 16:38:52 -05:00
|
|
|
deltas.forEach(delta => tryAddMove(x + delta[0], y + delta[1]));
|
2020-12-28 15:12:35 -05:00
|
|
|
} else if (isKing(piece)) {
|
2020-12-29 09:56:49 -05:00
|
|
|
let deltas = [
|
|
|
|
[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]
|
|
|
|
];
|
2020-12-29 16:38:52 -05:00
|
|
|
deltas.forEach(delta => tryAddMove(x + delta[0], y + delta[1]));
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
return moves;
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:38:52 -05:00
|
|
|
findIndex(piece) {
|
2020-12-29 08:25:17 -05:00
|
|
|
for(var i = 0; i < this.squareCount(); i++) {
|
2020-12-29 16:38:52 -05:00
|
|
|
let check = this.state.squares[i];
|
|
|
|
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) {
|
|
|
|
let [pos1X, pos1Y] = this.getXandY(i1);
|
|
|
|
let [pos2X, pos2Y] = this.getXandY(i2);
|
|
|
|
|
|
|
|
var a = pos1X - pos2X;
|
|
|
|
a = a * a;
|
|
|
|
|
|
|
|
var b = pos1Y - pos2Y;
|
|
|
|
b = b * b;
|
|
|
|
|
|
|
|
return Math.sqrt(a + b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inCheck(piece) {
|
|
|
|
var i;
|
|
|
|
var kingPos = this.getXandY(this.findIndex(piece));
|
2020-12-28 19:21:13 -05:00
|
|
|
|
2020-12-29 08:25:17 -05:00
|
|
|
for(i = 0; i < this.squareCount(); i++) {
|
|
|
|
let [x, y] = this.getXandY(i);
|
|
|
|
if(this.isEnemyOf(piece, x, y)) {
|
|
|
|
let moves = this.getValidMoves(i);
|
|
|
|
for(var 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-29 16:38:52 -05:00
|
|
|
let blackKing = this.inCheck(new Piece(BLACK, KING));
|
|
|
|
return blackKing ? blackKing : this.inCheck(new Piece(WHITE, KING));
|
2020-12-29 08:25:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
squareCount() {
|
|
|
|
return this.state.squares.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
squareAt(i) {
|
|
|
|
return i >= 0 && i < 64 ? this.state.squares[i] : null;
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:21:13 -05:00
|
|
|
checkmate() {
|
2020-12-29 08:25:17 -05:00
|
|
|
let checkedKing = this.whoInCheck();
|
|
|
|
if (checkedKing != null) {
|
|
|
|
// For each square
|
|
|
|
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)) {
|
|
|
|
// 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++) {
|
2020-12-30 09:49:29 -05:00
|
|
|
let [x, y] = [moves[j].x, moves[j].y];
|
2020-12-29 22:44:48 -05:00
|
|
|
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) {
|
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) {
|
|
|
|
let [x, y] = this.getXandY(source);
|
|
|
|
|
|
|
|
let piece = this.state.squares[source];
|
|
|
|
return this.getValidMovesAt(piece, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidMove(source, dest) {
|
|
|
|
let [destX, destY] = this.getXandY(dest);
|
|
|
|
|
|
|
|
for (var move of this.getValidMoves(source)) {
|
2020-12-30 09:49:29 -05:00
|
|
|
let x = move.x;
|
|
|
|
let y = move.y;
|
2020-12-28 15:12:35 -05:00
|
|
|
if (destX === x && destY === 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-29 22:44:48 -05:00
|
|
|
let move = this.isValidMove(from, to)
|
|
|
|
if (move) {
|
2020-12-30 09:49:29 -05:00
|
|
|
if (move.passant) {
|
|
|
|
squares[move.passant.x + (move.passant.y * 8)] = null;
|
|
|
|
}
|
|
|
|
// Remove existing passantable states
|
|
|
|
for(var square of squares) {
|
|
|
|
if (square) {
|
|
|
|
square.passantable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (move.passantable) {
|
|
|
|
squares[from].passantable = true;
|
2020-12-29 22:44:48 -05:00
|
|
|
}
|
|
|
|
let 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-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
|
|
|
|
var board = new Board();
|
|
|
|
board.state.squares = this.state.squares.slice();
|
|
|
|
board.state.squares[i] = board.state.squares[this.heldPiece()];
|
|
|
|
board.state.squares[this.heldPiece()] = null;
|
|
|
|
|
|
|
|
let moversKing = this.state.blackIsNext ?
|
|
|
|
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-29 08:25:17 -05:00
|
|
|
let 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-28 17:51:09 -05:00
|
|
|
let plainBg = (i + (Math.floor(i / 8))) % 2 === 0 ?
|
|
|
|
"white" : "#666";
|
2020-12-28 16:54:23 -05:00
|
|
|
let bgColor = this.heldPiece() === i ?
|
2020-12-28 17:51:09 -05:00
|
|
|
"#5D98E6" : plainBg;
|
2020-12-27 23:58:51 -05:00
|
|
|
return (
|
|
|
|
<Square
|
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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
row(row) {
|
|
|
|
let i = row * 8;
|
|
|
|
return (
|
|
|
|
<div className="board-row">
|
|
|
|
{this.renderSquare(i)}
|
|
|
|
{this.renderSquare(i + 1)}
|
|
|
|
{this.renderSquare(i + 2)}
|
|
|
|
{this.renderSquare(i + 3)}
|
|
|
|
{this.renderSquare(i + 4)}
|
|
|
|
{this.renderSquare(i + 5)}
|
|
|
|
{this.renderSquare(i + 6)}
|
|
|
|
{this.renderSquare(i + 7)}
|
|
|
|
</div>
|
|
|
|
);
|
2020-12-27 20:11:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-29 08:25:17 -05:00
|
|
|
let checkMsg = this.whoInCheck() ? "Check! " : "";
|
2020-12-29 09:56:49 -05:00
|
|
|
let isCheckmate = this.checkmate();
|
|
|
|
var namedPlayer = isCheckmate ?
|
|
|
|
!this.state.blackIsNext : this.state.blackIsNext
|
|
|
|
let 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-27 23:58:51 -05:00
|
|
|
var texttext =
|
2020-12-28 16:41:13 -05:00
|
|
|
<div style={{textAlign: `center`,}}>
|
|
|
|
<div className="status"><h1>{status}</h1></div>
|
2020-12-27 23:58:51 -05:00
|
|
|
{this.row(0)}
|
|
|
|
{this.row(1)}
|
|
|
|
{this.row(2)}
|
|
|
|
{this.row(3)}
|
|
|
|
{this.row(4)}
|
|
|
|
{this.row(5)}
|
|
|
|
{this.row(6)}
|
|
|
|
{this.row(7)}
|
2020-12-27 20:11:50 -05:00
|
|
|
</div>
|
2020-12-27 23:58:51 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
return (
|
|
|
|
texttext
|
2020-12-27 20:11:50 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Game extends React.Component {
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="game">
|
|
|
|
<div className="game-info">
|
|
|
|
<div>{/* status */}</div>
|
|
|
|
<ol>{/* TODO */}</ol>
|
|
|
|
</div>
|
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')
|
|
|
|
);
|