import React, { useState, useEffect, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import SocialShare from '@/components/SocialShare'; const POWERS_OF_TWO = [128, 64, 32, 16, 8, 4, 2, 1]; interface BinaryNumberGameProps { showSocialShare?: boolean; } const BinaryNumberGame: React.FC = ({ showSocialShare = true }) => { const [targetNumber, setTargetNumber] = useState(0); const [flippedCards, setFlippedCards] = useState(new Array(8).fill(false)); const [currentSum, setCurrentSum] = useState(0); const [timer, setTimer] = useState(0); const [isGameActive, setIsGameActive] = useState(false); const [isGameWon, setIsGameWon] = useState(false); const [hasGameStarted, setHasGameStarted] = useState(false); const [highScore, setHighScore] = useState(null); // Load high score from localStorage on component mount useEffect(() => { const savedHighScore = localStorage.getItem('binary-game-high-score'); if (savedHighScore) { setHighScore(parseInt(savedHighScore)); } }, []); // Start new game const startNewGame = useCallback(() => { const newTarget = Math.floor(Math.random() * 257); // 0-256 setTargetNumber(newTarget); setFlippedCards(new Array(8).fill(false)); setCurrentSum(0); setTimer(0); setIsGameActive(true); setIsGameWon(false); setHasGameStarted(true); }, []); // Start game for the first time const startGame = () => { startNewGame(); }; // Timer effect useEffect(() => { let interval: NodeJS.Timeout; if (isGameActive && !isGameWon) { interval = setInterval(() => { setTimer(prev => prev + 0.1); }, 100); } return () => clearInterval(interval); }, [isGameActive, isGameWon]); // Check for win condition useEffect(() => { if (currentSum === targetNumber && isGameActive) { setIsGameActive(false); setIsGameWon(true); // Update high score const currentTime = Math.round(timer * 10) / 10; if (highScore === null || currentTime < highScore) { setHighScore(currentTime); localStorage.setItem('binary-game-high-score', currentTime.toString()); } } }, [currentSum, targetNumber, isGameActive, timer, highScore]); // Calculate current sum useEffect(() => { const sum = flippedCards.reduce((acc, isFlipped, index) => { return isFlipped ? acc + POWERS_OF_TWO[index] : acc; }, 0); setCurrentSum(sum); }, [flippedCards]); const toggleCard = (index: number) => { if (!isGameActive || isGameWon) return; setFlippedCards(prev => { const newFlipped = [...prev]; newFlipped[index] = !newFlipped[index]; return newFlipped; }); }; return (

Binary Number Representation

Click the cards to represent the target number as a sum of powers of two!

{highScore !== null && ( Best Time: {highScore}s )}
{/* Cards representing powers of two */}
{POWERS_OF_TWO.map((power, index) => (
toggleCard(index)} > {flippedCards[index] ? ( {power} ) : (
)}
{power}
))}
{/* Sum display boxes */}

Current Sum

targetNumber ? "text-red-600" : "text-foreground"}`}>{currentSum}

Target Sum

{!hasGameStarted ? "🎯" : isGameWon ? targetNumber.toString(2) : targetNumber}
{/* Timer and controls */}
Time: {Math.round(timer * 10) / 10}s
{isGameWon && (
Congratulations! 🎉
Your score: {Math.round(timer * 10) / 10} seconds
{highScore === Math.round(timer * 10) / 10 && ( New High Score! 🏆 )}
)}
{showSocialShare && ( )}
); }; export default BinaryNumberGame;