diff --git a/public/black_bishop.png b/public/black_bishop.png new file mode 100644 index 0000000..89a4628 Binary files /dev/null and b/public/black_bishop.png differ diff --git a/public/black_king.png b/public/black_king.png new file mode 100644 index 0000000..5a23105 Binary files /dev/null and b/public/black_king.png differ diff --git a/public/black_knight.png b/public/black_knight.png new file mode 100644 index 0000000..009f43d Binary files /dev/null and b/public/black_knight.png differ diff --git a/public/black_pawn.png b/public/black_pawn.png new file mode 100644 index 0000000..c597a8f Binary files /dev/null and b/public/black_pawn.png differ diff --git a/public/black_queen.png b/public/black_queen.png new file mode 100644 index 0000000..fa7121a Binary files /dev/null and b/public/black_queen.png differ diff --git a/public/black_rook.png b/public/black_rook.png new file mode 100644 index 0000000..ca16e6b Binary files /dev/null and b/public/black_rook.png differ diff --git a/public/white_bishop.png b/public/white_bishop.png new file mode 100644 index 0000000..2b6aae8 Binary files /dev/null and b/public/white_bishop.png differ diff --git a/public/white_king.png b/public/white_king.png new file mode 100644 index 0000000..513ac42 Binary files /dev/null and b/public/white_king.png differ diff --git a/public/white_knight.png b/public/white_knight.png new file mode 100644 index 0000000..433b989 Binary files /dev/null and b/public/white_knight.png differ diff --git a/public/white_pawn.png b/public/white_pawn.png new file mode 100644 index 0000000..bf78a95 Binary files /dev/null and b/public/white_pawn.png differ diff --git a/public/white_queen.png b/public/white_queen.png new file mode 100644 index 0000000..ad10025 Binary files /dev/null and b/public/white_queen.png differ diff --git a/public/white_rook.png b/public/white_rook.png new file mode 100644 index 0000000..743648b Binary files /dev/null and b/public/white_rook.png differ diff --git a/src/index.css b/src/index.css index 79d5fee..f29dd1e 100644 --- a/src/index.css +++ b/src/index.css @@ -23,16 +23,22 @@ ol, ul { float: left; font-size: 24px; font-weight: bold; - line-height: 34px; - height: 34px; + line-height: 45px; + height: 45px; margin-right: -1px; margin-top: -1px; padding: 0; text-align: center; - width: 34px; + width: 45px; +} + +.square:hover { + background: lightblue; + transition: 50ms linear; } .square:focus { + background: #8d98e6; outline: none; } diff --git a/src/index.js b/src/index.js index 1ac8ebd..28f481b 100644 --- a/src/index.js +++ b/src/index.js @@ -2,43 +2,147 @@ import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; -class Square extends React.Component { - render() { +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 ; + 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 status = 'Next player: X'; + const winner = calculateWinner(this.state.squares); + let status; + if (winner) { + status = 'Winner: ' + winner; + } else { + status = 'Next player: ' + (this.state.xIsNext ? 'Black' : 'White'); + } - return ( + var texttext =
{status}
-
- {this.renderSquare(0)} - {this.renderSquare(1)} - {this.renderSquare(2)} -
-
- {this.renderSquare(3)} - {this.renderSquare(4)} - {this.renderSquare(5)} -
-
- {this.renderSquare(6)} - {this.renderSquare(7)} - {this.renderSquare(8)} -
+ {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 ); } } @@ -65,3 +169,23 @@ 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; +}