Changes
This commit is contained in:
parent
2bfc8603ca
commit
c4bbe0d000
1 changed files with 52 additions and 38 deletions
|
|
@ -141,11 +141,6 @@ const PresentsPuzzle = ({
|
||||||
setIsRunning(true);
|
setIsRunning(true);
|
||||||
isPausedRef.current = false;
|
isPausedRef.current = false;
|
||||||
setWinner(null);
|
setWinner(null);
|
||||||
setAliceOpened(new Set());
|
|
||||||
setBobOpened(new Set());
|
|
||||||
setAliceFound(0);
|
|
||||||
setBobFound(0);
|
|
||||||
setCurrentStep(0);
|
|
||||||
const presents = generatePresents();
|
const presents = generatePresents();
|
||||||
setPresentBoxes(presents);
|
setPresentBoxes(presents);
|
||||||
const newAliceOrder = getAliceOrder();
|
const newAliceOrder = getAliceOrder();
|
||||||
|
|
@ -153,36 +148,49 @@ const PresentsPuzzle = ({
|
||||||
setAliceOrder(newAliceOrder);
|
setAliceOrder(newAliceOrder);
|
||||||
setBobOrder(newBobOrder);
|
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
|
// Start animation from beginning
|
||||||
let aliceCount = 0;
|
let aliceCount = 0;
|
||||||
let bobCount = 0;
|
let bobCount = 0;
|
||||||
let currentWinner: 'alice' | 'bob' | 'tie' | null = null;
|
let currentWinner: 'alice' | 'bob' | 'tie' | null = null;
|
||||||
|
|
||||||
for (let i = 0; i < 100 && !currentWinner; i++) {
|
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) {
|
if (isPausedRef.current) {
|
||||||
setCurrentStep(i);
|
|
||||||
setIsRunning(false);
|
setIsRunning(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 50));
|
|
||||||
|
|
||||||
const aliceBox = newAliceOrder[i];
|
const aliceBox = newAliceOrder[i];
|
||||||
const bobBox = newBobOrder[i];
|
const bobBox = newBobOrder[i];
|
||||||
setAliceOpened(prev => new Set(prev).add(aliceBox));
|
|
||||||
setBobOpened(prev => new Set(prev).add(bobBox));
|
// Create new sets with the new boxes (ensures synchronous update)
|
||||||
setCurrentStep(i);
|
const newAliceOpened = new Set<number>();
|
||||||
|
const newBobOpened = new Set<number>();
|
||||||
|
for (let j = 0; j <= i; j++) {
|
||||||
|
newAliceOpened.add(newAliceOrder[j]);
|
||||||
|
newBobOpened.add(newBobOrder[j]);
|
||||||
|
}
|
||||||
|
|
||||||
const aliceFoundPresent = presents.has(aliceBox);
|
const aliceFoundPresent = presents.has(aliceBox);
|
||||||
const bobFoundPresent = presents.has(bobBox);
|
const bobFoundPresent = presents.has(bobBox);
|
||||||
if (aliceFoundPresent) {
|
if (aliceFoundPresent) aliceCount++;
|
||||||
aliceCount++;
|
if (bobFoundPresent) bobCount++;
|
||||||
setAliceFound(aliceCount);
|
|
||||||
}
|
// Update all state together for perfect sync
|
||||||
if (bobFoundPresent) {
|
setAliceOpened(newAliceOpened);
|
||||||
bobCount++;
|
setBobOpened(newBobOpened);
|
||||||
setBobFound(bobCount);
|
setAliceFound(aliceCount);
|
||||||
}
|
setBobFound(bobCount);
|
||||||
|
setCurrentStep(i);
|
||||||
|
|
||||||
if (aliceCount === 26 && bobCount === 26) {
|
if (aliceCount === 26 && bobCount === 26) {
|
||||||
currentWinner = 'tie';
|
currentWinner = 'tie';
|
||||||
|
|
@ -204,30 +212,36 @@ const PresentsPuzzle = ({
|
||||||
let currentWinner: 'alice' | 'bob' | 'tie' | null = winner;
|
let currentWinner: 'alice' | 'bob' | 'tie' | null = winner;
|
||||||
|
|
||||||
for (let i = currentStep + 1; i < 100 && !currentWinner; i++) {
|
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) {
|
if (isPausedRef.current) {
|
||||||
setCurrentStep(i);
|
|
||||||
setIsRunning(false);
|
setIsRunning(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 50));
|
|
||||||
|
|
||||||
const aliceBox = aliceOrder[i];
|
const aliceBox = aliceOrder[i];
|
||||||
const bobBox = bobOrder[i];
|
const bobBox = bobOrder[i];
|
||||||
setAliceOpened(prev => new Set(prev).add(aliceBox));
|
|
||||||
setBobOpened(prev => new Set(prev).add(bobBox));
|
// Create new sets with the new boxes
|
||||||
setCurrentStep(i);
|
const newAliceOpened = new Set<number>();
|
||||||
|
const newBobOpened = new Set<number>();
|
||||||
|
for (let j = 0; j <= i; j++) {
|
||||||
|
newAliceOpened.add(aliceOrder[j]);
|
||||||
|
newBobOpened.add(bobOrder[j]);
|
||||||
|
}
|
||||||
|
|
||||||
const aliceFoundPresent = presentBoxes.has(aliceBox);
|
const aliceFoundPresent = presentBoxes.has(aliceBox);
|
||||||
const bobFoundPresent = presentBoxes.has(bobBox);
|
const bobFoundPresent = presentBoxes.has(bobBox);
|
||||||
if (aliceFoundPresent) {
|
if (aliceFoundPresent) aliceCount++;
|
||||||
aliceCount++;
|
if (bobFoundPresent) bobCount++;
|
||||||
setAliceFound(aliceCount);
|
|
||||||
}
|
// Update all state together for perfect sync
|
||||||
if (bobFoundPresent) {
|
setAliceOpened(newAliceOpened);
|
||||||
bobCount++;
|
setBobOpened(newBobOpened);
|
||||||
setBobFound(bobCount);
|
setAliceFound(aliceCount);
|
||||||
}
|
setBobFound(bobCount);
|
||||||
|
setCurrentStep(i);
|
||||||
|
|
||||||
if (aliceCount === 26 && bobCount === 26) {
|
if (aliceCount === 26 && bobCount === 26) {
|
||||||
currentWinner = 'tie';
|
currentWinner = 'tie';
|
||||||
|
|
@ -268,7 +282,7 @@ const PresentsPuzzle = ({
|
||||||
setBobOpened(new Set());
|
setBobOpened(new Set());
|
||||||
setAliceFound(0);
|
setAliceFound(0);
|
||||||
setBobFound(0);
|
setBobFound(0);
|
||||||
setCurrentStep(0);
|
setCurrentStep(-1); // -1 means no boxes opened yet
|
||||||
setWinner(null);
|
setWinner(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -282,7 +296,7 @@ const PresentsPuzzle = ({
|
||||||
setWinner(null);
|
setWinner(null);
|
||||||
setIsRunning(false);
|
setIsRunning(false);
|
||||||
isPausedRef.current = false;
|
isPausedRef.current = false;
|
||||||
setCurrentStep(0);
|
setCurrentStep(-1); // -1 means not initialized
|
||||||
setAliceOrder([]);
|
setAliceOrder([]);
|
||||||
setBobOrder([]);
|
setBobOrder([]);
|
||||||
};
|
};
|
||||||
|
|
@ -392,7 +406,7 @@ const PresentsPuzzle = ({
|
||||||
className="gap-2"
|
className="gap-2"
|
||||||
>
|
>
|
||||||
<Play className="w-4 h-4" />
|
<Play className="w-4 h-4" />
|
||||||
{currentStep === 0 ? 'Start' : 'Resume'}
|
{currentStep === -1 ? 'Start' : 'Resume'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={pauseSimulation}
|
onClick={pauseSimulation}
|
||||||
|
|
@ -405,7 +419,7 @@ const PresentsPuzzle = ({
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={stepToStart}
|
onClick={stepToStart}
|
||||||
disabled={isRunning || currentStep === 0}
|
disabled={isRunning || currentStep <= -1}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
className="gap-2"
|
className="gap-2"
|
||||||
title="Step to start"
|
title="Step to start"
|
||||||
|
|
@ -414,7 +428,7 @@ const PresentsPuzzle = ({
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={stepBack}
|
onClick={stepBack}
|
||||||
disabled={isRunning || currentStep === 0}
|
disabled={isRunning || currentStep <= -1}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
className="gap-2"
|
className="gap-2"
|
||||||
title="Step back"
|
title="Step back"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue