Spaces:
Sleeping
Sleeping
File size: 9,796 Bytes
0b49cb3 a432b46 0b49cb3 3e4b4c2 0b49cb3 a432b46 0b49cb3 c133828 0b49cb3 c133828 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 c133828 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 3e4b4c2 0b49cb3 a432b46 0b49cb3 a432b46 0b49cb3 a432b46 0b49cb3 a432b46 0b49cb3 3e4b4c2 0b49cb3 a432b46 0b49cb3 a432b46 0b49cb3 a432b46 0b49cb3 3e4b4c2 0b49cb3 a432b46 0b49cb3 c133828 0b49cb3 a432b46 0b49cb3 c133828 0b49cb3 c133828 0b49cb3 3e4b4c2 0b49cb3 a432b46 3e4b4c2 a432b46 3e4b4c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
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<ComparisonQuestion | null>(null);
const [gameStep, setGameStep] = useState<GameStep>('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 (
<div className={gridClass}>
{emojis.map((emoji, index) => (
<span key={index} className="emoji-item">
{emoji}
</span>
))}
</div>
);
};
if (!currentQuestion) {
return <div className="loading">Loading...</div>;
}
return (
<div className="equal-game">
{gameStep === 'step1' || gameStep === 'correct-step1' ? (
// Step 1: Are they equal?
<>
<h1 className="question-title">{questionTitle}</h1>
<div className="comparison-container">
<div className="side-container">
{renderEmojiSide(currentQuestion.leftSide, 'left')}
<div className="count-display">{currentQuestion.leftSide.length}</div>
</div>
<div className="vs-divider">VS</div>
<div className="side-container">
{renderEmojiSide(currentQuestion.rightSide, 'right')}
<div className="count-display">{currentQuestion.rightSide.length}</div>
</div>
</div>
{gameStep === 'step1' && (
<div className="answer-buttons">
<button
className="answer-btn equal-btn"
onClick={() => handleStep1Answer('equal')}
>
{buttonLabels.equal}
</button>
<button
className="answer-btn not-equal-btn"
onClick={() => handleStep1Answer('not-equal')}
>
{buttonLabels.notEqual}
</button>
</div>
)}
</>
) : (
// Step 2: Which side has more/fewer? (Only if sides are NOT equal)
currentQuestion.leftSide.length !== currentQuestion.rightSide.length ? (
<>
<h1 className="question-title">
Which side has {currentQuestion.secondStepQuestion}?
</h1>
<div className="comparison-container">
<div className="side-container clickable" onClick={() => handleStep2Answer('left')}>
{renderEmojiSide(currentQuestion.leftSide, 'left')}
<div className="count-display">{currentQuestion.leftSide.length}</div>
</div>
<div className="vs-divider">VS</div>
<div className="side-container clickable" onClick={() => handleStep2Answer('right')}>
{renderEmojiSide(currentQuestion.rightSide, 'right')}
<div className="count-display">{currentQuestion.rightSide.length}</div>
</div>
</div>
<div className="step2-instruction">
Click on the side with {currentQuestion.secondStepQuestion} items
</div>
</>
) : (
// If somehow we get to step 2 with equal sides, generate new question
<div className="loading">Generating new question...</div>
)
)}
{showFeedback && (
<div className="feedback-message">
{feedback}
</div>
)}
</div>
);
}
export default App;
|