From 7536266ff26fce15f9e3dd255816077c44ee57cd Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Tue, 12 Nov 2024 21:28:03 -0800 Subject: [PATCH 1/4] feat: adding player moves and correct moves verifier --- workspaces/simon-game/src/app/components/App.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 9d38e3d6..943cdfda 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -4,6 +4,10 @@ import { Box } from "./Box.tsx"; import { playNote } from "../util/playNote.ts"; import { config } from "../constants.ts"; +function isSequenceCorrect(check: number[], correct: number[]): boolean { + return check.every((element, i) => element === correct[i]); +} + type GameState = "pre-game" | "game-over" | "player-turn" | "cpu-turn"; // TODO: Move this to a new file later From e34beae41e678f30340a4f9ad5b8707d2ee7d322 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Wed, 13 Nov 2024 00:14:45 -0800 Subject: [PATCH 2/4] feat: adding gamestatus state transitions --- .../simon-game/src/app/components/App.tsx | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 943cdfda..33cae1c3 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -4,7 +4,10 @@ import { Box } from "./Box.tsx"; import { playNote } from "../util/playNote.ts"; import { config } from "../constants.ts"; -function isSequenceCorrect(check: number[], correct: number[]): boolean { +function isSequenceCorrect( + check: readonly number[], + correct: readonly number[], +): boolean { return check.every((element, i) => element === correct[i]); } @@ -45,11 +48,36 @@ export function App() { color={box.color} onClick={() => { playNote(box.frequency); - setPlayerMoves((prev) => [...prev, index]); + setPlayerMoves((prev) => { + const newPlayerMoves = [...prev, index]; + const isCorrectSequence = isSequenceCorrect( + newPlayerMoves, + correctMoves, + ); + if (!isCorrectSequence) { + setGameState("game-over"); + return []; + } + if ( + isCorrectSequence && + newPlayerMoves.length === correctMoves.length + ) { + setGameState("cpu-turn"); + return []; + } + if ( + isCorrectSequence && + newPlayerMoves.length < correctMoves.length + ) { + setGameState("player-turn"); + } + return newPlayerMoves; + }); }} /> ))} +
Game State: {gameState}
Player Moves: {JSON.stringify(playerMoves, null, 2)}
Correct Moves: {JSON.stringify(correctMoves, null, 2)}> From f704fd22df82e58a9e7b7b2014b54d5e72f07208 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Wed, 13 Nov 2024 00:17:19 -0800 Subject: [PATCH 3/4] fix: gameover no longer resets players moves --- workspaces/simon-game/src/app/components/App.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 33cae1c3..3d3e9f5a 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -56,7 +56,6 @@ export function App() { ); if (!isCorrectSequence) { setGameState("game-over"); - return []; } if ( isCorrectSequence && From c945f387409c7245defcc3ebec106d6e01a85ec3 Mon Sep 17 00:00:00 2001 From: Eli Manzo <96manzo.eli@gmail.com> Date: Fri, 15 Nov 2024 20:47:28 -0800 Subject: [PATCH 4/4] chore: adding feedback --- .../simon-game/src/app/components/App.tsx | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/workspaces/simon-game/src/app/components/App.tsx b/workspaces/simon-game/src/app/components/App.tsx index 3d3e9f5a..22dc318d 100644 --- a/workspaces/simon-game/src/app/components/App.tsx +++ b/workspaces/simon-game/src/app/components/App.tsx @@ -4,11 +4,12 @@ import { Box } from "./Box.tsx"; import { playNote } from "../util/playNote.ts"; import { config } from "../constants.ts"; -function isSequenceCorrect( - check: readonly number[], +// TODO: Make this a reusable utility +function isPrefixCorrect( + prefix: readonly number[], correct: readonly number[], ): boolean { - return check.every((element, i) => element === correct[i]); + return prefix.every((element, i) => element === correct[i]); } type GameState = "pre-game" | "game-over" | "player-turn" | "cpu-turn"; @@ -48,28 +49,21 @@ export function App() { color={box.color} onClick={() => { playNote(box.frequency); - setPlayerMoves((prev) => { - const newPlayerMoves = [...prev, index]; - const isCorrectSequence = isSequenceCorrect( + setPlayerMoves(() => { + const newPlayerMoves = [...playerMoves, index]; + const isSequenceCorrect = isPrefixCorrect( newPlayerMoves, correctMoves, ); - if (!isCorrectSequence) { + if (!isSequenceCorrect) { setGameState("game-over"); + return newPlayerMoves; } - if ( - isCorrectSequence && - newPlayerMoves.length === correctMoves.length - ) { + if (newPlayerMoves.length === correctMoves.length) { setGameState("cpu-turn"); return []; } - if ( - isCorrectSequence && - newPlayerMoves.length < correctMoves.length - ) { - setGameState("player-turn"); - } + setGameState("player-turn"); return newPlayerMoves; }); }}