-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.js
More file actions
184 lines (160 loc) · 4.49 KB
/
TicTacToe.js
File metadata and controls
184 lines (160 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
React Tic Tac Toe
We provided some simple React template code. Your goal is to create a functioning Tic Tac Toe game. It should work the following way:
the first player to go places an X anywhere on the board by clicking a square, and then the next player will be able to place an O,
and it continues alternating like this every turn.
You should also implement a function to determine if any player won by getting 3 X's or O's in a diagonal, horizontal, or vertical row.
If there is a winner, display a message at the top. If nobody wins, then do not display any message.
Finally, you should also implement the reset function that resets the entire board.
You should also not be able to override the other players move during the game.
You are free to add classes and styles, but make sure you leave the component ID's and classes provided as they are.
Submit your code once it is complete and our system will validate your output.
*/
import React, { useState } from 'react';
import { createRoot } from 'react-dom/client';
const rowStyle = {
display: 'flex'
};
const squareStyle = {
width: '60px',
height: '60px',
backgroundColor: '#ddd',
margin: '4px',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
fontSize: '20px',
color: 'white'
};
const boardStyle = {
backgroundColor: '#eee',
width: '208px',
alignItems: 'center',
justifyContent: 'center',
display: 'flex',
flexDirection: 'column',
border: '3px #eee solid'
};
const containerStyle = {
display: 'flex',
alignItems: 'center',
flexDirection: 'column'
};
const instructionsStyle = {
marginTop: '5px',
marginBottom: '5px',
fontWeight: 'bold',
fontSize: '16px',
};
const buttonStyle = {
marginTop: '15px',
marginBottom: '16px',
width: '80px',
height: '40px',
backgroundColor: '#8acaca',
color: 'white',
fontSize: '16px',
};
function Square({ value, onClick }) {
return (
<div
className="square"
style={squareStyle}
onClick={onClick}
>
{value}
</div>
);
}
function Board({ squares, onClick, resetGame, winner, nextPlayer }) {
const renderSquare = (i) => (
<Square
value={squares[i]}
onClick={() => onClick(i)}
/>
);
return (
<div style={containerStyle} className="gameBoard">
<div id="statusArea" className="status" style={instructionsStyle}>
Next player: <span>{nextPlayer}</span>
</div>
<div id="winnerArea" className="winner" style={instructionsStyle}>
Winner: <span>{winner || 'None'}</span>
</div>
<button style={buttonStyle} onClick={resetGame}>
Reset
</button>
<div style={boardStyle}>
<div className="board-row" style={rowStyle}>
{renderSquare(0)}
{renderSquare(1)}
{renderSquare(2)}
</div>
<div className="board-row" style={rowStyle}>
{renderSquare(3)}
{renderSquare(4)}
{renderSquare(5)}
</div>
<div className="board-row" style={rowStyle}>
{renderSquare(6)}
{renderSquare(7)}
{renderSquare(8)}
</div>
</div>
</div>
);
}
function Game() {
const [squares, setSquares] = useState(Array(9).fill(null));
const [isXNext, setIsXNext] = useState(true);
const [winner, setWinner] = useState(null);
const handleClick = (i) => {
// Ignore click if square is already filled or there's a winner
if (squares[i] || winner) return;
const newSquares = squares.slice();
newSquares[i] = isXNext ? 'X' : 'O';
setSquares(newSquares);
setIsXNext(!isXNext);
checkWinner(newSquares);
};
const checkWinner = (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]) {
setWinner(squares[a]);
return;
}
}
};
const resetGame = () => {
setSquares(Array(9).fill(null));
setWinner(null);
setIsXNext(true);
};
return (
<div className="game">
<div className="game-board">
<Board
squares={squares}
onClick={handleClick}
resetGame={resetGame}
winner={winner}
nextPlayer={isXNext ? 'X' : 'O'}
/>
</div>
</div>
);
}
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<Game />);