Add undo functionality
Implemented an undo move feature for the subtraction game, allowing users to revert their last action.
This commit is contained in:
parent
28e09a1e28
commit
3dad5e12df
1 changed files with 35 additions and 1 deletions
|
|
@ -19,6 +19,11 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
const [winner, setWinner] = useState<string | null>(null);
|
const [winner, setWinner] = useState<string | null>(null);
|
||||||
const [highlightedIndices, setHighlightedIndices] = useState<number[]>([]);
|
const [highlightedIndices, setHighlightedIndices] = useState<number[]>([]);
|
||||||
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
|
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
|
||||||
|
const [previousGameState, setPreviousGameState] = useState<{
|
||||||
|
removedCircles: Set<number>;
|
||||||
|
removedByPlayer: Map<number, 'Lata' | 'Raj'>;
|
||||||
|
currentPlayer: 'Lata' | 'Raj';
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
// Update k slider max when n changes
|
// Update k slider max when n changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -49,6 +54,7 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
setGameStarted(true);
|
setGameStarted(true);
|
||||||
setCurrentPlayer('Lata');
|
setCurrentPlayer('Lata');
|
||||||
setWinner(null);
|
setWinner(null);
|
||||||
|
setPreviousGameState(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetGame = () => {
|
const resetGame = () => {
|
||||||
|
|
@ -60,6 +66,7 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
setCurrentPlayer('Lata');
|
setCurrentPlayer('Lata');
|
||||||
setHighlightedIndices([]);
|
setHighlightedIndices([]);
|
||||||
setHoveredIndex(null);
|
setHoveredIndex(null);
|
||||||
|
setPreviousGameState(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCircleClick = (index: number) => {
|
const handleCircleClick = (index: number) => {
|
||||||
|
|
@ -67,6 +74,13 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
return;
|
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
|
// Mark all circles from clicked index onwards as removed
|
||||||
const newRemovedCircles = new Set(removedCircles);
|
const newRemovedCircles = new Set(removedCircles);
|
||||||
const newRemovedByPlayer = new Map(removedByPlayer);
|
const newRemovedByPlayer = new Map(removedByPlayer);
|
||||||
|
|
@ -100,6 +114,15 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
setCurrentPlayer(currentPlayer === 'Lata' ? 'Raj' : 'Lata');
|
setCurrentPlayer(currentPlayer === 'Lata' ? 'Raj' : 'Lata');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const undoLastMove = () => {
|
||||||
|
if (!previousGameState) return;
|
||||||
|
|
||||||
|
setRemovedCircles(previousGameState.removedCircles);
|
||||||
|
setRemovedByPlayer(previousGameState.removedByPlayer);
|
||||||
|
setCurrentPlayer(previousGameState.currentPlayer);
|
||||||
|
setPreviousGameState(null);
|
||||||
|
};
|
||||||
|
|
||||||
const getBackgroundColor = () => {
|
const getBackgroundColor = () => {
|
||||||
if (!gameStarted || winner) return 'bg-background';
|
if (!gameStarted || winner) return 'bg-background';
|
||||||
return currentPlayer === 'Lata' ? 'bg-blue-100 dark:bg-blue-950' : 'bg-purple-100 dark:bg-purple-950';
|
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<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="text-center">
|
<div className="flex justify-center gap-4">
|
||||||
<Button
|
<Button
|
||||||
onClick={gameStarted ? resetGame : startGame}
|
onClick={gameStarted ? resetGame : startGame}
|
||||||
size="lg"
|
size="lg"
|
||||||
|
|
@ -180,6 +203,17 @@ const SubtractionGame: React.FC<SubtractionGameProps> = ({ showSocialShare = tru
|
||||||
>
|
>
|
||||||
{gameStarted ? 'Reset' : 'Play'}
|
{gameStarted ? 'Reset' : 'Play'}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
{gameStarted && !winner && previousGameState && (
|
||||||
|
<Button
|
||||||
|
onClick={undoLastMove}
|
||||||
|
variant="outline"
|
||||||
|
size="lg"
|
||||||
|
className="px-6"
|
||||||
|
>
|
||||||
|
Undo Last Move
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue