import React, { useState, useCallback } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { RotateCcw, Undo2 } from 'lucide-react'; import SocialShare from '@/components/SocialShare'; interface Edge { from: number; to: number; color: 'red' | 'blue' | null; } interface GameState { edges: Edge[]; currentPlayer: 'red' | 'blue'; gameOver: boolean; winner: 'red' | 'blue' | null; moveHistory: Edge[]; } interface GameOfSimProps { showSocialShare?: boolean; } const GameOfSim: React.FC = ({ showSocialShare = true }) => { const [vertices, setVertices] = useState(6); const [gameState, setGameState] = useState(() => initializeGame(6)); function initializeGame(numVertices: number): GameState { const edges: Edge[] = []; // Create all possible edges between vertices for (let i = 0; i < numVertices; i++) { for (let j = i + 1; j < numVertices; j++) { edges.push({ from: i, to: j, color: null }); } } return { edges, currentPlayer: 'red', gameOver: false, winner: null, moveHistory: [] }; } const resetGame = useCallback(() => { setGameState(initializeGame(vertices)); }, [vertices]); const undoMove = useCallback(() => { if (gameState.moveHistory.length === 0) return; const lastMove = gameState.moveHistory[gameState.moveHistory.length - 1]; const newEdges = gameState.edges.map(edge => { if (edge.from === lastMove.from && edge.to === lastMove.to) { return { ...edge, color: null }; } return edge; }); setGameState(prev => ({ ...prev, edges: newEdges, currentPlayer: prev.currentPlayer === 'red' ? 'blue' : 'red', moveHistory: prev.moveHistory.slice(0, -1), gameOver: false, winner: null })); }, [gameState.moveHistory, gameState.edges]); const checkForTriangle = useCallback((edges: Edge[], player: 'red' | 'blue'): boolean => { const playerEdges = edges.filter(edge => edge.color === player); // Check all possible triangles for (let i = 0; i < vertices; i++) { for (let j = i + 1; j < vertices; j++) { for (let k = j + 1; k < vertices; k++) { // Check if all three edges of triangle exist for this player const edge1 = playerEdges.find(e => (e.from === i && e.to === j) || (e.from === j && e.to === i)); const edge2 = playerEdges.find(e => (e.from === i && e.to === k) || (e.from === k && e.to === i)); const edge3 = playerEdges.find(e => (e.from === j && e.to === k) || (e.from === k && e.to === j)); if (edge1 && edge2 && edge3) { return true; } } } } return false; }, [vertices]); const handleEdgeClick = useCallback((edgeIndex: number) => { if (gameState.gameOver || gameState.edges[edgeIndex].color !== null) return; const newEdges = [...gameState.edges]; const clickedEdge = { ...newEdges[edgeIndex], color: gameState.currentPlayer }; newEdges[edgeIndex] = clickedEdge; // Check if this move creates a triangle const hasTriangle = checkForTriangle(newEdges, gameState.currentPlayer); setGameState(prev => ({ edges: newEdges, currentPlayer: hasTriangle ? prev.currentPlayer : (prev.currentPlayer === 'red' ? 'blue' : 'red'), gameOver: hasTriangle, winner: hasTriangle ? (prev.currentPlayer === 'red' ? 'blue' : 'red') : null, moveHistory: [...prev.moveHistory, clickedEdge] })); }, [gameState, checkForTriangle]); const getVertexPosition = (index: number, total: number) => { const angle = (2 * Math.PI * index) / total; const radius = 120; const centerX = 150; const centerY = 150; return { x: centerX + radius * Math.cos(angle - Math.PI / 2), y: centerY + radius * Math.sin(angle - Math.PI / 2) }; }; const handleVerticesChange = (value: string) => { const newVertices = parseInt(value); setVertices(newVertices); setGameState(initializeGame(newVertices)); }; return (
Game of Sim Take turns coloring the lines between dots. Avoid creating a triangle of your color - first player to make one loses! {/* Game Controls */}
{/* Current Turn Indicator */}
{gameState.gameOver ? (
Game Over!

{gameState.winner === 'blue' ? 'Blue' : 'Red'} Player {' '} wins!

) : (

Current Turn:

{gameState.currentPlayer === 'red' ? 'Red' : 'Blue'} Player
)}
{/* Game Board */}
{/* Draw edges */} {gameState.edges.map((edge, index) => { const pos1 = getVertexPosition(edge.from, vertices); const pos2 = getVertexPosition(edge.to, vertices); return ( handleEdgeClick(index)} /> ); })} {/* Draw vertices */} {Array.from({ length: vertices }, (_, i) => { const pos = getVertexPosition(i, vertices); return ( {i + 1} ); })}
{/* Instructions */}

Click on the lines between dots to color them with your color.

Avoid creating a triangle of your own color!

{/* Social Share */} {showSocialShare && (
)}
); }; export default GameOfSim;