From d0a30ceec98db08d140c83b52dc596d1472bb97f Mon Sep 17 00:00:00 2001 From: Dathan Huang Date: Thu, 14 Sep 2017 13:09:22 -0400 Subject: [PATCH 1/2] Update index.js --- react-fb-tictactoe/src/TicTacToe/index.js | 112 +++++++++++++++++++--- 1 file changed, 97 insertions(+), 15 deletions(-) diff --git a/react-fb-tictactoe/src/TicTacToe/index.js b/react-fb-tictactoe/src/TicTacToe/index.js index 56944d57..1995b9c0 100644 --- a/react-fb-tictactoe/src/TicTacToe/index.js +++ b/react-fb-tictactoe/src/TicTacToe/index.js @@ -9,27 +9,26 @@ import './TicTacToe.css' * with * class Square extends Component */ -class Square extends Component { - render() { - return ( - - ); - } +function Square(props){ + return ( + + ); } class Board extends Component { + renderSquare(i) { - return ; + return ( this.props.onClick(i)} + +/>); } render() { - const status = 'Next player: X'; - return (
-
{status}
{this.renderSquare(0)} {this.renderSquare(1)} @@ -51,19 +50,102 @@ class Board extends Component { } class Game extends Component { + constructor() { + super(); + this.state = { + history: [{ + squares: Array(9).fill(null), + } + ], + stepNumber: 0, + xIsNext: true, + }; +} + + + handleClick(i){ + const history = this.state.history.slice(0, this.state.stepNumber + 1); + const current = history[history.length - 1]; + const squares = current.squares.slice(); + if(calculateWinner(squares) || squares[i]) { + return; + } + squares[i] = this.state.xIsNext ? 'X' : 'O'; + this.setState({ + history: history.concat([{ + squares: squares, + }]), + stepNumber: history.length, + xIsNext: !this.state.xIsNext, + }); +} + + jumpTo(step){ + this.setState({ + stepNumber: step, + xIsNext: (step %2) === 0 + } + ); + } render() { + const history = this.state.history; + const current = history[this.state.stepNumber]; + const winner = calculateWinner(current.squares); + + const moves = history.map((step, move) => { + const desc = move ? + 'Move #' + move : + 'Game start' ; + return ( +
  • + this.jumpTo(move)}> {desc} +
  • + ); + }); + + let status; + if(winner) { + status = 'Winner: ' + winner; + } + else { + + status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O'); + } return (
    - + this.handleClick(i)} + />
    -
    {/* status */}
    -
      {/* todo */}
    +
    {status}
    +
      {moves}
    ); } } +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; +} + export default Game; From 8ad5aaacabb4dda3cdd87a011d2361955d5a6126 Mon Sep 17 00:00:00 2001 From: Dathan Huang Date: Thu, 21 Sep 2017 11:22:48 -0400 Subject: [PATCH 2/2] Update index.js --- react-fb-tictactoe/src/index.js | 77 +++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/react-fb-tictactoe/src/index.js b/react-fb-tictactoe/src/index.js index fae3e350..886add93 100644 --- a/react-fb-tictactoe/src/index.js +++ b/react-fb-tictactoe/src/index.js @@ -1,8 +1,69 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; -import './index.css'; -import App from './App'; -import registerServiceWorker from './registerServiceWorker'; - -ReactDOM.render(, document.getElementById('root')); -registerServiceWorker(); +import React, { Component } from 'react'; +import './TicTacToe.css' + + +/* + * Changes from original tutorial + * - Here we import Component, so we can replace + * class Square extends React.Component + * with + * class Square extends Component + */ +class Square extends Component { + render() { + return ( + + ); + } +} + +class Board extends Component { + renderSquare(i) { + return ; + } + + render() { + const status = 'Next player: X'; + + return ( +
    +
    {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)} +
    +
    + ); + } +} + +class Game extends Component { + render() { + return ( +
    +
    + +
    +
    +
    {/* status */}
    +
      {/* todo */}
    +
    +
    + ); + } +} + +export default Game;