2020-12-27 20:11:50 -05:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import './index.css';
|
|
|
|
|
2020-12-27 23:58:51 -05:00
|
|
|
const Pieces = {
|
|
|
|
WhitePawn: './white_pawn.png',
|
|
|
|
WhiteRook: './white_rook.png',
|
|
|
|
WhiteKnight: './white_knight.png',
|
|
|
|
WhiteBishop: './white_bishop.png',
|
|
|
|
WhiteQueen: './white_queen.png',
|
|
|
|
WhiteKing: './white_king.png',
|
|
|
|
|
|
|
|
BlackPawn: './black_pawn.png',
|
|
|
|
BlackRook: './black_rook.png',
|
|
|
|
BlackKnight: './black_knight.png',
|
|
|
|
BlackBishop: './black_bishop.png',
|
|
|
|
BlackQueen: './black_queen.png',
|
|
|
|
BlackKing: './black_king.png',
|
|
|
|
};
|
|
|
|
|
|
|
|
function Square(props) {
|
|
|
|
let bg = {
|
|
|
|
backgroundImage: `url(${props.value})`,
|
|
|
|
// backgroundColor: "lightblue",
|
|
|
|
};
|
|
|
|
if (props.value != null) {
|
2020-12-27 20:11:50 -05:00
|
|
|
return (
|
2020-12-27 23:58:51 -05:00
|
|
|
<button
|
|
|
|
className="square"
|
|
|
|
onClick={props.onClick}
|
|
|
|
style={bg}
|
|
|
|
>
|
2020-12-27 20:11:50 -05:00
|
|
|
</button>
|
|
|
|
);
|
2020-12-27 23:58:51 -05:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="square"
|
|
|
|
onClick={props.onClick}
|
|
|
|
></button>
|
|
|
|
);
|
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(),
|
|
|
|
xIsNext: true,
|
|
|
|
};
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
let whiteRow = [
|
|
|
|
Pieces.WhiteRook,
|
|
|
|
Pieces.WhiteKnight,
|
|
|
|
Pieces.WhiteBishop,
|
|
|
|
Pieces.WhiteQueen,
|
|
|
|
Pieces.WhiteKing,
|
|
|
|
Pieces.WhiteBishop,
|
|
|
|
Pieces.WhiteKnight,
|
|
|
|
Pieces.WhiteRook
|
|
|
|
];
|
|
|
|
let blackRow = [
|
|
|
|
Pieces.BlackRook,
|
|
|
|
Pieces.BlackKnight,
|
|
|
|
Pieces.BlackBishop,
|
|
|
|
Pieces.BlackQueen,
|
|
|
|
Pieces.BlackKing,
|
|
|
|
Pieces.BlackBishop,
|
|
|
|
Pieces.BlackKnight,
|
|
|
|
Pieces.BlackRook
|
|
|
|
];
|
|
|
|
let squares = whiteRow.slice();
|
|
|
|
squares = squares.concat(Array(8).fill(Pieces.WhitePawn));
|
|
|
|
squares = squares.concat(Array(32).fill(null));
|
|
|
|
squares = squares.concat(Array(8).fill(Pieces.BlackPawn));
|
|
|
|
squares = squares.concat(blackRow.slice());
|
|
|
|
return squares;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(i) {
|
|
|
|
const squares = this.state.squares.slice();
|
|
|
|
if (calculateWinner(squares) || squares[i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
squares[i] = this.state.xIsNext ? 'Black' : 'White';
|
|
|
|
this.setState({
|
|
|
|
squares: squares,
|
|
|
|
xIsNext: !this.state.xIsNext,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:11:50 -05:00
|
|
|
renderSquare(i) {
|
2020-12-27 23:58:51 -05:00
|
|
|
return (
|
|
|
|
<Square
|
|
|
|
value={this.state.squares[i]}
|
|
|
|
onClick={() => this.handleClick(i)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-27 23:58:51 -05:00
|
|
|
const winner = calculateWinner(this.state.squares);
|
|
|
|
let status;
|
|
|
|
if (winner) {
|
|
|
|
status = 'Winner: ' + winner;
|
|
|
|
} else {
|
|
|
|
status = 'Next player: ' + (this.state.xIsNext ? 'Black' : 'White');
|
|
|
|
}
|
2020-12-27 20:11:50 -05:00
|
|
|
|
2020-12-27 23:58:51 -05:00
|
|
|
var texttext =
|
2020-12-27 20:11:50 -05:00
|
|
|
<div>
|
|
|
|
<div className="status">{status}</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-board">
|
|
|
|
<Board />
|
|
|
|
</div>
|
|
|
|
<div className="game-info">
|
|
|
|
<div>{/* status */}</div>
|
|
|
|
<ol>{/* TODO */}</ol>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<Game />,
|
|
|
|
document.getElementById('root')
|
|
|
|
);
|
2020-12-27 23:58:51 -05:00
|
|
|
|
|
|
|
function calculateWinner(squares) {
|
|
|
|
const lines = [
|
|
|
|
[0, 1, 2],
|
|
|
|
[3, 4, 5],
|
|
|
|
[6, 7, 8],
|
|
|
|
[0, 3, 6],
|
|
|
|
[1, 4, 7],
|
|
|
|
[2, 5, 8],
|
|
|
|
[0, 4, 8],
|
|
|
|
[2, 4, 6],
|
|
|
|
];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
const [a, b, c] = lines[i];
|
|
|
|
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
|
|
|
|
return squares[a];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|