From c4bbe0d0001f03bca97b966b1c6610ec29c9c27b Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Wed, 19 Nov 2025 03:44:37 +0000 Subject: [PATCH] Changes --- src/components/PresentsPuzzle.tsx | 90 ++++++++++++++++++------------- 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/src/components/PresentsPuzzle.tsx b/src/components/PresentsPuzzle.tsx index 56dea60..024b35c 100644 --- a/src/components/PresentsPuzzle.tsx +++ b/src/components/PresentsPuzzle.tsx @@ -141,11 +141,6 @@ const PresentsPuzzle = ({ setIsRunning(true); isPausedRef.current = false; setWinner(null); - setAliceOpened(new Set()); - setBobOpened(new Set()); - setAliceFound(0); - setBobFound(0); - setCurrentStep(0); const presents = generatePresents(); setPresentBoxes(presents); const newAliceOrder = getAliceOrder(); @@ -153,36 +148,49 @@ const PresentsPuzzle = ({ setAliceOrder(newAliceOrder); setBobOrder(newBobOrder); + // Initialize with empty state + setAliceOpened(new Set()); + setBobOpened(new Set()); + setAliceFound(0); + setBobFound(0); + setCurrentStep(-1); // -1 means no boxes opened yet + // Start animation from beginning let aliceCount = 0; let bobCount = 0; let currentWinner: 'alice' | 'bob' | 'tie' | null = null; for (let i = 0; i < 100 && !currentWinner; i++) { + await new Promise(resolve => setTimeout(resolve, 50)); + + // Check pause after delay, before opening boxes if (isPausedRef.current) { - setCurrentStep(i); setIsRunning(false); return; } - await new Promise(resolve => setTimeout(resolve, 50)); - const aliceBox = newAliceOrder[i]; const bobBox = newBobOrder[i]; - setAliceOpened(prev => new Set(prev).add(aliceBox)); - setBobOpened(prev => new Set(prev).add(bobBox)); - setCurrentStep(i); + + // Create new sets with the new boxes (ensures synchronous update) + const newAliceOpened = new Set(); + const newBobOpened = new Set(); + for (let j = 0; j <= i; j++) { + newAliceOpened.add(newAliceOrder[j]); + newBobOpened.add(newBobOrder[j]); + } const aliceFoundPresent = presents.has(aliceBox); const bobFoundPresent = presents.has(bobBox); - if (aliceFoundPresent) { - aliceCount++; - setAliceFound(aliceCount); - } - if (bobFoundPresent) { - bobCount++; - setBobFound(bobCount); - } + if (aliceFoundPresent) aliceCount++; + if (bobFoundPresent) bobCount++; + + // Update all state together for perfect sync + setAliceOpened(newAliceOpened); + setBobOpened(newBobOpened); + setAliceFound(aliceCount); + setBobFound(bobCount); + setCurrentStep(i); if (aliceCount === 26 && bobCount === 26) { currentWinner = 'tie'; @@ -204,30 +212,36 @@ const PresentsPuzzle = ({ let currentWinner: 'alice' | 'bob' | 'tie' | null = winner; for (let i = currentStep + 1; i < 100 && !currentWinner; i++) { + await new Promise(resolve => setTimeout(resolve, 50)); + + // Check pause after delay, before opening boxes if (isPausedRef.current) { - setCurrentStep(i); setIsRunning(false); return; } - await new Promise(resolve => setTimeout(resolve, 50)); - const aliceBox = aliceOrder[i]; const bobBox = bobOrder[i]; - setAliceOpened(prev => new Set(prev).add(aliceBox)); - setBobOpened(prev => new Set(prev).add(bobBox)); - setCurrentStep(i); + + // Create new sets with the new boxes + const newAliceOpened = new Set(); + const newBobOpened = new Set(); + for (let j = 0; j <= i; j++) { + newAliceOpened.add(aliceOrder[j]); + newBobOpened.add(bobOrder[j]); + } const aliceFoundPresent = presentBoxes.has(aliceBox); const bobFoundPresent = presentBoxes.has(bobBox); - if (aliceFoundPresent) { - aliceCount++; - setAliceFound(aliceCount); - } - if (bobFoundPresent) { - bobCount++; - setBobFound(bobCount); - } + if (aliceFoundPresent) aliceCount++; + if (bobFoundPresent) bobCount++; + + // Update all state together for perfect sync + setAliceOpened(newAliceOpened); + setBobOpened(newBobOpened); + setAliceFound(aliceCount); + setBobFound(bobCount); + setCurrentStep(i); if (aliceCount === 26 && bobCount === 26) { currentWinner = 'tie'; @@ -268,7 +282,7 @@ const PresentsPuzzle = ({ setBobOpened(new Set()); setAliceFound(0); setBobFound(0); - setCurrentStep(0); + setCurrentStep(-1); // -1 means no boxes opened yet setWinner(null); }; @@ -282,7 +296,7 @@ const PresentsPuzzle = ({ setWinner(null); setIsRunning(false); isPausedRef.current = false; - setCurrentStep(0); + setCurrentStep(-1); // -1 means not initialized setAliceOrder([]); setBobOrder([]); }; @@ -392,7 +406,7 @@ const PresentsPuzzle = ({ className="gap-2" > - {currentStep === 0 ? 'Start' : 'Resume'} + {currentStep === -1 ? 'Start' : 'Resume'}