S
S
Sergey2021-11-07 19:52:18
React Native
Sergey, 2021-11-07 19:52:18

How to determine the winner of tic-tac-toe in React Native?

I decided to start learning React Native here and transfer tic-tac-toe from the usual react to native.
Source:

function Square(props) {
  return (
    <Pressable style={styles.button} onPress={props.onPress}>
      {props.value}
    </Pressable>
  );
}

class Board extends React.Component {
  renderSquare(i) {
    return (
      <Square
        value={this.props.squares[i]}
        onPress={() => this.props.onPress(i)}
      />
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <StatusBar style="auto" />
        <Pressable style={styles.boardRow}>
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </Pressable>
        <Pressable style={styles.boardRow}>
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </Pressable>
        <Pressable style={styles.boardRow}>
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </Pressable>
      </View>
    );
  }
}

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

  handlePress(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,
    });
  }

  jumpTo(squares) {
    this.setState({
      squares: Array(9).fill(null),
      xIsNext: true,
    });
  }

  render() {
    const winner = calculateWinner(this.state.squares);

    let status;
    if (winner) {
      status = 'Выиграл: ' + winner;
    } else {
      status = 'Следующий ход: ' + (this.state.xIsNext ? 'X' : 'O');
    }

    return (
      <View className="game">
        <View className="game-board">
          <Board
            squares={this.state.squares}
            onPress={(i) => this.handlePress(i)}
          />
        </View>
        <View style={styles.menu}>
          <Text style={styles.statusText}>{status}</Text>
          <Pressable style={styles.backButton} onPress={() => this.jumpTo()}>
            <Text style={styles.textButton}> 'К началу игры' </Text>
          </Pressable>
        </View>
      </View>
    );
  }
}

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;
}

const styles = StyleSheet.create({
});

export default Game;


When I try to run it on my phone, it gives an error - Text strings must be rendered within a component.
Corrected a piece (tagged X and O):
handlePress(i) {
    const squares = this.state.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? 
    <Text style={styles.gameSquares}>X</Text> : <Text style={styles.gameSquares}>O</Text>;
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }

After that, the game has ceased to determine the winner and continues indefinitely. The problem is clear, but how to fix it - not so much.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question