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 ( ); } //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 ( this.props.onclick(i)} key={i} /> ); } render() { return (
{Array(3) .fill(null) .map((item1, i) => { return (
{Array(3) .fill(null) .map((item2, j) => { return this.renderSquare(3 * i + j); })}
); })}
); } } //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 (
  • ); }); // 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 (
    this.handleClick(i)} />
    {status}
      {moves}
    ); } } // 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(, document.getElementById("root"));