import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; 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) { return ( ); } else { return ( ); } } class Board extends React.Component { 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, }); } renderSquare(i) { return ( this.handleClick(i)} /> ); } row(row) { let i = row * 8; return (
{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)}
); } render() { const winner = calculateWinner(this.state.squares); let status; if (winner) { status = 'Winner: ' + winner; } else { status = 'Next player: ' + (this.state.xIsNext ? 'Black' : 'White'); } var texttext =
{status}
{this.row(0)} {this.row(1)} {this.row(2)} {this.row(3)} {this.row(4)} {this.row(5)} {this.row(6)} {this.row(7)}
; return ( texttext ); } } class Game extends React.Component { render() { return (
{/* status */}
    {/* TODO */}
); } } // ======================================== ReactDOM.render( , document.getElementById('root') ); 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; }