From a1945611ef393cc6e474cc8c20b9eda53564bc23 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:09:13 +0000 Subject: [PATCH] Add pause functionality to Bulgarian Solitaire Update the "Playing..." button to "Pause". When clicked, the simulation will pause and the button text will change to "Continue", allowing the user to resume or reset the game. --- src/components/BulgarianSolitaire.tsx | 38 ++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/components/BulgarianSolitaire.tsx b/src/components/BulgarianSolitaire.tsx index 36f3a88..27b6fc6 100644 --- a/src/components/BulgarianSolitaire.tsx +++ b/src/components/BulgarianSolitaire.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import SocialShare from '@/components/SocialShare'; @@ -18,6 +18,7 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare const [boxes, setBoxes] = useState([]); const [columns, setColumns] = useState([]); const [isPlaying, setIsPlaying] = useState(false); + const [isPaused, setIsPaused] = useState(false); const [draggedBox, setDraggedBox] = useState(null); const [step, setStep] = useState(0); const [isComplete, setIsComplete] = useState(false); @@ -26,6 +27,8 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare const [lastUsedColumn, setLastUsedColumn] = useState(null); const [selectedBoxForMove, setSelectedBoxForMove] = useState(null); const [showInstructions, setShowInstructions] = useState(false); + + const pauseRef = useRef(false); const totalBoxes = n; @@ -173,6 +176,22 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + const waitForUnpause = async () => { + while (pauseRef.current) { + await sleep(100); + } + }; + + const handlePause = () => { + pauseRef.current = true; + setIsPaused(true); + }; + + const handleResume = () => { + pauseRef.current = false; + setIsPaused(false); + }; + const isTriangularConfiguration = (cols: number[][]) => { const nonEmptyCols = cols.filter(col => col.length > 0).sort((a, b) => a.length - b.length); @@ -215,6 +234,7 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare setHighlightedBoxes(topBoxes); setIsAnimating(true); await sleep(800); // Show highlighting + await waitForUnpause(); // Check for pause // Create new pile from non-empty columns const newPile: number[] = []; @@ -265,6 +285,7 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare })); await sleep(1200); // Wait to see the sorted result + await waitForUnpause(); // Check for pause again } setIsComplete(isTriangularConfiguration(currentColumns)); @@ -278,6 +299,10 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare }; const reset = () => { + pauseRef.current = false; + setIsPaused(false); + setIsPlaying(false); + const newBoxes: Box[] = []; for (let i = 0; i < totalBoxes; i++) { newBoxes.push({ id: i, columnIndex: null }); @@ -286,7 +311,6 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare setColumns(Array.from({ length: totalBoxes }, () => [])); setStep(0); setIsComplete(false); - setIsPlaying(false); setHighlightedBoxes(new Set()); setIsAnimating(false); }; @@ -347,10 +371,10 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare disabled={isPlaying} /> - - @@ -428,12 +452,12 @@ const BulgarianSolitaire: React.FC = ({ showSocialShare {/* Play Button */}