import React, { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Badge } from '@/components/ui/badge'; import { Crown, Circle, RotateCcw, BookOpen, Trophy } from 'lucide-react'; import SocialShare from '@/components/SocialShare'; type Player = 0 | 1; type CellState = 'empty' | 'penny' | 'gold'; interface GameState { track: CellState[]; currentPlayer: Player; winner: Player | null; gameStarted: boolean; selectedCoin: number | null; moveHistory: string[]; } const GoldCoinGame: React.FC = () => { const [gameState, setGameState] = useState({ track: Array(15).fill('empty'), currentPlayer: 0, winner: null, gameStarted: false, selectedCoin: null, moveHistory: [] }); const [showRules, setShowRules] = useState(false); const [showGameOver, setShowGameOver] = useState(false); const playerNames = ['Player 1', 'Player 2']; const playerColors = ['#3b82f6', '#ef4444']; const generateRandomBoard = (): CellState[] => { const track = Array(15).fill('empty') as CellState[]; // Always place the gold coin at position 7-14 (random) const goldPosition = Math.floor(Math.random() * 8) + 7; track[goldPosition] = 'gold'; // Place 3-5 pennies randomly in positions 0-6 const numPennies = Math.floor(Math.random() * 3) + 3; // 3-5 pennies const availablePositions = Array.from({length: 7}, (_, i) => i); // positions 0-6 for (let i = 0; i < numPennies; i++) { if (availablePositions.length > 0) { const randomIndex = Math.floor(Math.random() * availablePositions.length); const position = availablePositions.splice(randomIndex, 1)[0]; track[position] = 'penny'; } } return track; }; const initializeGame = () => { setGameState({ track: generateRandomBoard(), currentPlayer: 0, winner: null, gameStarted: true, selectedCoin: null, moveHistory: [] }); }; const resetGame = () => { setGameState({ track: Array(15).fill('empty'), currentPlayer: 0, winner: null, gameStarted: false, selectedCoin: null, moveHistory: [] }); setShowGameOver(false); }; const canMoveCoin = (fromIndex: number, toIndex: number): boolean => { if (fromIndex <= toIndex) return false; // Can only move left if (toIndex < 0) return false; // Can't move off the track if (gameState.track[toIndex] !== 'empty') return false; // Can't move to a position with a coin // Check if there are any coins in the way for (let i = toIndex + 1; i < fromIndex; i++) { if (gameState.track[i] !== 'empty') { return false; } } return true; }; const canTakeCoin = (index: number): boolean => { // Can only take the leftmost coin for (let i = 0; i < index; i++) { if (gameState.track[i] !== 'empty') { return false; } } return gameState.track[index] !== 'empty'; }; const getValidMovePositions = (fromIndex: number): number[] => { const validPositions: number[] = []; for (let i = 0; i < fromIndex; i++) { if (canMoveCoin(fromIndex, i)) { validPositions.push(i); } } return validPositions; }; const handleCellClick = (index: number) => { if (gameState.winner !== null || !gameState.gameStarted) return; const cellState = gameState.track[index]; // If a coin is selected, try to move it if (gameState.selectedCoin !== null) { // If clicking on the same coin, deselect it if (gameState.selectedCoin === index) { setGameState(prev => ({ ...prev, selectedCoin: null })); return; } if (canMoveCoin(gameState.selectedCoin, index)) { const newTrack = [...gameState.track]; newTrack[index] = newTrack[gameState.selectedCoin]; newTrack[gameState.selectedCoin] = 'empty'; const moveDescription = `Player ${gameState.currentPlayer + 1} moved ${newTrack[index] === 'gold' ? 'gold coin' : 'penny'} from position ${gameState.selectedCoin + 1} to position ${index + 1}`; setGameState(prev => ({ ...prev, track: newTrack, currentPlayer: ((prev.currentPlayer + 1) % 2) as Player, selectedCoin: null, moveHistory: [...prev.moveHistory, moveDescription] })); } return; } // Select a coin if it's not empty if (cellState !== 'empty') { setGameState(prev => ({ ...prev, selectedCoin: index })); } }; const handleTakeCoin = () => { if (gameState.winner !== null || !gameState.gameStarted || gameState.selectedCoin === null) return; if (!canTakeCoin(gameState.selectedCoin)) return; const cellState = gameState.track[gameState.selectedCoin]; const newTrack = [...gameState.track]; newTrack[gameState.selectedCoin] = 'empty'; const moveDescription = `Player ${gameState.currentPlayer + 1} took ${cellState === 'gold' ? 'gold coin' : 'penny'} from position ${gameState.selectedCoin + 1}`; // Check if this is a winning move const isWinningMove = cellState === 'gold'; setGameState(prev => ({ ...prev, track: newTrack, currentPlayer: isWinningMove ? prev.currentPlayer : ((prev.currentPlayer + 1) % 2) as Player, winner: isWinningMove ? prev.currentPlayer : null, selectedCoin: null, moveHistory: [...prev.moveHistory, moveDescription] })); if (isWinningMove) { setShowGameOver(true); } }; const renderCell = (cellState: CellState, index: number) => { const isSelected = gameState.selectedCoin === index; const validPositions = gameState.selectedCoin !== null ? getValidMovePositions(gameState.selectedCoin) : []; const isValidMoveTarget = validPositions.includes(index); const isClickable = gameState.gameStarted && (cellState !== 'empty' || gameState.selectedCoin !== null); return (
handleCellClick(index)} > {cellState === 'gold' && (
)} {cellState === 'penny' && (
)}
{index + 1}
); }; const getLeftmostCoin = (): { index: number; type: string } | null => { for (let i = 0; i < gameState.track.length; i++) { if (gameState.track[i] !== 'empty') { return { index: i, type: gameState.track[i] }; } } return null; }; const leftmostCoin = getLeftmostCoin(); const canTakeSelectedCoin = gameState.selectedCoin !== null && canTakeCoin(gameState.selectedCoin); return (
The Gold Coin Game A strategic two-player game where you move coins to the left or take the leftmost coin. The player who takes the gold coin wins! {/* Game Controls */}
{!gameState.gameStarted ? ( ) : ( )}
{/* Game Status */} {gameState.gameStarted && (
Current Player: {playerNames[gameState.currentPlayer]}
{gameState.selectedCoin !== null && (
Selected coin at position {gameState.selectedCoin + 1}. Click a position to move it.
)}
)} {/* Game Board */}
{gameState.track.map((cellState, index) => (
{renderCell(cellState, index)}
))}
{/* Instructions */}

{!gameState.gameStarted ? ( "Click 'Start Game' to begin with a randomized board" ) : gameState.selectedCoin !== null ? ( canTakeSelectedCoin ? ( "Use the take button below to remove this coin" ) : ( "Click a highlighted position to move the selected coin" ) ) : ( "Click on a coin to select it, then click a position to move it left, or take the leftmost coin" )}

{/* Take Coin Button */} {canTakeSelectedCoin && (
)}
{/* Move History */} {gameState.moveHistory.length > 0 && (

Move History

{gameState.moveHistory.map((move, index) => (
{move}
))}
)}
{/* Rules Dialog */} The Gold Coin Game Rules

Objective

Be the first player to acquire the gold coin and win the game!

Setup

The game starts with coins placed on a 15-position track. The gold coin is positioned farthest to the right.

On Your Turn

You have two options:

  1. Move a coin: Move any coin to the left by one or more spaces. A coin cannot jump over another coin.
  2. Take a coin: Take the leftmost coin on the track into your possession.

Winning

The player who successfully takes the gold coin is declared the winner!

Strategy Tips

  • Try to control the position of the gold coin
  • Use pennies to block your opponent's moves
  • Plan several moves ahead
  • Don't always take the first available coin
  • Consider the spacing between coins carefully
{/* Game Over Dialog */} Game Over!
{playerNames[gameState.winner!]} wins!

Congratulations! {playerNames[gameState.winner!]} successfully acquired the gold coin.

); }; export default GoldCoinGame;