create tic tac toe game in react using jsx files

Solutions on MaxInterview for create tic tac toe game in react using jsx files by the best coders in the world

showing results for - "create tic tac toe game in react using jsx files"
Cyrielle
04 Sep 2016
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
Gerardo
15 May 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 react is toughtic tac toe sample design reactsomething about react tic tac toereact tutorial for beginnersreact js tutorial beginnerreact js tutorialstic tac toe game from react tic tac toe react jsreact website tictac tutoriallearn reacy jslearn reactjstic tac toe doesnt show react tutorialreact square componenttic tac toe react with user name choosemaking tic tac toe in reactreact tic tac toe tutorialreact tictactoe exmplemake a tic tac toe game in react functionalreact tic tac toe where to put game functionalitytic tac toe in react jstic tac toe jsxhow to write react react tic tak toe gametic tac toe game reactreact tic tac toe componenttic tac toe react jsreact app learnreactjs org tic tac toe complete excercisetic tok toe in reactreact game tutorialmaking tictactoe in react walk throughtic tac toe in reat tic tac toe game in react jstic tac toe game with react nodesmall game code in react js walkthroughcreate tic tac toe game reactlearn react tic tac toereact tic tac to gametic tac toe react jsreact tictactoe docstic tac toe react examplecreating a tic tac toe react appcreate 27start game btn reactreact tic tac toe using react domtic tac toe react js simple react tic tac toetic tac toe react node jsusing jsx as boarding in reacttic tac toe in react with moadltictac toe reacttic tac toe game reacttic tac toe game in react class componentssimple toc reactreact intro 3ci 3e in reactreact tutorial tic tac toetic tac toe game nodejs reacttic tac toe game using reacttic tac toe game in reactcreate tic tac toe game reactylearn react from scratchtic toc in reactjstic tac toe in reacttic tac toe using reactjshow to make tic tac ai vs au toe in reacthow to build simple tic tac toe game with reactreact tutorialbuilding tic tac toe using reactjsreact js for dummiestic tac toe project in reactreact class component tic tac toehow to make a tic tac toe game react jsbuild tic tac toe reacttic tac toe reactjs tutorialbuild tic tac toe in reactreact docs tic tac toetic tac toe class react jsmake a tic tac toe ai reacthow can we understand react documentationtic tact toe react appsimple react game tutorialtic tac toe in reactjstic tac toe game using react js2021tic tac toe react jstic tac toe react gametic tok toe game using reactreactjs tictactoetic tac toe react js appreact tic tac toe class basedreact for dummies learn reacttic tac toe with reacttictactoe reactreact get startedhow to change x and o on tic tac toe reacttic tac toe multiplayer game in reactreact tictactoe apireact js foe beginnershow to format a tic tac toe game in reactjsreact beginningreact learntictactoe game in reacttic tac toe react how to check if 3 xreact tic tac toe npmtic tac toe game react jsreact create app allow user to make and squarestic toe game reactcreating tic tac toe in reacttict tac toe raeactcreate 27start game 27 btn reacttic tac toe vs computer javascript react nativelearning react tic tac toetic tak toe reacttic tac toe game node js reacthow is right to make react game 3freactjs realtime tic tac toereactjs tic tac toe gamereact tic tac toe appreact front end tutorialreact documentation tic tac toedescription method reactreactjs button gametic tac toe reactjsexplainer walk though react jsjavascript react code fillertic tac toe using reactjs explainedmultiplayer tic tac toe with reacthow to make tic tac toe in reacttic tac toe winner reacthow to use 27s in reactreact frontend js operationslearn react jsreact tutorial tic tac toeoriginal in reactcreate 27start game button 27 reacttic tac toe game implementation in reacthow to make a tic tac toe using reacttic toe game using react jsreact tic tac toe gamehow to build tic tac toe in reactcreate tic tac toe game in react using jsx filesreact game start button and routingreact native tic tac toetic tac toe in react nativereact tic toc toereact create square componenttic tac toe react rutic tac toe n 2an eactmultiplayer tic tac toe reacttic tac toe react rodionchachuratic tac toe game in react js building from scratchtic tac toe sample with reactreact tik toe gametic toe exammple react jstic tac toe react using different componentstic tac toe react tutorialtic tac toe game code in react jstic tac toe using react jsreactjs totick tak toe reactreact js tic tac toe gametic tac toe game with reactreact tic tac toe coderbyte solutionreact basicsreact tutorial tic tac toe functioanl componenttic tac toe react rustic tac toe reactjavascript array tic tact toe reacttic tac toe program in react jstic tic toe reacttic tac toe game using react jsreact app tic tac toebullies react to decorate tic tacsreact demo not showing tick tac toe boardtic tac toe reactreact tic tac toe coderbytereact tac toereact tic tac toe time travelreactjs getting startednnode react tic tac toe tutorialmake tic tac toe in react nativereact tictactoereact getting startedtic tac toe reactjs web apptic tak toe game reactsimple tic tac toe react jstik tak toe reacttic tac toe finised project in reactpick your starting point in react jsreactdoc tic tac toereactjs tic tac toetic tac toe game in react nativereact tic tac toe examplereact tutorialsgame start button and routing reacttic tac toe react codetic tac teo game reactreact tic tac toe tutorialreact full tutorialreact javascript simple way of understatingsimple tic tac toe for 2 player reacttic tac toe react using different componentdsreact js tic tac toetic tac toe node js reacttic tac toe react typescriptnpx create react app tic tac toreact tic tac toe classcreate 27start game 27 button reactreact code for tic tak toetic tac toe game in react js from scratchtic tac toe react nativetic tac toe iwth reacttic tac toe react projectsetting up react classreact js cool tutorialcreate tic tac toe game in react using jsx files