window.game.receiveMove(square);
});
+events.addEventListener("game-state", event => {
+ const state = JSON.parse(event.data);
+
+ window.game.resetState();
+
+ for (let square of state.moves) {
+ window.game.receiveMove(square);
+ }
+});
+
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
return fetch_post_json("move", { square: i });
}
+ resetState() {
+ this.setState({
+ history: [
+ {
+ squares: Array(9).fill(null)
+ }
+ ],
+ stepNumber: 0,
+ xIsNext: true
+ });
+ }
+
receiveMove(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
- const moves = history.map((step, move) => {
- const desc = move ?
- 'Go to move #' + move :
- 'Go to game start';
- return (
- <li key={move}>
- <button onClick={() => this.jumpTo(move)}>{desc}</button>
- </li>
- );
- });
-
let status;
if (winner) {
status = "Winner: " + winner;
return (
<div className="game">
+ <div className="game-info">
+ <div>{status}</div>
+ </div>
<div className="game-board">
<Board
squares={current.squares}
onClick={i => this.handleClick(i)}
/>
</div>
- <div className="game-info">
- <div>{status}</div>
- <ol>{moves}</ol>
- </div>
</div>
);
}