2020-12-27 20:11:50 -05:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2021-01-01 20:03:56 -05:00
|
|
|
|
|
|
|
import Popup from './components/Popup';
|
|
|
|
|
2020-12-27 20:11:50 -05:00
|
|
|
import './index.css';
|
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
|
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
|
|
|
|
2021-01-02 01:51:28 -05:00
|
|
|
const EMPTY = -1;
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2021-01-02 00:46:08 -05:00
|
|
|
const SHUFFLING_ENABLED = 0;
|
|
|
|
|
|
|
|
function getAllSettings() {
|
|
|
|
return [SHUFFLING_ENABLED];
|
|
|
|
}
|
|
|
|
|
|
|
|
function settingText(setting) {
|
|
|
|
switch(setting) {
|
|
|
|
case SHUFFLING_ENABLED:
|
|
|
|
return "Shuffle Back Row";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
function range(n) {
|
|
|
|
return Array.from(Array(n).keys());
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:38:52 -05:00
|
|
|
function imageFromPiece(piece) {
|
2021-01-02 01:51:28 -05:00
|
|
|
if (piece && piece.type >= 0) {
|
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"
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 01:51:28 -05:00
|
|
|
|
|
|
|
isEmpty() {
|
|
|
|
return this.type === EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFull() {
|
|
|
|
return !this.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
isBlack() {
|
|
|
|
return this.color === BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
isWhite() {
|
|
|
|
return this.color === WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEnemyOf(piece) {
|
|
|
|
if (this.color === EMPTY || piece.color === EMPTY) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.color !== piece.color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isFriendOf(piece) {
|
|
|
|
if (this.color === EMPTY || piece.color === EMPTY) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.color === piece.color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is(type) {
|
|
|
|
return this.type === type;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasMoved() {
|
|
|
|
return this.moves !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasntMoved() {
|
|
|
|
return !this.hasMoved();
|
|
|
|
}
|
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);
|
2021-01-01 20:03:56 -05:00
|
|
|
this.state = (props && props.text) ?
|
|
|
|
{
|
2020-12-31 13:39:56 -05:00
|
|
|
squares: this.stateFromText(props.text),
|
|
|
|
blackIsNext: true,
|
|
|
|
hand: {
|
|
|
|
heldPiece: null,
|
|
|
|
},
|
2021-01-02 00:46:08 -05:00
|
|
|
settings: {},
|
2021-01-01 20:03:56 -05:00
|
|
|
} : this.originalState();
|
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-31 13:39:56 -05:00
|
|
|
stateFromText(text) {
|
|
|
|
text = text.replace(/[\n]+/g, '');
|
|
|
|
const squares = text.substring(1);
|
|
|
|
return {
|
|
|
|
hand: null,
|
|
|
|
blackIsNext: text[0].toUpperCase() === 'B',
|
|
|
|
squares: squares.split('').map(c => {
|
|
|
|
const type = c.toLowerCase();
|
|
|
|
const color = c === type ? WHITE : BLACK;
|
|
|
|
switch (type) {
|
|
|
|
case 'r':
|
|
|
|
return new Piece(color, ROOK);
|
|
|
|
case 'n':
|
|
|
|
return new Piece(color, KNIGHT);
|
|
|
|
case 'b':
|
|
|
|
return new Piece(color, BISHOP);
|
|
|
|
case 'q':
|
|
|
|
return new Piece(color, QUEEN);
|
|
|
|
case 'k':
|
|
|
|
return new Piece(color, KING);
|
|
|
|
case 'p':
|
|
|
|
return new Piece(color, PAWN);
|
|
|
|
default:
|
2021-01-02 01:51:28 -05:00
|
|
|
return new Piece(EMPTY, EMPTY);
|
2020-12-31 13:39:56 -05:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
shuffledBackRow() {
|
|
|
|
return "rnbqkbnr".split('').sort(() => Math.random() - 0.5).join('');
|
|
|
|
}
|
|
|
|
|
2021-01-02 00:46:08 -05:00
|
|
|
shuffledBackRowState() {
|
2021-01-01 20:03:56 -05:00
|
|
|
const backRow = this.shuffledBackRow();
|
|
|
|
const text = ["B", backRow, "pppppppp",
|
|
|
|
"________", "________", "________", "________",
|
|
|
|
"PPPPPPPP", backRow.toUpperCase()].join('');
|
2021-01-02 00:46:08 -05:00
|
|
|
return this.stateFromText(text);
|
2021-01-01 20:03:56 -05:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:39:56 -05:00
|
|
|
textFromState() {
|
|
|
|
const turn = (this.state.blackIsNext? 'B' : 'W');
|
|
|
|
return turn + this.state.squares.map(square => {
|
|
|
|
if (!square) {
|
|
|
|
return '_';
|
|
|
|
}
|
|
|
|
let color = (c) => {
|
|
|
|
return square.color === BLACK ? c.toUpperCase() : c;
|
|
|
|
};
|
|
|
|
switch (square.type) {
|
|
|
|
case ROOK:
|
|
|
|
return color('r');
|
|
|
|
case KNIGHT:
|
|
|
|
return color('n');
|
|
|
|
case BISHOP:
|
|
|
|
return color('b');
|
|
|
|
case QUEEN:
|
|
|
|
return color('q');
|
|
|
|
case KING:
|
|
|
|
return color('k');
|
|
|
|
case PAWN:
|
|
|
|
return color('p');
|
|
|
|
default:
|
|
|
|
return '_';
|
|
|
|
}
|
|
|
|
}).join('');;
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
doReset() {
|
2021-01-02 00:46:08 -05:00
|
|
|
this.setState(this.getSetting(SHUFFLING_ENABLED) ?
|
|
|
|
this.shuffledBackRowState() :
|
|
|
|
this.originalState());
|
|
|
|
this.setState({
|
|
|
|
showPopup: false,
|
|
|
|
});
|
2021-01-01 20:03:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
originalState() {
|
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++) {
|
2021-01-02 01:51:28 -05:00
|
|
|
squares.push(new Piece(color, type));
|
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);
|
2021-01-02 01:51:28 -05:00
|
|
|
add(32, EMPTY, EMPTY);
|
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
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
return ({
|
|
|
|
squares,
|
|
|
|
blackIsNext: true,
|
|
|
|
hand: {
|
|
|
|
heldPiece: null,
|
|
|
|
},
|
|
|
|
showPopup: false,
|
2021-01-02 00:46:08 -05:00
|
|
|
settings: {},
|
2021-01-01 20:03:56 -05:00
|
|
|
});
|
2020-12-27 23:58:51 -05:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-02 01:51:28 -05:00
|
|
|
pieceAtIndex(i) {
|
2020-12-30 13:48:53 -05:00
|
|
|
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 {
|
2021-01-02 01:51:28 -05:00
|
|
|
return new Piece(EMPTY, EMPTY);
|
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)) {
|
2021-01-02 01:51:28 -05:00
|
|
|
if(this.pieceAt(x, y).isEmpty()) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y});
|
2020-12-30 10:08:18 -05:00
|
|
|
// Keep searching
|
|
|
|
return 0;
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.isEnemyOf(this.pieceAt(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
|
|
|
|
2021-01-02 01:51:28 -05:00
|
|
|
if (piece.is(PAWN)) {
|
|
|
|
const pieceIsBlack = piece.isBlack();
|
2020-12-30 16:56:57 -05:00
|
|
|
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);
|
2021-01-02 01:51:28 -05:00
|
|
|
if (left && left.passantable && left.isEnemyOf(piece)) {
|
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
|
|
|
}
|
2021-01-02 01:51:28 -05:00
|
|
|
if (right && right.passantable && right.isEnemyOf(piece)) {
|
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
|
|
|
}
|
|
|
|
|
2021-01-02 01:51:28 -05:00
|
|
|
if (this.pieceAt(x, y + shift).isEmpty()) {
|
2020-12-30 13:48:53 -05:00
|
|
|
moves.push({x, y: y + shift});
|
|
|
|
// Pawn moving two spaces becomes en-passantable
|
2021-01-02 01:51:28 -05:00
|
|
|
if (y === startLine && this.pieceAt(x, y + (shift * 2)).isEmpty()) {
|
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 => {
|
2021-01-02 01:51:28 -05:00
|
|
|
const y2 = y + shift;
|
|
|
|
if (this.isValidXY(x, y2) && piece.isEnemyOf(this.pieceAt(x, y2))) {
|
|
|
|
moves.push({x, y: y2});
|
2020-12-28 18:01:05 -05:00
|
|
|
}
|
|
|
|
});
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.is(ROOK)) {
|
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;});
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.is(BISHOP)) {
|
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;});
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.is(QUEEN)) {
|
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));
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.is(KNIGHT)) {
|
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]));
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (piece.is(KING)) {
|
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]));
|
2021-01-02 01:51:28 -05:00
|
|
|
if (piece.hasntMoved()) {
|
2020-12-30 22:50:07 -05:00
|
|
|
const kingIndex = this.findIndex(piece);
|
|
|
|
const [x, y] = this.getXandY(kingIndex);
|
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
let leftRook = this.pieceAt(0, y);
|
2021-01-02 01:51:28 -05:00
|
|
|
if(leftRook.is(ROOK) && leftRook.hasntMoved()) {
|
2020-12-30 16:56:57 -05:00
|
|
|
// Check if spaces between rook and king are empty
|
2021-01-02 01:51:28 -05:00
|
|
|
if(this.pieceAt(1, y).isEmpty() &&
|
|
|
|
this.pieceAt(2, y).isEmpty() &&
|
|
|
|
this.pieceAt(3, y).isEmpty()) {
|
2020-12-30 22:50:07 -05:00
|
|
|
// Check if between space puts king in check
|
|
|
|
let board = this.clone();
|
|
|
|
board.state.squares[board.getIndex(x - 1, y)] = piece;
|
2021-01-02 01:51:28 -05:00
|
|
|
board.state.squares[kingIndex].isEmpty();
|
2020-12-30 22:50:07 -05:00
|
|
|
if(board.inCheck(piece) == null) {
|
|
|
|
moves.push({x: x - 2, y, castle: [x - 1, y]});
|
|
|
|
}
|
|
|
|
}
|
2020-12-30 16:56:57 -05:00
|
|
|
}
|
2020-12-30 22:50:07 -05:00
|
|
|
|
2020-12-30 16:56:57 -05:00
|
|
|
let rightRook = this.pieceAt(7, y);
|
2021-01-02 01:51:28 -05:00
|
|
|
if(rightRook.is(ROOK) && rightRook.hasntMoved()) {
|
2020-12-30 16:56:57 -05:00
|
|
|
// Check if spaces between rook and king are empty
|
2021-01-02 01:51:28 -05:00
|
|
|
if(this.pieceAt(5, y).isEmpty() &&
|
|
|
|
this.pieceAt(6, y).isEmpty()) {
|
2020-12-30 22:50:07 -05:00
|
|
|
// Check if between space puts king in check
|
|
|
|
let board = this.clone();
|
|
|
|
board.state.squares[board.getIndex(x + 1, y)] = piece;
|
2021-01-02 01:51:28 -05:00
|
|
|
board.state.squares[kingIndex].isEmpty();
|
2020-12-30 22:50:07 -05:00
|
|
|
if(board.inCheck(piece) == null) {
|
|
|
|
moves.push({x: x + 2, y, castle: [x + 1, y]});
|
|
|
|
}
|
|
|
|
}
|
2020-12-30 16:56:57 -05:00
|
|
|
}
|
|
|
|
}
|
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];
|
2021-01-02 01:51:28 -05:00
|
|
|
if(check.type === piece.type && check.color === piece.color) {
|
2020-12-29 16:38:52 -05:00
|
|
|
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++) {
|
2021-01-02 01:51:28 -05:00
|
|
|
if(piece.isEnemyOf(this.pieceAtIndex(i))) {
|
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++) {
|
2021-01-02 01:51:28 -05:00
|
|
|
const piece = this.pieceAtIndex(i);
|
2020-12-29 08:25:17 -05:00
|
|
|
// If that piece is on the checked team
|
2021-01-02 01:51:28 -05:00
|
|
|
if (piece != null && piece.isFriendOf(checkedKing)) {
|
2020-12-29 08:25:17 -05:00
|
|
|
// 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];
|
2021-01-02 01:51:28 -05:00
|
|
|
board.state.squares[i].isEmpty();
|
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
|
|
|
|
2021-01-02 01:51:28 -05:00
|
|
|
const piece = this.pieceAtIndex(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() {
|
2021-01-01 20:03:56 -05:00
|
|
|
return (this.state && this.state.hand) ? this.state.hand.heldPiece : null;
|
2020-12-28 15:12:35 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-01-02 01:51:28 -05:00
|
|
|
squares[this.getIndex(move.passant.x, move.passant.y)] = new Piece(EMPTY, EMPTY);
|
2020-12-30 09:49:29 -05:00
|
|
|
}
|
2020-12-30 22:50:07 -05:00
|
|
|
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])];
|
2021-01-02 01:51:28 -05:00
|
|
|
squares[this.getIndex(rookX, move.castle[1])] = new Piece(EMPTY, EMPTY);
|
2020-12-30 22:50:07 -05:00
|
|
|
}
|
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];
|
2021-01-02 01:51:28 -05:00
|
|
|
squares[from] = new Piece(EMPTY, EMPTY);
|
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()];
|
2021-01-02 01:51:28 -05:00
|
|
|
board.state.squares[this.heldPiece()] = new Piece(EMPTY, EMPTY);
|
2020-12-30 00:09:02 -05:00
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2021-01-02 01:51:28 -05:00
|
|
|
} else if (this.state.squares[i].isFull()) {
|
|
|
|
const isSquareBlack = this.state.squares[i].isBlack();
|
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
|
|
|
}
|
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
togglePopup() {
|
|
|
|
this.setState({
|
|
|
|
showPopup: !this.state.showPopup
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-02 00:46:08 -05:00
|
|
|
setSetting(name, value) {
|
|
|
|
let settings = this.state.settings;
|
|
|
|
settings[name] = value;
|
|
|
|
this.setState({
|
|
|
|
settings
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSetting(name) {
|
|
|
|
return this.state.settings[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSetting(name) {
|
|
|
|
console.log("toggle " + settingText(name));
|
|
|
|
let settings = this.state.settings;
|
|
|
|
settings[name] = !settings[name];
|
|
|
|
console.log(settings[name]);
|
|
|
|
this.setState({
|
|
|
|
settings: settings,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:03:56 -05:00
|
|
|
renderPopup() {
|
|
|
|
return (this.state.showPopup ?
|
2021-01-02 00:46:08 -05:00
|
|
|
<Popup
|
|
|
|
header='QuickChess'
|
|
|
|
closePopup={this.togglePopup.bind(this)}
|
|
|
|
body={<div>
|
|
|
|
|
|
|
|
<p>This is a simple implementation of the classic board game,
|
|
|
|
implemented in React. It supports all possible moves, including
|
|
|
|
castling, and <em>en passant</em>.</p>
|
|
|
|
|
|
|
|
{
|
|
|
|
getAllSettings().map(setting => {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button onClick={this.toggleSetting.bind(this, setting)}>
|
|
|
|
{
|
|
|
|
settingText(setting) +
|
|
|
|
(this.getSetting(setting) ? " On" : " Off")
|
|
|
|
}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
2021-01-01 20:03:56 -05:00
|
|
|
|
2021-01-02 00:46:08 -05:00
|
|
|
<button onClick={this.doReset.bind(this)}>Reset Game</button>
|
2021-01-01 20:03:56 -05:00
|
|
|
|
|
|
|
</div>}
|
|
|
|
/>
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Board class can't (always) include a settings button if used as a demo. */
|
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`,}}>
|
2021-01-01 20:03:56 -05:00
|
|
|
<div className="status">
|
|
|
|
<h1 style={{display: "inline-block"}}>{status}</h1>
|
2021-01-02 00:46:08 -05:00
|
|
|
<img
|
|
|
|
className="icon"
|
|
|
|
alt="Settings icon: a gear"
|
|
|
|
style={{height: "5vh", float: "right"}}
|
|
|
|
src="./gear.svg" onClick={this.togglePopup.bind(this)}>
|
|
|
|
</img>
|
2021-01-01 20:03:56 -05:00
|
|
|
</div>
|
2020-12-30 16:56:57 -05:00
|
|
|
{range(8).map(n => this.row(n))}
|
2021-01-01 20:03:56 -05:00
|
|
|
{this.renderPopup()}
|
2020-12-27 20:11:50 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Game extends React.Component {
|
2021-01-01 20:03:56 -05:00
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = { showPopup: false };
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:11:50 -05:00
|
|
|
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')
|
|
|
|
);
|