import { useState, useEffect } from 'react' import './App.css' // Game question structure interface ComparisonQuestion { leftSide: string[]; rightSide: string[]; isEqual: boolean; secondStepQuestion: 'more' | 'fewer'; } // Game states type GameStep = 'step1' | 'step2' | 'correct-step1' | 'correct-step2'; function App() { const [currentQuestion, setCurrentQuestion] = useState(null); const [gameStep, setGameStep] = useState('step1'); const [feedback, setFeedback] = useState(''); const [showFeedback, setShowFeedback] = useState(false); const [buttonLabels, setButtonLabels] = useState({ equal: 'Equal', notEqual: 'Not Equal' }); const [questionTitle, setQuestionTitle] = useState('Are they equal?'); // Emoji pools for different categories const emojiCategories = { animals: ['🐶', '🐱', '🐭', '🐹', '🐰', '🦊', '🐻', '🐼', '🐸', '🐷'], fruits: ['🍎', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🫐', '🍒', '🥝'], objects: ['⚽', '🏀', '🎾', '🏈', '🎱', '🎪', '🎨', '🎭', '🎪', '🎯'], hearts: ['💙', '💚', '💛', '💜', '🤍', '🖤', '🤎', '💗', '💖', '💕'], stars: ['⭐', '🌟', '💫', '✨', '🌠', '⚡', '🔥', '❄️', '☀️', '🌙'] }; // Generate random button labels and question title const generateButtonLabels = () => { const equalOptions = ['Equal', 'Same']; const notEqualOptions = ['Not Equal', 'Different']; return { equal: equalOptions[Math.floor(Math.random() * equalOptions.length)], notEqual: notEqualOptions[Math.floor(Math.random() * notEqualOptions.length)] }; }; const generateQuestionTitle = () => { const titleOptions = ['Are they equal?', 'Are they same?']; return titleOptions[Math.floor(Math.random() * titleOptions.length)]; }; // Generate a random question const generateQuestion = (): ComparisonQuestion => { const categories = Object.keys(emojiCategories) as (keyof typeof emojiCategories)[]; const selectedCategory = categories[Math.floor(Math.random() * categories.length)]; const emojis = emojiCategories[selectedCategory]; // Pick a random emoji for this question const emoji = emojis[Math.floor(Math.random() * emojis.length)]; // Generate counts (1-10) const leftCount = Math.floor(Math.random() * 10) + 1; let rightCount = Math.floor(Math.random() * 10) + 1; // 50% chance of making them equal const shouldBeEqual = Math.random() < 0.5; if (shouldBeEqual) { rightCount = leftCount; } // Create arrays with repeated emoji const leftSide = Array(leftCount).fill(emoji); const rightSide = Array(rightCount).fill(emoji); // Determine second step question type const secondStepQuestion: 'more' | 'fewer' = Math.random() < 0.5 ? 'more' : 'fewer'; // Generate new button labels and question title for this question setButtonLabels(generateButtonLabels()); setQuestionTitle(generateQuestionTitle()); return { leftSide, rightSide, isEqual: leftCount === rightCount, secondStepQuestion }; }; // Initialize with first question useEffect(() => { setCurrentQuestion(generateQuestion()); }, []); // Safety effect: if we're in step 2 but sides are equal, generate new question useEffect(() => { if (currentQuestion && (gameStep === 'step2' || gameStep === 'correct-step2') && currentQuestion.leftSide.length === currentQuestion.rightSide.length) { // Generate new question and go back to step 1 setTimeout(() => { setCurrentQuestion(generateQuestion()); setGameStep('step1'); }, 1000); } }, [currentQuestion, gameStep]); // Handle step 1 answer (equal/not equal) const handleStep1Answer = (answer: 'equal' | 'not-equal') => { if (!currentQuestion) return; const isCorrect = (answer === 'equal') === currentQuestion.isEqual; if (isCorrect) { setFeedback('Correct! 🎉'); setShowFeedback(true); setGameStep('correct-step1'); // If they correctly identified as "equal", generate new question // If they correctly identified as "not equal", proceed to step 2 if (answer === 'equal') { // Generate new question after delay setTimeout(() => { setCurrentQuestion(generateQuestion()); setGameStep('step1'); setShowFeedback(false); setFeedback(''); }, 2000); } else { // Proceed to step 2 only if they are NOT equal setTimeout(() => { setGameStep('step2'); setShowFeedback(false); setFeedback(''); }, 1500); } } else { setFeedback('Try again! 🤔'); setShowFeedback(true); setTimeout(() => { setShowFeedback(false); setFeedback(''); }, 1000); } }; // Handle step 2 answer (more/fewer) const handleStep2Answer = (answer: 'left' | 'right') => { if (!currentQuestion) return; const leftCount = currentQuestion.leftSide.length; const rightCount = currentQuestion.rightSide.length; // Safety check: if sides are equal, this shouldn't happen in step 2 if (leftCount === rightCount) { // Generate new question and go back to step 1 setCurrentQuestion(generateQuestion()); setGameStep('step1'); return; } let isCorrect = false; if (currentQuestion.secondStepQuestion === 'more') { if (leftCount > rightCount && answer === 'left') isCorrect = true; if (rightCount > leftCount && answer === 'right') isCorrect = true; } else { // fewer if (leftCount < rightCount && answer === 'left') isCorrect = true; if (rightCount < leftCount && answer === 'right') isCorrect = true; } if (isCorrect) { setFeedback('Excellent! 🌟'); setShowFeedback(true); setGameStep('correct-step2'); // Generate new question after delay setTimeout(() => { setCurrentQuestion(generateQuestion()); setGameStep('step1'); setShowFeedback(false); setFeedback(''); }, 2000); } else { setFeedback('Try again! 🤔'); setShowFeedback(true); setTimeout(() => { setShowFeedback(false); setFeedback(''); }, 1000); } }; // Render emoji grid const renderEmojiSide = (emojis: string[], side: 'left' | 'right') => { const gridClass = `emoji-grid ${side}`; return (
{emojis.map((emoji, index) => ( {emoji} ))}
); }; if (!currentQuestion) { return
Loading...
; } return (
{gameStep === 'step1' || gameStep === 'correct-step1' ? ( // Step 1: Are they equal? <>

{questionTitle}

{renderEmojiSide(currentQuestion.leftSide, 'left')}
{currentQuestion.leftSide.length}
VS
{renderEmojiSide(currentQuestion.rightSide, 'right')}
{currentQuestion.rightSide.length}
{gameStep === 'step1' && (
)} ) : ( // Step 2: Which side has more/fewer? (Only if sides are NOT equal) currentQuestion.leftSide.length !== currentQuestion.rightSide.length ? ( <>

Which side has {currentQuestion.secondStepQuestion}?

handleStep2Answer('left')}> {renderEmojiSide(currentQuestion.leftSide, 'left')}
{currentQuestion.leftSide.length}
VS
handleStep2Answer('right')}> {renderEmojiSide(currentQuestion.rightSide, 'right')}
{currentQuestion.rightSide.length}
Click on the side with {currentQuestion.secondStepQuestion} items
) : ( // If somehow we get to step 2 with equal sides, generate new question
Generating new question...
) )} {showFeedback && (
{feedback}
)}
); } export default App;