index.js 4.92 KB
Newer Older
mayi's avatar
jinziqi  
mayi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";

//1.1.1
function Square(props) {
  // constructor(props) {
  //   super(props);
  //   this.state = {
  //     value: null,
  //   };
  // }

  return (
    <button className="square" onClick={props.onclick}>
      <font color={props.color}>{props.value}</font>
    </button>
  );
}

//1.1
class Board extends React.Component {
  // constructor(props) {
  //   super(props);
  //   this.state = {
  //     squares: Array(9).fill(null),
  //     XIsNext: true,
  //   };
  // }

  renderSquare(i) {
    let color;
    if (this.props.lines && this.props.lines.includes(i)) {
      color = "red";
    } else {
      color = "black";
    }

    return (
      <Square
        color={color}
        value={this.props.squares[i]}
        onclick={() => this.props.onclick(i)}
        key={i}
      />
    );
  }

  render() {
    return (
      <div>
        {Array(3)
          .fill(null)
          .map((item1, i) => {
            return (
              <div className="board-row" key={i}>
                {Array(3)
                  .fill(null)
                  .map((item2, j) => {
                    return this.renderSquare(3 * i + j);
                  })}
              </div>
            );
          })}
      </div>
    );
  }
}
//1
class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null),
          x: 0,
          y: 0,
        },
      ],
      stepNumber: 0,
      XIsNext: true,
      change: 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();
    // console.log(i,"i")
    if (calculateWinner(squares).winner || squares[i]) {
      return;
    }
    squares[i] = this.state.XIsNext ? "X" : "O";

    this.setState({
      history: history.concat([
        {
          squares: squares,
          x: Math.floor(i / 3),
          y: i % 3,
        },
      ]),
      stepNumber: history.length,
      XIsNext: !this.state.XIsNext,
    });
  }
  jumpTo(step) {
    this.setState({
      stepNumber: step,
      XIsNext: step % 2 === 0,
    });
  }
  changeDesc() {
    this.setState({
      change: !this.state.change,
    });
    console.log(this.state.change);
  }
  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    // const winner = calculateWinner(current.squares);
    const result = calculateWinner(current.squares);
    const winner = result.winner;
    // console.log(winner,'winner')
    const lines = result.winnerlines;
    // console.log(lines,'1lines23')
    const change = this.state.change;
    var moves = history.map((step, move) => {
      const desc = move ? "Go to move #" + move : "Go to game start";
      const blod = this.state.stepNumber === move ? "blod" : "";
      return (
        <li key={move}>
          <button onClick={() => this.jumpTo(move)} className={blod}>
            {desc}({step.x + 1}:{step.y + 1})
          </button>
        </li>
      );
    });
    // console.log(moves, "moves");
    let status;
    let changeDes;
    //  console.log(moves.length,'moves.length') 最长10格  也就是
    if (moves.length - 1 === 9 && typeof lines == "undefined") {
      moves.push('平局')
    
    }
    if (change) {
      changeDes = "升序";
    } else {
      changeDes = "降序";
      moves = moves.reverse();
    }

    if (winner) {
      status = "winner" + winner;
    } else {
      status = "Next player :" + (this.state.XIsNext ? "X" : "O");
    }
    return (
      <div className="game">
        <div className="game-board">
          <Board
            lines={lines}
            squares={current.squares}
            onclick={(i) => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <button onClick={() => this.changeDesc()}>{changeDes}</button>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
}
// 1 从calculateWinner拿到谁赢了  且赢的坐标
// 2 把值给调用calculateWinner的地方,并且修改。
// 3 给出条件何时棋子变红,也就是 胜利的坐标出现并且包含在记录里面
// 4 观察哪地方渲染的棋子,然后给颜色即可
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]) {
      // console.log(lines[i], "winnerlines");
      return { winner: squares[a], winnerlines: lines[i] };
    }
  }
  return { winner: null, lines: null };
}
// ========================================

ReactDOM.render(<Game />, document.getElementById("root"));