Skip to content

Commit 9caf147

Browse files
committed
adding cpu-turn effect
1 parent 2c878a4 commit 9caf147

File tree

2 files changed

+27
-69
lines changed

2 files changed

+27
-69
lines changed

workspaces/simon-game/src/app/components/App.tsx

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22

33
import { Box } from "./Box.tsx";
44
import { playNote } from "../util/playNote.ts";
@@ -24,36 +24,28 @@ export function App() {
2424
const [gameState, setGameState] = useState<GameState>("pre-game");
2525
const [correctMoves, setCorrectMoves] = useState<readonly number[]>([]);
2626

27+
useEffect(() => {
28+
if (gameState !== "cpu-turn") {
29+
return;
30+
}
31+
setCorrectMoves((prev) => [...prev, randNum(4)]);
32+
setPlayerMoves([]);
33+
setGameState("player-turn");
34+
}, [gameState]);
35+
36+
const newGame = () => {
37+
setGameState("cpu-turn");
38+
setCorrectMoves([]); // TODO: Add to cpu-turn state instead once created
39+
setPlayerMoves([]);
40+
};
41+
2742
switch (gameState) {
2843
case "cpu-turn": {
2944
return (
3045
<>
3146
<div style={{ display: "flex", gap: 10 }}>
3247
{config.boxes.map((box, index) => (
33-
<Box
34-
key={index}
35-
color={box.color}
36-
onClick={() => {
37-
playNote(box.frequency);
38-
setPlayerMoves(() => {
39-
const newPlayerMoves = [...playerMoves, index];
40-
const isSequenceCorrect = isPrefixCorrect(
41-
newPlayerMoves,
42-
correctMoves,
43-
);
44-
if (!isSequenceCorrect) {
45-
setGameState("game-over");
46-
return newPlayerMoves;
47-
}
48-
if (newPlayerMoves.length === correctMoves.length) {
49-
setGameState("cpu-turn");
50-
return [];
51-
}
52-
setGameState("player-turn");
53-
return newPlayerMoves;
54-
});
55-
}}
56-
/>
48+
<Box key={index} color={box.color} isDisabled />
5749
))}
5850
</div>
5951
<pre>Game State: {gameState}</pre>
@@ -65,37 +57,8 @@ export function App() {
6557
case "game-over": {
6658
return (
6759
<>
68-
<div style={{ display: "flex", gap: 10 }}>
69-
{config.boxes.map((box, index) => (
70-
<Box
71-
key={index}
72-
color={box.color}
73-
onClick={() => {
74-
playNote(box.frequency);
75-
setPlayerMoves(() => {
76-
const newPlayerMoves = [...playerMoves, index];
77-
const isSequenceCorrect = isPrefixCorrect(
78-
newPlayerMoves,
79-
correctMoves,
80-
);
81-
if (!isSequenceCorrect) {
82-
setGameState("game-over");
83-
return newPlayerMoves;
84-
}
85-
if (newPlayerMoves.length === correctMoves.length) {
86-
setGameState("cpu-turn");
87-
return [];
88-
}
89-
setGameState("player-turn");
90-
return newPlayerMoves;
91-
});
92-
}}
93-
/>
94-
))}
95-
</div>
96-
<pre>Game State: {gameState}</pre>
97-
<pre>Player Moves: {JSON.stringify(playerMoves, null, 2)}</pre>
98-
<pre>Correct Moves: {JSON.stringify(correctMoves, null, 2)}</pre>
60+
<h1>Game Over ... Start again ...</h1>
61+
<button onClick={newGame}>New Game</button>
9962
</>
10063
);
10164
}
@@ -129,14 +92,7 @@ export function App() {
12992
case "pre-game": {
13093
return (
13194
<div style={{ display: "flex", gap: 10 }}>
132-
<button
133-
onClick={() => {
134-
setGameState("cpu-turn");
135-
setCorrectMoves((prev) => [...prev, randNum(4)]); // TODO: Add to cpu-turn state instead once created
136-
}}
137-
>
138-
Start Game
139-
</button>
95+
<button onClick={newGame}>Start Game</button>
14096
Simon Game
14197
</div>
14298
);

workspaces/simon-game/src/app/components/Box.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import React from "react";
33
type Props = {
44
color?: string;
55
height?: number;
6-
width?: number;
6+
isDisabled?: boolean;
77
margin?: number;
8-
onClick: () => void;
8+
onClick?: () => void;
9+
width?: number;
910
};
1011

1112
export function Box({
1213
color = "lightblue",
1314
height = 100,
14-
width = 100,
15+
isDisabled = false,
1516
margin,
1617
onClick,
18+
width = 100,
1719
}: Props) {
1820
return (
1921
<button
@@ -23,10 +25,10 @@ export function Box({
2325
width,
2426
borderRadius: 20, // rounded corners
2527
margin, // spaces between boxes
26-
cursor: "pointer",
28+
cursor: isDisabled ? undefined : "pointer",
2729
border: 0,
2830
}}
29-
onClick={onClick}
31+
onClick={isDisabled ? undefined : onClick}
3032
/>
3133
);
3234
}

0 commit comments

Comments
 (0)