From 633084ee95d1fa22c90440cc1338ca15bd40f48c Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Tue, 4 Jun 2024 00:27:49 +0400 Subject: [PATCH] Finish most of it --- src/game.jsx | 67 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/src/game.jsx b/src/game.jsx index 95b62ef..74ed5d7 100644 --- a/src/game.jsx +++ b/src/game.jsx @@ -8,7 +8,7 @@ import OvalOutline from "../assets/icon-o-outline.svg?react"; import logo from "../assets/logo.svg"; import restart from "../assets/icon-restart.svg"; -import { restartGame } from "./store"; +import { restartGame, setIsGameRunning } from "./store"; import Modal from "./modal"; const getWinningCombo = (grid) => { @@ -46,28 +46,16 @@ export default ({ players }) => { const winningCombo = getWinningCombo(grid); const winner = winningCombo.length > 0 ? grid[winningCombo[0]] : null; + const gridFull = !grid.some((s) => s == ""); + const roundOver = winner || gridFull; + const tie = roundOver && !winner; + const cpuTurn = players[turn] == "CPU"; useEffect(() => { - if (winner) { + if (roundOver) { const timeoutId = setTimeout(() => { - updateScore((score) => { - score[winner]++; - }); - - updateGrid(() => Array(9).fill("")); - }, 1000); - - return () => clearTimeout(timeoutId); - } - - if (!grid.some((s) => s == "")) { - const timeoutId = setTimeout(() => { - updateScore((score) => { - score.ties++; - }); - - updateGrid(() => Array(9).fill("")); + setRoundModal(true); }, 1000); return () => clearTimeout(timeoutId); @@ -89,6 +77,18 @@ export default ({ players }) => { } }, [grid]); + const nextRound = () => { + updateScore((score) => { + if (winner) { + score[winner]++; + } else { + score.ties++; + } + }); + + updateGrid(() => Array(9).fill("")); + }; + const renderSymbol = (symbol, index) => { if (symbol == "") { const colorClass = turn == "X" ? "text-blue-700" : "text-yellow-700"; @@ -180,13 +180,11 @@ export default ({ players }) => { { - setRestartModal(false); - }} + onClose={() => setRestartModal(false)} >

Restart game?

+ setRoundModal(false)} + > +
+ + +
+
); };