From 4721b8d68ceb8250de50741a983c5fe0b0b106f5 Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Tue, 4 Jun 2024 03:56:20 +0400 Subject: [PATCH] Refact Grid button --- src/game.jsx | 91 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/src/game.jsx b/src/game.jsx index 74ed5d7..cf3aedd 100644 --- a/src/game.jsx +++ b/src/game.jsx @@ -32,6 +32,40 @@ const getWinningCombo = (grid) => { return combos.find(wins) || []; }; +const GridButton = ({ onClick, symbol, turn, disabled, won }) => { + const Symbol = symbol == "X" ? Cross : Oval; + let symbolColor = symbol == "X" ? "text-blue-700" : "text-yellow-700"; + + const SymbolOutline = turn == "X" ? CrossOutline : OvalOutline; + let outlineColor = turn == "X" ? "text-blue-700" : "text-yellow-700"; + + let background = "bg-navy-400 inner-shadow-2-navy"; + + if (won) { + symbolColor = "text-navy-400"; + background = + symbol == "X" + ? "bg-blue-400 inner-shadow-2-blue" + : "bg-yellow-400 inner-shadow-2-yellow"; + } + + return ( + + ); +}; + export default ({ players }) => { const [score, updateScore] = useImmer({ X: 0, O: 0, ties: 0 }); const [grid, updateGrid] = useImmer(Array(9).fill("")); @@ -40,7 +74,7 @@ export default ({ players }) => { const [roundModal, setRoundModal] = useState(false); const turn = grid.filter((s) => s == "").length % 2 != 0 ? "X" : "O"; - const TurnOutline = turn == "X" ? CrossOutline : OvalOutline; + const cpuTurn = players[turn] == "CPU"; const TurnIndicator = turn == "X" ? Cross : Oval; const winningCombo = getWinningCombo(grid); @@ -48,9 +82,6 @@ export default ({ players }) => { const gridFull = !grid.some((s) => s == ""); const roundOver = winner || gridFull; - const tie = roundOver && !winner; - - const cpuTurn = players[turn] == "CPU"; useEffect(() => { if (roundOver) { @@ -89,39 +120,6 @@ export default ({ players }) => { updateGrid(() => Array(9).fill("")); }; - const renderSymbol = (symbol, index) => { - if (symbol == "") { - const colorClass = turn == "X" ? "text-blue-700" : "text-yellow-700"; - - return ( - - ); - } - - const Symbol = symbol == "X" ? Cross : Oval; - let colorClass = symbol == "X" ? "text-blue-700" : "text-yellow-700"; - - if (winner && winningCombo.includes(index)) { - colorClass = "text-navy-400"; - } - - return ; - }; - - const boxBackground = (index) => { - if (winner && winningCombo.includes(index)) { - if (winner == "X") { - return "bg-blue-400 inner-shadow-2-blue"; - } else { - return "bg-yellow-400 inner-shadow-2-yellow"; - } - } - - return "bg-navy-400 inner-shadow-2-navy"; - }; - return ( <>
@@ -147,18 +145,17 @@ export default ({ players }) => {
{grid.map((symbol, index) => ( - + }) + } + /> ))}