import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

function Square(props) {
  return (
    <button className="square" onClick={() => props.onClick()}>
      {props.value}
    </button>
  );
}

function Row(props) {
  return (
    <div className="board-row">
      <Square value={props.squares[props.rowNum * 3]} onClick={() => props.onClick(props.rowNum * 3)} />
      <Square value={props.squares[props.rowNum * 3 + 1]} onClick={() => props.onClick(props.rowNum * 3 + 1)} />
      <Square value={props.squares[props.rowNum * 3 + 2]} onClick={() => props.onClick(props.rowNum * 3 + 2)} />
    </div>
  )
}

function Board(props) {
  return (
    <div>
      <Row squares={props.squares} onClick={props.onClick} rowNum={0} />
      <Row squares={props.squares} onClick={props.onClick} rowNum={1} />
      <Row squares={props.squares} onClick={props.onClick} rowNum={2} />
    </div>
  );
}

class Game extends React.Component {
  constructor() {
    super();
    this.state = {
      history: [{
        squares: Array(9).fill(null)
      }],
      xIsNext: true,
      stepNumber: 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 ' + moveLocation(step.squareNum) :
        'Game start';
      if (move === this.state.stepNumber) {
        return (
          <li key={move}>
            <b><a href="#" onClick={() => this.jumpTo(move)}>{desc}</a></b>
          </li>
        );
      } else {
        return (
          <li key={move}>
            <a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
          </li>
        );
      }
    });

    let status;
    if (winner) {
        status = 'Winner: ' + winner;
    } else {
        status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }
    return (
      <div className="game">
        <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>
    );
  }
  handleClick(i) {
    const stepNumber = this.state.stepNumber;
    const history = this.state.history.slice(0, 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,
        squareNum: i,
      }]),
      stepNumber: history.length,
      xIsNext: !this.state.xIsNext,
    });
  }
  jumpTo(step) {
    this.setState({
      stepNumber: step,
      xIsNext: (step % 2) ? false : true,
    });
  }
}

// ========================================

ReactDOM.render(
  <Game />,
  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;
}

function moveLocation(squareNum) {
  return "(" + row(squareNum) + ", " + col(squareNum) + ")"
}

function row(squareNum) {
  return Math.floor(squareNum / 3)
}

function col(squareNum) {
  return squareNum % 3
}

@@ -233,32 +233,70 @@
 on Row(props) %7B%0A
+  const squareNum = props.rowNum * 3;%0A
   return (%0A    %3C
@@ -357,32 +357,25 @@
 squares%5B
-props.rowNum * 3
+squareNum
 %5D%7D onCli
@@ -398,32 +398,25 @@
 onClick(
-props.rowNum * 3
+squareNum
 )%7D /%3E%0A  
@@ -448,32 +448,25 @@
 squares%5B
-props.rowNum * 3
+squareNum
  + 1%5D%7D o
@@ -493,32 +493,25 @@
 onClick(
-props.rowNum * 3
+squareNum
  + 1)%7D /
@@ -547,32 +547,25 @@
 squares%5B
-props.rowNum * 3
+squareNum
  + 2%5D%7D o
@@ -592,32 +592,25 @@
 onClick(
-props.rowNum * 3
+squareNum
  + 2)%7D /

Extract squareNum