import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import confetti from 'canvas-confetti'; import SocialShare from '@/components/SocialShare'; interface SubtractionGameProps { showSocialShare?: boolean; } const SubtractionGame: React.FC = ({ showSocialShare = true }) => { const [n, setN] = useState([10]); const [k, setK] = useState([3]); const [gameStarted, setGameStarted] = useState(false); const [circles, setCircles] = useState([]); const [removedCircles, setRemovedCircles] = useState>(new Set()); const [removedByPlayer, setRemovedByPlayer] = useState>(new Map()); const [currentPlayer, setCurrentPlayer] = useState<'Lata' | 'Raj'>('Lata'); const [winner, setWinner] = useState(null); const [highlightedIndices, setHighlightedIndices] = useState([]); const [hoveredIndex, setHoveredIndex] = useState(null); // 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 activeCircles = circles.filter(i => !removedCircles.has(i)); const lastKIndices = []; const activeCount = activeCircles.length; const start = Math.max(0, activeCount - k[0]); for (let i = start; i < activeCount; i++) { lastKIndices.push(activeCircles[i]); } setHighlightedIndices(lastKIndices); } }, [circles, k, removedCircles]); const startGame = () => { const initialCircles = Array.from({ length: n[0] }, (_, i) => i); setCircles(initialCircles); setRemovedCircles(new Set()); setRemovedByPlayer(new Map()); setGameStarted(true); setCurrentPlayer('Lata'); setWinner(null); }; const resetGame = () => { setGameStarted(false); setCircles([]); setRemovedCircles(new Set()); setRemovedByPlayer(new Map()); setWinner(null); setCurrentPlayer('Lata'); setHighlightedIndices([]); setHoveredIndex(null); }; const handleCircleClick = (index: number) => { if (!gameStarted || winner || !highlightedIndices.includes(index) || removedCircles.has(index)) { return; } // Mark all circles from clicked index onwards as removed const newRemovedCircles = new Set(removedCircles); const newRemovedByPlayer = new Map(removedByPlayer); const activeCircles = circles.filter(i => !removedCircles.has(i)); const clickedPosition = activeCircles.indexOf(index); // Remove all circles from clicked position onwards for (let i = clickedPosition; i < activeCircles.length; i++) { newRemovedCircles.add(activeCircles[i]); newRemovedByPlayer.set(activeCircles[i], currentPlayer); } setRemovedCircles(newRemovedCircles); setRemovedByPlayer(newRemovedByPlayer); // Check if game is over (no active circles left) const remainingActive = circles.filter(i => !newRemovedCircles.has(i)); if (remainingActive.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-blue-100 dark:bg-blue-950' : 'bg-purple-100 dark:bg-purple-950'; }; const getCircleClass = (index: number) => { const isRemoved = removedCircles.has(index); const isHighlighted = highlightedIndices.includes(index); const isHovered = hoveredIndex !== null && index >= hoveredIndex; if (isRemoved) { const removedBy = removedByPlayer.get(index); const borderColor = removedBy === 'Lata' ? 'border-blue-400' : 'border-purple-400'; return `w-8 h-8 rounded-full border-2 border-dashed bg-transparent ${borderColor}`; } const baseClass = `w-8 h-8 rounded-full border-2 border-black transition-colors ${isHighlighted ? 'cursor-pointer' : ''}`; let fillClass; if (isHovered && isHighlighted) { fillClass = 'bg-red-300 hover:bg-red-400'; } else if (isHighlighted) { fillClass = 'bg-yellow-200 hover:bg-yellow-300'; } else { fillClass = 'bg-gray-300'; } 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)} onMouseEnter={() => (highlightedIndices.includes(index) && !removedCircles.has(index)) ? setHoveredIndex(index) : null} onMouseLeave={() => setHoveredIndex(null)} title={highlightedIndices.includes(index) ? 'Click to select' : 'Not selectable'} /> ))}
{circles.length > 0 && (

{highlightedIndices.length} circle(s) highlighted • Last {Math.min(k[0], circles.filter(i => !removedCircles.has(i)).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
{showSocialShare && ( )}
); }; export default SubtractionGame;