import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import confetti from 'canvas-confetti'; const SubtractionGame = () => { const [n, setN] = useState([10]); const [k, setK] = useState([3]); const [gameStarted, setGameStarted] = useState(false); const [circles, setCircles] = useState([]); const [currentPlayer, setCurrentPlayer] = useState<'Lata' | 'Raj'>('Lata'); const [winner, setWinner] = useState(null); const [highlightedIndices, setHighlightedIndices] = useState([]); // Update k slider max when n changes useEffect(() => { if (k[0] > n[0]) { setK([n[0]]); } }, [n, k]); // Calculate highlighted indices useEffect(() => { if (circles.length > 0) { const lastKIndices = []; const start = Math.max(0, circles.length - k[0]); for (let i = start; i < circles.length; i++) { lastKIndices.push(i); } setHighlightedIndices(lastKIndices); } }, [circles, k]); const startGame = () => { const initialCircles = Array.from({ length: n[0] }, (_, i) => i); setCircles(initialCircles); setGameStarted(true); setCurrentPlayer('Lata'); setWinner(null); }; const resetGame = () => { setGameStarted(false); setCircles([]); setWinner(null); setCurrentPlayer('Lata'); setHighlightedIndices([]); }; const handleCircleClick = (index: number) => { if (!gameStarted || winner || !highlightedIndices.includes(index)) { return; } // Remove all circles to the right of clicked circle const newCircles = circles.slice(0, index); setCircles(newCircles); // Check if game is over if (newCircles.length === 0) { setWinner(currentPlayer); setGameStarted(false); // Trigger confetti confetti({ particleCount: 100, spread: 70, origin: { y: 0.6 } }); return; } // Switch player setCurrentPlayer(currentPlayer === 'Lata' ? 'Raj' : 'Lata'); }; const getBackgroundColor = () => { if (!gameStarted || winner) return 'bg-background'; return currentPlayer === 'Lata' ? 'bg-red-100 dark:bg-red-950' : 'bg-purple-100 dark:bg-purple-950'; }; const getCircleClass = (index: number) => { const baseClass = 'w-8 h-8 rounded-full border-2 border-black cursor-pointer transition-colors'; const isHighlighted = highlightedIndices.includes(index); const fillClass = isHighlighted ? 'bg-yellow-200 hover:bg-yellow-300' : 'bg-gray-300 hover:bg-gray-400'; return `${baseClass} ${fillClass}`; }; return (

The Subtraction Game

{/* Game Controls */}
{/* N Slider */}
{/* K Slider */}
{/* Game Status */} {gameStarted && !winner && (

{currentPlayer}'s Turn

Click on any highlighted circle to make your move

)} {/* Winner Display */} {winner && (

🎉 {winner} Wins! 🎉

{winner} made the last move and won the game!

)} {/* Game Board */} {circles.length > 0 && (
{circles.map((_, index) => (
handleCircleClick(index)} title={highlightedIndices.includes(index) ? 'Click to select' : 'Not selectable'} /> ))}
{circles.length > 0 && (

{highlightedIndices.length} circle(s) highlighted • Last {Math.min(k[0], circles.length)} positions

)}
)} {/* Game Rules */}

How to Play

  • • Set the number of circles (n) and maximum selection range (k)
  • • Players take turns clicking on highlighted circles
  • • When you click a circle, all circles to its right disappear
  • • The last k circles (or fewer if less than k remain) are always highlighted
  • • The player who makes the last move wins!
  • • Background color indicates whose turn it is
); }; export default SubtractionGame;