import React, { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import { Badge } from '@/components/ui/badge'; import { RotateCcw, Trophy } from 'lucide-react'; import SocialShare from '@/components/SocialShare'; interface PebblePlacementGameProps { showSocialShare?: boolean; } const PebblePlacementGame: React.FC = ({ showSocialShare = false }) => { const [numPebbles, setNumPebbles] = useState(3); const [pebblesInHand, setPebblesInHand] = useState(3); const [placedPebbles, setPlacedPebbles] = useState>(new Set()); const [furthestReached, setFurthestReached] = useState(0); const [gameStarted, setGameStarted] = useState(false); // Reset game when number of pebbles changes useEffect(() => { resetGame(); }, [numPebbles]); // Update furthest reached whenever pebbles are placed useEffect(() => { if (placedPebbles.size > 0) { const maxPosition = Math.max(...Array.from(placedPebbles)); if (maxPosition > furthestReached) { setFurthestReached(maxPosition); } } }, [placedPebbles, furthestReached]); const resetGame = () => { setPebblesInHand(numPebbles); setPlacedPebbles(new Set()); setFurthestReached(0); setGameStarted(false); }; const startGame = () => { setGameStarted(true); }; const canPlacePebble = (position: number): boolean => { // Can always place on position 1 if (position === 1) return true; // For other positions, predecessor must have a pebble return placedPebbles.has(position - 1); }; const canRemovePebble = (position: number): boolean => { // Can only remove if pebble is placed if (!placedPebbles.has(position)) return false; // Position 1 is always removable if (position === 1) return true; // For other positions, predecessor must have pebble return placedPebbles.has(position - 1); }; const hasAnyRemovablePebble = (): boolean => { return Array.from(placedPebbles).some(pos => canRemovePebble(pos)); }; const handlePositionClick = (position: number) => { if (!gameStarted) return; if (placedPebbles.has(position)) { // Try to remove pebble if (canRemovePebble(position)) { const newPlacedPebbles = new Set(placedPebbles); newPlacedPebbles.delete(position); setPlacedPebbles(newPlacedPebbles); setPebblesInHand(pebblesInHand + 1); } } else { // Try to place pebble if (pebblesInHand > 0 && canPlacePebble(position)) { const newPlacedPebbles = new Set(placedPebbles); newPlacedPebbles.add(position); setPlacedPebbles(newPlacedPebbles); setPebblesInHand(pebblesInHand - 1); } } }; const getPositionState = (position: number): 'placed-removable' | 'placed-fixed' | 'available' | 'blocked' => { if (placedPebbles.has(position)) { return canRemovePebble(position) ? 'placed-removable' : 'placed-fixed'; } if (canPlacePebble(position) && pebblesInHand > 0) return 'available'; return 'blocked'; }; const getPositionStyle = (position: number): string => { const state = getPositionState(position); const baseClasses = "w-8 h-8 border-2 rounded-full flex items-center justify-center text-sm font-medium cursor-pointer transition-all duration-200"; switch (state) { case 'placed-removable': return `${baseClasses} bg-green-100 text-green-800 border-green-200 hover:bg-green-200 dark:bg-green-900/30 dark:text-green-300 dark:border-green-700`; case 'placed-fixed': return `${baseClasses} bg-red-100 text-red-800 border-red-200 cursor-not-allowed dark:bg-red-900/30 dark:text-red-300 dark:border-red-700`; case 'available': return `${baseClasses} bg-secondary text-secondary-foreground border-primary border-2 hover:bg-secondary/80 hover:scale-110 shadow-sm`; case 'blocked': return `${baseClasses} bg-muted text-muted-foreground border-muted cursor-not-allowed opacity-50`; } }; return (
Pebble Placement Strategy Game Place your pebbles strategically to reach the furthest position possible! {/* Game Setup */}
setNumPebbles(value[0])} min={2} max={6} step={1} className="w-full max-w-xs" disabled={gameStarted} />
{!gameStarted ? ( ) : (
Pebbles in hand: {pebblesInHand} {furthestReached > 0 && ( Furthest: {furthestReached} )}
)}
{/* Rules */}

Rules:

  • • You can always place a pebble on position 1
  • • You can place a pebble on position i only if position (i-1) has a pebble
  • • You can remove a pebble from position i only if position (i-1) has a pebble
  • • The pebble on position 1 is always removable
  • • Goal: Place a pebble on the largest possible position
{/* Game Board */} {gameStarted && (

Positions 1-100

{/* All positions in a flowing line */}
{Array.from({ length: 100 }, (_, i) => { const position = i + 1; return (
handlePositionClick(position)} title={ placedPebbles.has(position) ? canRemovePebble(position) ? `Pebble placed on ${position}. Click to remove.` : `Pebble on ${position} cannot be removed (no predecessor pebble)` : canPlacePebble(position) && pebblesInHand > 0 ? `Click to place pebble on ${position}` : `Cannot place pebble on ${position}` } > {position}
); })}
)} {/* Game Status */} {gameStarted && pebblesInHand === 0 && (

All pebbles placed!

Your furthest position: {furthestReached}

{hasAnyRemovablePebble() ? (

You can still rearrange your pebbles to try reaching further!

) : (

No more moves possible - great job!

)}
)}
{showSocialShare && ( )}
); }; export default PebblePlacementGame;