tic tac toe react

Solutions on MaxInterview for tic tac toe react by the best coders in the world

showing results for - "tic tac toe react"
Louison
01 Jan 2021
1function Square(props) {
2  return (
3    <button className="square" onClick={props.onClick}>
4      {props.value}
5    </button>
6  );
7}
8
9class Board extends React.Component {
10  renderSquare(i) {
11    return (
12      <Square
13        value={this.props.squares[i]}
14        onClick={() => this.props.onClick(i)}
15      />
16    );
17  }
18
19  render() {
20    return (
21      <div>
22        <div className="board-row">
23          {this.renderSquare(0)}
24          {this.renderSquare(1)}
25          {this.renderSquare(2)}
26        </div>
27        <div className="board-row">
28          {this.renderSquare(3)}
29          {this.renderSquare(4)}
30          {this.renderSquare(5)}
31        </div>
32        <div className="board-row">
33          {this.renderSquare(6)}
34          {this.renderSquare(7)}
35          {this.renderSquare(8)}
36        </div>
37      </div>
38    );
39  }
40}
41
42class Game extends React.Component {
43  constructor(props) {
44    super(props);
45    this.state = {
46      history: [
47        {
48          squares: Array(9).fill(null)
49        }
50      ],
51      stepNumber: 0,
52      xIsNext: true
53    };
54  }
55
56  handleClick(i) {
57    const history = this.state.history.slice(0, this.state.stepNumber + 1);
58    const current = history[history.length - 1];
59    const squares = current.squares.slice();
60    if (calculateWinner(squares) || squares[i]) {
61      return;
62    }
63    squares[i] = this.state.xIsNext ? "X" : "O";
64    this.setState({
65      history: history.concat([
66        {
67          squares: squares
68        }
69      ]),
70      stepNumber: history.length,
71      xIsNext: !this.state.xIsNext
72    });
73  }
74
75  jumpTo(step) {
76    this.setState({
77      stepNumber: step,
78      xIsNext: (step % 2) === 0
79    });
80  }
81
82  render() {
83    const history = this.state.history;
84    const current = history[this.state.stepNumber];
85    const winner = calculateWinner(current.squares);
86
87    const moves = history.map((step, move) => {
88      const desc = move ?
89        'Go to move #' + move :
90        'Go to game start';
91      return (
92        <li key={move}>
93          <button onClick={() => this.jumpTo(move)}>{desc}</button>
94        </li>
95      );
96    });
97
98    let status;
99    if (winner) {
100      status = "Winner: " + winner;
101    } else {
102      status = "Next player: " + (this.state.xIsNext ? "X" : "O");
103    }
104
105    return (
106      <div className="game">
107        <div className="game-board">
108          <Board
109            squares={current.squares}
110            onClick={i => this.handleClick(i)}
111          />
112        </div>
113        <div className="game-info">
114          <div>{status}</div>
115          <ol>{moves}</ol>
116        </div>
117      </div>
118    );
119  }
120}
121
122// ========================================
123
124ReactDOM.render(<Game />, document.getElementById("root"));
125
126function calculateWinner(squares) {
127  const lines = [
128    [0, 1, 2],
129    [3, 4, 5],
130    [6, 7, 8],
131    [0, 3, 6],
132    [1, 4, 7],
133    [2, 5, 8],
134    [0, 4, 8],
135    [2, 4, 6]
136  ];
137  for (let i = 0; i < lines.length; i++) {
138    const [a, b, c] = lines[i];
139    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
140      return squares[a];
141    }
142  }
143  return null;
144}
145
Mirko
18 Jan 2018
1    useEffect(()=>{
2        //checking winner row and col
3        for (let i = 0; i <= 2; i++){
4            const idx = (i % 3) * 3 // -> 0,3,6
5            //check row
6            if ( (table[idx] + table[idx+1] + table[idx+2] )=== 9 || (table[idx] + table[idx+1] + table[idx+2] ) === 15){
7                setWinner([idx,idx+1,idx+2])
8                gameOver()
9            }
10            //check col
11            if ((table[i] + table[i+3] + table[i+6] )=== 9 || (table[i] + table[i+3] + table[i+6] ) === 15){
12                setWinner([i,i+3,i+6])
13                gameOver()
14            }
15        }
16        //checking winner diagonal
17        if ((table[0] + table[4] + table[8] ) === 15 || (table[0] + table[4] + table[8] ) === 9 ){
18            setWinner([0, 4, 8])
19            gameOver()
20        }
21        if ((table[2] + table[4] + table[6] ) === 9 || (table[2] + table[4] + table[6] ) ===15){
22            setWinner([2, 4, 6])
23            gameOver()
24        }
25        // check if table completed
26        if (table.indexOf(0) === -1){
27            gameOver()
28        }
29    }, [table])
30
queries leading to this page
tic tac toe using reactjs explainedtic tac toe game reactreact js tic tac toe gamereact tic tac toe time travelhow to use 27s in reactreact app tic tac toereact app learnreact js foe beginnerspick your starting point in react jstic tac toe react jsmake a tic tac toe ai reacttic tac toe in reacttic tac toe react examplereactjs button gametic tac toe react codereact tic tac toe tutorialreact tic tac toe class basedtic tac toe reacttic tac toe using reactjsnpx create react app tic tac totictactoe game in reacttic tac toe game from react setting up react classreact tic tac to gamebuild tic tac toe reacttic tac toe game react jstic tac toe in react jstic tac toe react using different componentdsreact tic tac toe gametic tak toe reacttic toc in reactjsdescription method reactreact learntic tac toe with reacttic tac toe game implementation in reactcreating a tic tac toe react appreact tic tac toe componentreact js tutorialsreact tictactoe docstic tac toe in reactjstic tac toe react with user name choosetic tac toe game using react js2021simple tic tac toe react jstic tac toe react node jsreact tic tac toe tutoriallearn reactreact intronnode react tic tac toe tutorialtic tac toe react jsreact class component tic tac toemultiplayer tic tac toe reacttick tak toe reactreact for dummies tic tac toe game in react js building from scratchreactdoc tic tac toelearn react jstic tac toe finised project in reacttic tac toe winner reacttic tok toe game using reactcreate 27start game 27 button reactmake tic tac toe in react nativereactjs tic tac toereact tutorial tic tac toe functioanl componentreact demo not showing tick tac toe boardtic tac toe game reacttic tac teo game reactreact create square componentcreate 27start game 27 btn reactjavascript array tic tact toe reacttic tac toe reactjstic tac toe in reat react native tic tac toereact front end tutorialreactjs realtime tic tac toetic tac toe jsxreactjs tictactoereact tictactoereact tik toe gamereact tictactoe apihow to write react learn reactjstic tac toe react jstic tac toe game using reactreact tutorialstic tic toe reactexplainer walk though react jsreact tictactoe exmplereact frontend js operationsreact game tutorialreact tutorial tic tac toereact get startedbuilding tic tac toe using reactjslearn reacy jsmaking tictactoe in react walk throughtic tac toe in react nativetic tac toe reactjs tutorialtic toe exammple react jstic toe game reacthow to make tic tac ai vs au toe in reacttic tac toe in react with moadlsimple toc reacttic tac toe iwth reactreact tic tac toetic tac toe sample design reactcreate tic tac toe game reactusing jsx as boarding in reactreact js for dummiesbullies react to decorate tic tacsreact tic tak toe gametic tac toe react nativecreate 27start game button 27 reacthow to format a tic tac toe game in reactjshow can we understand react documentationtic tact toe react apptic tac toe class react jsreact basicstic tac toe react projectoriginal in reacttic tac toe multiplayer game in reactreact js tic tac toesimple tic tac toe for 2 player reactgame start button and routing reactreact tic tac toe npmtic tok toe in reacttic tac toe react typescriptmultiplayer tic tac toe with reacttic tac toe game with react nodetic tac toe game in react jsreact docs tic tac toejavascript react code fillertic tac toe react js apptic tac toe doesnt show react tutorialreactjs tic tac toe gamereactjs org tic tac toe complete excercisetic tac toe node js reacttic tac toe react rusreact js cool tutorialhow to change x and o on tic tac toe reacttic tac toe game with reactreact tic tac toe using react domreactjs getting startedtic tac toe react tutorialbuild tic tac toe in reactreact tutorial tic tac toereact code for tic tak toereact documentation tic tac toetic tac toe game in reacttic tac toe react gametic tak toe game reactreact tic tac toe coderbyte solutionreact getting startedreact tic toc toetic tac toe using react is tough 3ci 3e in reactreact beginningcreate tic tac toe game reactytic tac toe game in react nativehow to make a tic tac toe using reactreact game start button and routinglearn react tic tac toereact tutorialtic tac toe react using different componentsreact js tutorial beginnertict tac toe raeacttic tac toe react jsreact website tictac tutorialreact tic tac toe classcreating tic tac toe in reacthow to build tic tac toe in reactsomething about react tic tac toereact square componentreactjs totic tac toe reactjs web apptictac toe reacttic tac toe program in react jstik tak toe reacttic tac toe game code in react jstic tac toe using react jslearn react from scratchreact full tutorialcreate tic tac toe game in react using jsx filessmall game code in react js walkthroughreact tac toetic tac toe react rodionchachurahow is right to make react game 3freact tutorial for beginnershow to make a tic tac toe game react jstic tac toe n 2an eactsimple react game tutorialtic tac toe reacttic tac toe project in reactreact tic tac toe appreact create app allow user to make and squaresreact javascript simple way of understatingreact tic tac toe coderbytereact tic tac toe where to put game functionalitytic tac toe game using react jstic tac toe vs computer javascript react nativetic tac toe game nodejs reacttic tac toe react rumaking tic tac toe in reactreact tic tac toe examplecreate 27start game btn reacttic toe game using react jslearning react tic tac toetic tac toe react js simple how to build simple tic tac toe game with reacttic tac toe game in react js from scratchtictactoe reacttic tac toe game in react class componentstic tac toe sample with reactmake a tic tac toe game in react functionalhow to make tic tac toe in reacttic tac toe react how to check if 3 xtic tac toe game node js reacttic tac toe react