Add binary interactive to School Connect

Implement a binary representation interactive game with cards, a timer, and score tracking on the School Connect page.
This commit is contained in:
gpt-engineer-app[bot] 2025-07-19 13:16:46 +00:00
parent 02b53640b7
commit 435f7a3d43
3 changed files with 284 additions and 5 deletions

View file

@ -0,0 +1,168 @@
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';
const POWERS_OF_TWO = [128, 64, 32, 16, 8, 4, 2, 1];
const BinaryNumberGame = () => {
const [targetNumber, setTargetNumber] = useState<number>(0);
const [flippedCards, setFlippedCards] = useState<boolean[]>(new Array(8).fill(false));
const [currentSum, setCurrentSum] = useState<number>(0);
const [timer, setTimer] = useState<number>(0);
const [isGameActive, setIsGameActive] = useState<boolean>(false);
const [isGameWon, setIsGameWon] = useState<boolean>(false);
const [highScore, setHighScore] = useState<number | null>(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);
}, []);
// Initialize game on mount
useEffect(() => {
startNewGame();
}, [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 (
<div className="max-w-4xl mx-auto p-6 space-y-6">
<div className="text-center space-y-2">
<h2 className="text-2xl font-bold text-foreground">Binary Number Representation</h2>
<p className="text-muted-foreground">Click the cards to represent the target number as a sum of powers of two!</p>
{highScore !== null && (
<Badge variant="secondary" className="text-sm">
Best Time: {highScore}s
</Badge>
)}
</div>
{/* Cards representing powers of two */}
<div className="grid grid-cols-8 gap-3">
{POWERS_OF_TWO.map((power, index) => (
<div key={power} className="flex flex-col items-center space-y-2">
<Card
className={`cursor-pointer transition-all duration-200 hover:scale-105 aspect-square w-full ${
flippedCards[index]
? 'bg-blue-100 border-blue-300 shadow-md'
: 'bg-gradient-to-br from-pink-100 to-purple-100 border-pink-200'
}`}
onClick={() => toggleCard(index)}
>
<CardContent className="p-0 h-full flex items-center justify-center">
{flippedCards[index] ? (
<span className="text-2xl font-bold text-blue-800">{power}</span>
) : (
<div className="w-full h-full bg-gradient-to-br from-pink-200 via-purple-200 to-indigo-200 rounded-lg opacity-80 flex items-center justify-center">
<div className="w-8 h-8 bg-white rounded-full opacity-60"></div>
</div>
)}
</CardContent>
</Card>
<span className="text-xs text-muted-foreground font-medium">{power}</span>
</div>
))}
</div>
{/* Sum display boxes */}
<div className="grid grid-cols-2 gap-4">
<Card className="bg-secondary">
<CardContent className="p-6 text-center">
<h3 className="text-lg font-semibold text-muted-foreground mb-2">Current Sum</h3>
<div className="text-4xl font-bold text-foreground">{currentSum}</div>
</CardContent>
</Card>
<Card className="bg-primary/10 border-primary/20">
<CardContent className="p-6 text-center">
<h3 className="text-lg font-semibold text-muted-foreground mb-2">Target Sum</h3>
<div className="text-4xl font-bold text-primary">{targetNumber}</div>
</CardContent>
</Card>
</div>
{/* Timer and controls */}
<div className="text-center space-y-4">
<div className="text-2xl font-mono text-foreground">
Time: {Math.round(timer * 10) / 10}s
</div>
{isGameWon && (
<div className="space-y-2">
<div className="text-xl font-bold text-green-600">Congratulations! 🎉</div>
<div className="text-lg text-muted-foreground">
Your score: {Math.round(timer * 10) / 10} seconds
</div>
{highScore === Math.round(timer * 10) / 10 && (
<Badge variant="default" className="bg-yellow-500 text-yellow-900">
New High Score! 🏆
</Badge>
)}
</div>
)}
<Button onClick={startNewGame} size="lg" className="px-8">
{isGameWon ? 'Play Again' : 'New Game'}
</Button>
</div>
</div>
);
};
export default BinaryNumberGame;

View file

@ -0,0 +1,112 @@
import React, { useState } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import { Search } from 'lucide-react';
import BinaryNumberGame from '@/components/BinaryNumberGame';
interface Interactive {
id: string;
title: string;
description: string;
tags: string[];
component: React.ComponentType;
}
const interactives: Interactive[] = [
{
id: 'binary-number-game',
title: 'Binary Number Representation',
description: 'Learn binary representation by expressing numbers as sums of powers of two. Toggle cards to match the target sum!',
tags: ['binary', 'math', 'numbers', 'representation', 'powers-of-two'],
component: BinaryNumberGame,
},
];
const InteractiveGallery = () => {
const [searchTerm, setSearchTerm] = useState('');
const [selectedInteractive, setSelectedInteractive] = useState<Interactive | null>(null);
const filteredInteractives = interactives.filter(interactive =>
interactive.title.toLowerCase().includes(searchTerm.toLowerCase()) ||
interactive.description.toLowerCase().includes(searchTerm.toLowerCase()) ||
interactive.tags.some(tag => tag.toLowerCase().includes(searchTerm.toLowerCase()))
);
if (selectedInteractive) {
const InteractiveComponent = selectedInteractive.component;
return (
<div className="space-y-6">
<div className="flex items-center gap-4">
<button
onClick={() => setSelectedInteractive(null)}
className="text-primary hover:text-primary/80 font-medium"
>
Back to Gallery
</button>
<h1 className="text-2xl font-bold text-foreground">{selectedInteractive.title}</h1>
</div>
<div className="bg-background border rounded-lg p-6">
<InteractiveComponent />
</div>
</div>
);
}
return (
<div className="space-y-6">
<div className="space-y-4">
<h1 className="text-3xl font-bold text-foreground">School Connect Interactives</h1>
<p className="text-muted-foreground text-lg">
Interactive tools and resources designed to connect theoretical concepts with practical applications in educational settings.
</p>
</div>
{/* Search bar */}
<div className="relative max-w-md">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground w-4 h-4" />
<Input
placeholder="Search interactives..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10"
/>
</div>
{/* Gallery */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{filteredInteractives.map((interactive) => (
<Card
key={interactive.id}
className="cursor-pointer hover:shadow-lg transition-all duration-200 hover:scale-105"
onClick={() => setSelectedInteractive(interactive)}
>
<CardHeader className="space-y-3">
<CardTitle className="text-xl text-foreground">{interactive.title}</CardTitle>
<CardDescription className="text-muted-foreground line-clamp-3">
{interactive.description}
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex flex-wrap gap-2">
{interactive.tags.map((tag) => (
<Badge key={tag} variant="secondary" className="text-xs">
{tag}
</Badge>
))}
</div>
</CardContent>
</Card>
))}
</div>
{filteredInteractives.length === 0 && (
<div className="text-center py-12">
<p className="text-muted-foreground text-lg">No interactives found matching your search.</p>
</div>
)}
</div>
);
};
export default InteractiveGallery;

View file

@ -1,11 +1,10 @@
import ComingSoon from "@/components/ComingSoon";
import InteractiveGallery from "@/components/InteractiveGallery";
const SchoolConnect = () => {
return (
<ComingSoon
title="School Connect"
description="Interactive tools and resources designed to connect theoretical concepts with practical applications in educational settings, helping bridge the gap between learning and real-world application."
/>
<div className="container mx-auto px-4 py-8">
<InteractiveGallery />
</div>
);
};