From 3dad5e12dfc4aa81abff26557cf3a3592a949175 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 11:39:35 +0000 Subject: [PATCH] Add undo functionality Implemented an undo move feature for the subtraction game, allowing users to revert their last action. --- src/components/SubtractionGame.tsx | 36 +++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/components/SubtractionGame.tsx b/src/components/SubtractionGame.tsx index 4887d3e..2834753 100644 --- a/src/components/SubtractionGame.tsx +++ b/src/components/SubtractionGame.tsx @@ -19,6 +19,11 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru const [winner, setWinner] = useState(null); const [highlightedIndices, setHighlightedIndices] = useState([]); const [hoveredIndex, setHoveredIndex] = useState(null); + const [previousGameState, setPreviousGameState] = useState<{ + removedCircles: Set; + removedByPlayer: Map; + currentPlayer: 'Lata' | 'Raj'; + } | null>(null); // Update k slider max when n changes useEffect(() => { @@ -49,6 +54,7 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru setGameStarted(true); setCurrentPlayer('Lata'); setWinner(null); + setPreviousGameState(null); }; const resetGame = () => { @@ -60,6 +66,7 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru setCurrentPlayer('Lata'); setHighlightedIndices([]); setHoveredIndex(null); + setPreviousGameState(null); }; const handleCircleClick = (index: number) => { @@ -67,6 +74,13 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru return; } + // Store current state before making the move + setPreviousGameState({ + removedCircles: new Set(removedCircles), + removedByPlayer: new Map(removedByPlayer), + currentPlayer + }); + // Mark all circles from clicked index onwards as removed const newRemovedCircles = new Set(removedCircles); const newRemovedByPlayer = new Map(removedByPlayer); @@ -100,6 +114,15 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru setCurrentPlayer(currentPlayer === 'Lata' ? 'Raj' : 'Lata'); }; + const undoLastMove = () => { + if (!previousGameState) return; + + setRemovedCircles(previousGameState.removedCircles); + setRemovedByPlayer(previousGameState.removedByPlayer); + setCurrentPlayer(previousGameState.currentPlayer); + setPreviousGameState(null); + }; + const getBackgroundColor = () => { if (!gameStarted || winner) return 'bg-background'; return currentPlayer === 'Lata' ? 'bg-blue-100 dark:bg-blue-950' : 'bg-purple-100 dark:bg-purple-950'; @@ -172,7 +195,7 @@ const SubtractionGame: React.FC = ({ showSocialShare = tru -
+
+ + {gameStarted && !winner && previousGameState && ( + + )}