Skip to content

Commit b8c3129

Browse files
committed
adding switch statements for game states
1 parent 69f896c commit b8c3129

File tree

3 files changed

+177
-50
lines changed

3 files changed

+177
-50
lines changed

workspaces/simon-game/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack",
1919
"format": "prettier --color --write .",
2020
"lint": "code-chronicles-lint",
21-
"typecheck": "code-chronicles-typecheck"
21+
"typecheck": "code-chronicles-typecheck",
22+
"foo": "tsx src/foo.ts"
2223
},
2324
"dependencies": {
2425
"@code-chronicles/util": "workspace:*",

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

Lines changed: 126 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,132 @@ export function App() {
2424
const [gameState, setGameState] = useState<GameState>("pre-game");
2525
const [correctMoves, setCorrectMoves] = useState<readonly number[]>([]);
2626

27-
if (gameState === "pre-game") {
28-
return (
29-
<div style={{ display: "flex", gap: 10 }}>
30-
<button
31-
onClick={() => {
32-
setGameState("cpu-turn");
33-
setCorrectMoves((prev) => [...prev, randNum(4)]); // TODO: Add to cpu-turn state instead once created
34-
}}
35-
>
36-
Start Game
37-
</button>
38-
Simon Game
39-
</div>
40-
);
41-
}
42-
43-
return (
44-
<>
45-
<div style={{ display: "flex", gap: 10 }}>
46-
{config.boxes.map((box, index) => (
47-
<Box
48-
key={index}
49-
color={box.color}
27+
switch (gameState) {
28+
case "cpu-turn": {
29+
return (
30+
<>
31+
<div style={{ display: "flex", gap: 10 }}>
32+
{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+
/>
57+
))}
58+
</div>
59+
<pre>Game State: {gameState}</pre>
60+
<pre>Player Moves: {JSON.stringify(playerMoves, null, 2)}</pre>
61+
<pre>Correct Moves: {JSON.stringify(correctMoves, null, 2)}</pre>
62+
</>
63+
);
64+
}
65+
case "game-over": {
66+
return (
67+
<>
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>
99+
</>
100+
);
101+
}
102+
case "player-turn": {
103+
return (
104+
<>
105+
<div style={{ display: "flex", gap: 10 }}>
106+
{config.boxes.map((box, index) => (
107+
<Box
108+
key={index}
109+
color={box.color}
110+
onClick={() => {
111+
playNote(box.frequency);
112+
setPlayerMoves(() => {
113+
const newPlayerMoves = [...playerMoves, index];
114+
const isSequenceCorrect = isPrefixCorrect(
115+
newPlayerMoves,
116+
correctMoves,
117+
);
118+
if (!isSequenceCorrect) {
119+
setGameState("game-over");
120+
return newPlayerMoves;
121+
}
122+
if (newPlayerMoves.length === correctMoves.length) {
123+
setGameState("cpu-turn");
124+
return [];
125+
}
126+
setGameState("player-turn");
127+
return newPlayerMoves;
128+
});
129+
}}
130+
/>
131+
))}
132+
</div>
133+
<pre>Game State: {gameState}</pre>
134+
<pre>Player Moves: {JSON.stringify(playerMoves, null, 2)}</pre>
135+
<pre>Correct Moves: {JSON.stringify(correctMoves, null, 2)}</pre>
136+
</>
137+
);
138+
}
139+
case "pre-game": {
140+
return (
141+
<div style={{ display: "flex", gap: 10 }}>
142+
<button
50143
onClick={() => {
51-
playNote(box.frequency);
52-
setPlayerMoves(() => {
53-
const newPlayerMoves = [...playerMoves, index];
54-
const isSequenceCorrect = isPrefixCorrect(
55-
newPlayerMoves,
56-
correctMoves,
57-
);
58-
if (!isSequenceCorrect) {
59-
setGameState("game-over");
60-
return newPlayerMoves;
61-
}
62-
if (newPlayerMoves.length === correctMoves.length) {
63-
setGameState("cpu-turn");
64-
return [];
65-
}
66-
setGameState("player-turn");
67-
return newPlayerMoves;
68-
});
144+
setGameState("cpu-turn");
145+
setCorrectMoves((prev) => [...prev, randNum(4)]); // TODO: Add to cpu-turn state instead once created
69146
}}
70-
/>
71-
))}
72-
</div>
73-
<pre>Game State: {gameState}</pre>
74-
<pre>Player Moves: {JSON.stringify(playerMoves, null, 2)}</pre>
75-
<pre>Correct Moves: {JSON.stringify(correctMoves, null, 2)}</pre>
76-
</>
77-
);
147+
>
148+
Start Game
149+
</button>
150+
Simon Game
151+
</div>
152+
);
153+
}
154+
}
78155
}

workspaces/simon-game/src/foo.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const ESCAPES: Record<string, string | undefined> = {
2+
'"': '\\"',
3+
"\\": "\\\\",
4+
"\n": "\\n",
5+
"\r": "\\r",
6+
"\t": "\\t",
7+
};
8+
9+
function stringifyJSON(data: unknown): string {
10+
if (typeof data === "number") {
11+
return String(data);
12+
}
13+
if (typeof data === "boolean") {
14+
return data ? "true" : "false";
15+
}
16+
if (typeof data === "string") {
17+
return '"' + [...data].map((c) => ESCAPES[c] ?? c).join("") + '"';
18+
}
19+
if (typeof data === "object") {
20+
// eslint-disable-next-line eqeqeq
21+
if (data === null) {
22+
return "null";
23+
}
24+
if (Array.isArray(data)) {
25+
if (data.length === 0) {
26+
return "[]";
27+
}
28+
29+
return "[" + data.map(stringifyJSON).join(",") + "]";
30+
}
31+
32+
const entries = Object.entries(data);
33+
if (entries.length === 0) {
34+
return "{}";
35+
}
36+
return (
37+
"{" +
38+
entries.map(
39+
([key, value]) => `${stringifyJSON(key)}:${stringifyJSON(value)}`,
40+
) +
41+
"}"
42+
);
43+
}
44+
45+
throw new Error("Unknown Type");
46+
}
47+
48+
console.log(stringifyJSON({ 1: "world", 2: { hello: "world2" } }));
49+
console.log(stringifyJSON([0, 1, -3, [-4, 5, [2]]]));

0 commit comments

Comments
 (0)