react井字棋的逻辑

井字棋的逻辑

页面会渲染一次

成功以后不在页面输出内容,那么就要加一个方法,每次点击按钮的时候调用该方法
然后判断页面是否成功(这个方法要把所有成功的数组列举出来,然后进行判断我点击操作的索引值是否和成功案例的数组是否对应),如果成功返回一个成功的一方的标识。
然后成功以后我点击返回的总是成功一方的标识,我让它的状态是不变的。

但是每次点击都还是会触发代码进行判断。只是返回的状态没有修改所以没有添加内容。

每次页面的重新渲染都是通过点击事件改变states来进行操作的。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <style>
      body {
        font: 14px "Century Gothic", Futura, sans-serif;
        margin: 20px;
      }

      ol,
      ul {
        padding-left: 30px;
      }

      .board-row:after {
        clear: both;
        content: "";
        display: table;
      }

      .status {
        margin-bottom: 10px;
      }

      .square {
        background: red;
        border: 1px solid #999;
        float: left;
        font-size: 24px;
        font-weight: bold;
        line-height: 34px;
        height: 34px;
        margin-right: -1px;
        margin-top: -1px;
        padding: 0;
        text-align: center;
        width: 34px;
      }

      .square:focus {
        outline: none;
      }

      .kbd-navigation .square:focus {
        background: #ddd;
      }

      .game {
        display: flex;
        flex-direction: row;
      }

      .game-info {
        margin-left: 20px;
      }
    </style>
  </head>
  <body>
    <div id="root"></div>

    <script type="text/babel">
      function Square(props) {
        // console.log("haha");
        return (
          <button className="square" onClick={props.onClick}>
            {props.value}
          </button>
        );
      }

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

        handleClick(i) {
          //   console.log("heihei");
          console.log(i);
          const squares = this.state.squares.slice();
          squares[i] = this.state.xIsNext ? "X" : "O";
          this.setState({
            squares: squares,
            xIsNext: !this.state.xIsNext,
          });
        }

        renderSquare(i) {
          return (
            <Square
              value={this.state.squares[i]}
              onClick={() => this.handleClick(i)}
            />
          );
        }

        render() {
          const status = "Next player: " + (this.state.xIsNext ? "X" : "O");

          return (
            <div>
              <div className="status">{status}</div>
              <div className="board-row">
                <Square
                  onClick={() => this.handleClick(0)}
                  value={this.state.squares[0]}
                />
                <Square
                  onClick={() => this.handleClick(1)}
                  value={this.state.squares[1]}
                />
                <Square onClick={() => this.handleClick(2)} />
              </div>
              <div className="board-row">
                {this.renderSquare(3)}
                {this.renderSquare(4)}
                {this.renderSquare(5)}
              </div>
              <div className="board-row">
                {this.renderSquare(6)}
                {this.renderSquare(7)}
                {this.renderSquare(8)}
              </div>
            </div>
          );
        }
      }

      class Game extends React.Component {
        render() {
          return (
            <div className="game">
              <div className="game-board">
                <Board />
              </div>
              <div className="game-info">
                <div>{/* status */}</div>
                <ol>{/* TODO */}</ol>
              </div>
            </div>
          );
        }
      }

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

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

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React 实例</title>
    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <style>
      body {
        font: 14px "Century Gothic", Futura, sans-serif;
        margin: 20px;
      }

      ol,
      ul {
        padding-left: 30px;
      }

      .board-row:after {
        clear: both;
        content: "";
        display: table;
      }

      .status {
        margin-bottom: 10px;
      }

      .square {
        background: red;
        border: 1px solid #999;
        float: left;
        font-size: 24px;
        font-weight: bold;
        line-height: 34px;
        height: 34px;
        margin-right: -1px;
        margin-top: -1px;
        padding: 0;
        text-align: center;
        width: 34px;
      }

      .square:focus {
        outline: none;
      }

      .kbd-navigation .square:focus {
        background: #ddd;
      }

      .game {
        display: flex;
        flex-direction: row;
      }

      .game-info {
        margin-left: 20px;
      }
    </style>
  </head>
  <body>
    <div id="root"></div>

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

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

        handleClick(i) {
          const squares = this.state.squares.slice();
          if (calculateWinner(squares) || squares[i]) {
            return;
          }
          squares[i] = this.state.xIsNext ? "X" : "O";
          this.setState({
            squares: squares,
            xIsNext: !this.state.xIsNext,
          });
        }

        renderSquare(i) {
          return (
            <Square
              value={this.state.squares[i]}
              onClick={() => this.handleClick(i)}
            />
          );
        }

        render() {
          const winner = calculateWinner(this.state.squares);
          //   console.log(winner);
          let status;
          if (winner) {
            status = "Winner: " + winner;
          } else {
            status = "Next player: " + (this.state.xIsNext ? "X" : "O");
          }

          return (
            <div>
              <div className="status">{status}</div>
              <div className="board-row">
                {this.renderSquare(0)}
                {this.renderSquare(1)}
                {this.renderSquare(2)}
              </div>
              <div className="board-row">
                {this.renderSquare(3)}
                {this.renderSquare(4)}
                {this.renderSquare(5)}
              </div>
              <div className="board-row">
                {this.renderSquare(6)}
                {this.renderSquare(7)}
                {this.renderSquare(8)}
              </div>
            </div>
          );
        }
      }

      class Game extends React.Component {
        render() {
          return (
            <div className="game">
              <div className="game-board">
                <Board />
              </div>
              <div className="game-info">
                <div>{/* status */}</div>
                <ol>{/* TODO */}</ol>
              </div>
            </div>
          );
        }
      }

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

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

      function calculateWinner(squares) {
        // console.log(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];
          console.log("haha");
          if (
            squares[a] &&
            squares[a] === squares[b] &&
            squares[a] === squares[c]
          ) {
            // 赢了的时候才会进入这个方法
            console.log("haha");
            return squares[a];
          }
        }
        return null;
      }
    </script>
  </body>
</html>

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值