Skip to content

Commit c945f38

Browse files
committed
chore: adding feedback
1 parent f704fd2 commit c945f38

File tree

1 file changed

+11
-17
lines changed
  • workspaces/simon-game/src/app/components

1 file changed

+11
-17
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { Box } from "./Box.tsx";
44
import { playNote } from "../util/playNote.ts";
55
import { config } from "../constants.ts";
66

7-
function isSequenceCorrect(
8-
check: readonly number[],
7+
// TODO: Make this a reusable utility
8+
function isPrefixCorrect(
9+
prefix: readonly number[],
910
correct: readonly number[],
1011
): boolean {
11-
return check.every((element, i) => element === correct[i]);
12+
return prefix.every((element, i) => element === correct[i]);
1213
}
1314

1415
type GameState = "pre-game" | "game-over" | "player-turn" | "cpu-turn";
@@ -48,28 +49,21 @@ export function App() {
4849
color={box.color}
4950
onClick={() => {
5051
playNote(box.frequency);
51-
setPlayerMoves((prev) => {
52-
const newPlayerMoves = [...prev, index];
53-
const isCorrectSequence = isSequenceCorrect(
52+
setPlayerMoves(() => {
53+
const newPlayerMoves = [...playerMoves, index];
54+
const isSequenceCorrect = isPrefixCorrect(
5455
newPlayerMoves,
5556
correctMoves,
5657
);
57-
if (!isCorrectSequence) {
58+
if (!isSequenceCorrect) {
5859
setGameState("game-over");
60+
return newPlayerMoves;
5961
}
60-
if (
61-
isCorrectSequence &&
62-
newPlayerMoves.length === correctMoves.length
63-
) {
62+
if (newPlayerMoves.length === correctMoves.length) {
6463
setGameState("cpu-turn");
6564
return [];
6665
}
67-
if (
68-
isCorrectSequence &&
69-
newPlayerMoves.length < correctMoves.length
70-
) {
71-
setGameState("player-turn");
72-
}
66+
setGameState("player-turn");
7367
return newPlayerMoves;
7468
});
7569
}}

0 commit comments

Comments
 (0)