Tell us what’s happening:
hello, the game is working well as expected, but i can’t pass test #11. i need help
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Tic-Tac-Toe</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.26.5/babel.min.js"></script>
<script
data-plugins="transform-modules-umd"
type="text/babel"
src="index.jsx"
></script>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script
data-plugins="transform-modules-umd"
type="text/babel"
data-presets="react"
data-type="module"
>
import { Board } from './index.jsx';
ReactDOM.createRoot(document.getElementById('root')).render(<Board />);
</script>
</body>
</html>
/* file: styles.css */
.container {
display: grid;
grid-template-columns: 70px 70px 70px;
gap: 5px;
}
button {
height: 70px;
border-radius: 3px;
border: 1px solid transparent;
background-color: rgb(200, 200, 200);
font-size: 1.4rem;
}
button:hover {
background-color: rgb(210, 210, 210)
}
#reset {
width: 220px;
height: 50px;
margin-top: 10px;
background-color: rgb(210, 210, 210);
font-size: 1.1rem;
}
:disabled {
color: black;
}
p {
font-size: 1.2rem;
}
/* file: index.jsx */
const { useState, useRef, useEffect } = React;
const rounds = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
export function Board() {
const [game, setGame] = useState({
players: new Array(9).fill(0),
progress: 'Next player: X',
played: 'X',
winner: false
});
const container = useRef(null)
const handleClick = (e) => {
if (!game.winner) {
//set players
if (e.target.textContent === '') {
let player = game.played;
setGame((prev) => ({
...prev, played: player === 'X' ? 'O' : 'X', progress: `Next player: ${player == 'O' ? 'X' : 'O'}`
}))
//check winner
rounds.forEach((round, ind) => {
if (round.includes(Number(e.target.value))) {
const position = round.indexOf(Number(e.target.value))
round[position] = game.played;
if (round.join('').match(/[X]{3}|[O]{3}/g)) {
const won = round.join('').includes('X');
setGame((prev) => ({
...prev, progress: `Winner: ${won ? 'X' : 'O'}`, winner: true
}))
}
}
})
e.target.textContent = game.played
}
}
}
useEffect(() => {
if (!rounds.flat().join('').match(/[0-9]/g) && !game.winner) {
setGame((prev) => ({ ...prev, progress: "It's a draw" }))
}
}, [game.played])
const resetGame = () => {
if (container.current) {
[0, 1, 2, 3, 4, 5, 6, 7, 8].forEach(el => {
container.current.children[el].textContent = ''
})
rounds = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
setGame((prev) => ({ ...prev, winner: false, played: 'X', progress: 'Next player: X' }))
}
}
return (
<>
<p>{game.progress}</p>
<div className="container" ref={container}>
{game.players.map((player, ind) => {
return <button value={ind} className="square" key={ind} onClick={(e) => handleClick(e)}></button>
})}
</div>
<button id="reset" type="button" onClick={resetGame}>Reset</button>
</>
)
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 OPR/131.0.0.0
Challenge Information:
Build a Tic-Tac-Toe Game - Build a Tic-Tac-Toe Game