diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 22dc318d..234b9077 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { Box } from "./Box.tsx"; import { playNote } from "../util/playNote.ts"; @@ -24,55 +24,78 @@ export function App() { const [gameState, setGameState] = useState("pre-game"); const [correctMoves, setCorrectMoves] = useState([]); - if (gameState === "pre-game") { - return ( -
- - Simon Game -
- ); - } + useEffect(() => { + if (gameState !== "cpu-turn") { + return; + } + setCorrectMoves((prev) => [...prev, randNum(4)]); + setPlayerMoves([]); + setGameState("player-turn"); + }, [gameState]); + + const newGame = () => { + setGameState("cpu-turn"); + setCorrectMoves([]); + setPlayerMoves([]); + }; - return ( - <> -
- {config.boxes.map((box, index) => ( - { - playNote(box.frequency); - setPlayerMoves(() => { - const newPlayerMoves = [...playerMoves, index]; - const isSequenceCorrect = isPrefixCorrect( - newPlayerMoves, - correctMoves, - ); - if (!isSequenceCorrect) { - setGameState("game-over"); - return newPlayerMoves; - } - if (newPlayerMoves.length === correctMoves.length) { - setGameState("cpu-turn"); - return []; - } - setGameState("player-turn"); - return newPlayerMoves; - }); - }} - /> - ))} -
-
Game State: {gameState}
-
Player Moves: {JSON.stringify(playerMoves, null, 2)}
-
Correct Moves: {JSON.stringify(correctMoves, null, 2)}
- - ); + switch (gameState) { + case "cpu-turn": { + return ( + <> +
+ {config.boxes.map((box, index) => ( + + ))} +
+
Game State: {gameState}
+
Player Moves: {JSON.stringify(playerMoves, null, 2)}
+
Correct Moves: {JSON.stringify(correctMoves, null, 2)}
+ + ); + } + case "game-over": { + return ( + <> +

Game Over ... Start again ...

+ + + ); + } + case "player-turn": { + return ( + <> +
+ {config.boxes.map((box, index) => ( + { + playNote(box.frequency); + const newPlayerMoves = [...playerMoves, index]; + setPlayerMoves(newPlayerMoves); + if (!isPrefixCorrect(newPlayerMoves, correctMoves)) { + setGameState("game-over"); + } else if (newPlayerMoves.length === correctMoves.length) { + setGameState("cpu-turn"); + } + }} + /> + ))} +
+
Game State: {gameState}
+
Player Moves: {JSON.stringify(playerMoves, null, 2)}
+
Correct Moves: {JSON.stringify(correctMoves, null, 2)}
+ + ); + } + case "pre-game": { + return ( +
+ + Simon Game +
+ ); + } + } } diff --git a/workspaces/simon-game/src/app/components/Box.tsx b/workspaces/simon-game/src/app/components/Box.tsx index 64e989ab..a7c9e2ca 100644 --- a/workspaces/simon-game/src/app/components/Box.tsx +++ b/workspaces/simon-game/src/app/components/Box.tsx @@ -3,17 +3,19 @@ import React from "react"; type Props = { color?: string; height?: number; - width?: number; + isDisabled?: boolean; margin?: number; - onClick: () => void; + onClick?: () => void; + width?: number; }; export function Box({ color = "lightblue", height = 100, - width = 100, + isDisabled = false, margin, onClick, + width = 100, }: Props) { return (