Spaces:
Running
Running
File size: 16,700 Bytes
383f554 |
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite Algebra Game</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.pulse-animation {
animation: pulse 1.5s infinite;
}
.fade-in {
animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.shake {
animation: shake 0.5s;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
20%, 40%, 60%, 80% { transform: translateX(5px); }
}
</style>
</head>
<body class="bg-gradient-to-br from-indigo-900 to-purple-800 min-h-screen text-white font-sans">
<div class="container mx-auto px-4 py-8 max-w-3xl">
<header class="text-center mb-8">
<h1 class="text-4xl font-bold mb-2">∞ Infinite Algebra Game</h1>
<p class="text-xl opacity-80">Solve algebra problems as they get progressively harder!</p>
</header>
<div class="bg-white/10 backdrop-blur-md rounded-xl shadow-2xl overflow-hidden transition-all duration-300">
<!-- Game Stats -->
<div class="flex justify-between items-center p-6 border-b border-white/10">
<div class="text-center">
<div class="text-sm opacity-70">Level</div>
<div class="text-2xl font-bold" id="level">1</div>
</div>
<div class="text-center">
<div class="text-sm opacity-70">Score</div>
<div class="text-2xl font-bold" id="score">0</div>
</div>
<div class="text-center">
<div class="text-sm opacity-70">Streak</div>
<div class="text-2xl font-bold" id="streak">0</div>
</div>
</div>
<!-- Game Area -->
<div class="p-8 text-center">
<div class="mb-8">
<div class="text-lg mb-2 opacity-80">Solve for x:</div>
<div class="text-5xl font-bold mb-6 fade-in" id="problem">2x + 5 = 15</div>
<div class="max-w-xs mx-auto relative">
<input type="number" id="answer" class="w-full bg-white/20 border border-white/30 rounded-lg py-4 px-6 text-2xl text-center focus:outline-none focus:ring-2 focus:ring-purple-400 transition" placeholder="Your answer" autofocus>
<button id="submit-btn" class="mt-4 bg-purple-600 hover:bg-purple-500 text-white font-bold py-3 px-8 rounded-full transition-all duration-200 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-purple-400 focus:ring-opacity-50">
Submit <i class="fas fa-arrow-right ml-2"></i>
</button>
</div>
</div>
<div id="feedback" class="h-8 mb-4"></div>
<div class="bg-white/10 rounded-lg p-4 text-left hidden" id="hint-box">
<div class="flex items-start">
<div class="mr-3 text-purple-300 text-xl"><i class="fas fa-lightbulb"></i></div>
<div>
<div class="font-bold mb-1">Hint</div>
<div id="hint-text">Remember to isolate x by performing inverse operations on both sides of the equation.</div>
</div>
</div>
</div>
</div>
<!-- Game Controls -->
<div class="p-6 border-t border-white/10 flex justify-between">
<button id="hint-btn" class="text-purple-300 hover:text-purple-200 transition flex items-center">
<i class="fas fa-lightbulb mr-2"></i> Get Hint
</button>
<button id="new-game-btn" class="text-white/70 hover:text-white transition flex items-center">
<i class="fas fa-redo mr-2"></i> New Game
</button>
</div>
</div>
<!-- How to Play -->
<div class="mt-8 bg-white/10 backdrop-blur-md rounded-xl p-6">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fas fa-question-circle mr-2"></i> How to Play
</h2>
<ul class="space-y-2">
<li class="flex items-start">
<i class="fas fa-check-circle text-purple-300 mr-2 mt-1"></i>
<span>Solve the algebra equation for x</span>
</li>
<li class="flex items-start">
<i class="fas fa-check-circle text-purple-300 mr-2 mt-1"></i>
<span>Type your answer in the box and press Submit</span>
</li>
<li class="flex items-start">
<i class="fas fa-check-circle text-purple-300 mr-2 mt-1"></i>
<span>Problems get harder as you level up</span>
</li>
<li class="flex items-start">
<i class="fas fa-check-circle text-purple-300 mr-2 mt-1"></i>
<span>Correct answers give you points and increase your streak</span>
</li>
</ul>
</div>
</div>
<script>
// Game state
const gameState = {
level: 1,
score: 0,
streak: 0,
currentProblem: null,
correctAnswer: null,
hintShown: false
};
// DOM elements
const elements = {
level: document.getElementById('level'),
score: document.getElementById('score'),
streak: document.getElementById('streak'),
problem: document.getElementById('problem'),
answer: document.getElementById('answer'),
submitBtn: document.getElementById('submit-btn'),
feedback: document.getElementById('feedback'),
hintBox: document.getElementById('hint-box'),
hintText: document.getElementById('hint-text'),
hintBtn: document.getElementById('hint-btn'),
newGameBtn: document.getElementById('new-game-btn')
};
// Initialize game
function initGame() {
gameState.level = 1;
gameState.score = 0;
gameState.streak = 0;
gameState.hintShown = false;
updateUI();
generateProblem();
elements.answer.value = '';
elements.answer.focus();
elements.feedback.innerHTML = '';
elements.hintBox.classList.add('hidden');
}
// Update UI with current game state
function updateUI() {
elements.level.textContent = gameState.level;
elements.score.textContent = gameState.score;
elements.streak.textContent = gameState.streak;
}
// Generate a new algebra problem based on current level
function generateProblem() {
let a, b, c, equation, answer;
// Adjust problem difficulty based on level
if (gameState.level <= 3) {
// Simple equations: ax + b = c
a = getRandomInt(1, 5);
b = getRandomInt(1, 10);
c = a * getRandomInt(1, 10) + b;
equation = `${a}x + ${b} = ${c}`;
answer = (c - b) / a;
} else if (gameState.level <= 6) {
// Equations with subtraction: ax - b = c
a = getRandomInt(1, 8);
b = getRandomInt(1, 15);
c = a * getRandomInt(1, 10) - b;
equation = `${a}x - ${b} = ${c}`;
answer = (c + b) / a;
} else if (gameState.level <= 9) {
// Equations with multiplication coefficients: a(bx + c) = d
a = getRandomInt(2, 5);
b = getRandomInt(1, 5);
c = getRandomInt(1, 10);
const d = a * (b * getRandomInt(1, 10) + c);
equation = `${a}(${b}x + ${c}) = ${d}`;
answer = (d / a - c) / b;
} else if (gameState.level <= 12) {
// Equations with division: (ax + b)/c = d
a = getRandomInt(1, 5);
b = getRandomInt(1, 10);
c = getRandomInt(2, 5);
const d = Math.floor((a * getRandomInt(1, 10) + b) / c);
equation = `(${a}x + ${b})/${c} = ${d}`;
answer = (d * c - b) / a;
} else {
// More complex equations with multiple operations
const operation = getRandomInt(1, 3);
switch(operation) {
case 1:
// ax + b = cx + d
a = getRandomInt(1, 6);
b = getRandomInt(1, 15);
c = getRandomInt(1, 6);
const d = (a - c) * getRandomInt(1, 10) + b;
equation = `${a}x + ${b} = ${c}x + ${d}`;
answer = (d - b) / (a - c);
break;
case 2:
// a(x + b) = c(x - d)
a = getRandomInt(2, 5);
b = getRandomInt(1, 5);
c = getRandomInt(2, 5);
const e = getRandomInt(1, 5);
const f = (a * (e + b)) / c + e;
equation = `${a}(x + ${b}) = ${c}(x - ${e})`;
answer = f;
break;
case 3:
// (ax + b)/(cx + d) = e
a = getRandomInt(1, 4);
b = getRandomInt(1, 8);
c = getRandomInt(1, 4);
const g = getRandomInt(1, 3);
const h = getRandomInt(1, 5);
const i = (a * h + b) / (c * h + g);
equation = `(${a}x + ${b})/(${c}x + ${g}) = ${i}`;
answer = h;
break;
}
}
gameState.currentProblem = equation;
gameState.correctAnswer = answer;
gameState.hintShown = false;
// Add fade-in animation
elements.problem.classList.remove('fade-in');
void elements.problem.offsetWidth; // Trigger reflow
elements.problem.classList.add('fade-in');
elements.problem.textContent = equation;
elements.hintBox.classList.add('hidden');
// Generate hint based on problem type
generateHint(equation, answer);
}
// Generate a hint for the current problem
function generateHint(equation, answer) {
let hint = "";
if (equation.includes('+') && !equation.includes('(') && !equation.includes('/')) {
hint = `To solve ${equation}, subtract the constant term from both sides, then divide by the coefficient of x.`;
} else if (equation.includes('-') && !equation.includes('(') && !equation.includes('/')) {
hint = `To solve ${equation}, add the constant term to both sides, then divide by the coefficient of x.`;
} else if (equation.includes('(') && equation.includes(')') && !equation.includes('/')) {
hint = `First, distribute the multiplication across the parentheses in ${equation}, then combine like terms and solve as usual.`;
} else if (equation.includes('/')) {
hint = `For ${equation}, start by eliminating the denominator by multiplying both sides by it, then proceed to solve the resulting equation.`;
} else {
hint = `Combine like terms in ${equation} to isolate x on one side of the equation, then solve for x.`;
}
elements.hintText.textContent = hint;
}
// Check the user's answer
function checkAnswer() {
const userAnswer = parseFloat(elements.answer.value);
if (isNaN(userAnswer)) {
showFeedback('Please enter a valid number', 'error');
elements.answer.classList.add('shake');
setTimeout(() => elements.answer.classList.remove('shake'), 500);
return;
}
// Round to 2 decimal places for comparison
const roundedUserAnswer = Math.round(userAnswer * 100) / 100;
const roundedCorrectAnswer = Math.round(gameState.correctAnswer * 100) / 100;
if (roundedUserAnswer === roundedCorrectAnswer) {
// Correct answer
gameState.score += gameState.level * 10;
gameState.streak += 1;
// Bonus for streak
if (gameState.streak >= 3) {
gameState.score += gameState.streak * 5;
}
// Level up every 3 correct answers
if (gameState.streak % 3 === 0) {
gameState.level += 1;
}
updateUI();
showFeedback(`Correct! x = ${roundedCorrectAnswer}`, 'success');
// Add celebration effect
elements.problem.classList.add('pulse-animation');
setTimeout(() => {
elements.problem.classList.remove('pulse-animation');
generateProblem();
elements.answer.value = '';
elements.answer.focus();
}, 1000);
} else {
// Wrong answer
gameState.streak = 0;
updateUI();
showFeedback(`Incorrect. Try again!`, 'error');
elements.answer.classList.add('shake');
setTimeout(() => elements.answer.classList.remove('shake'), 500);
elements.answer.select();
}
}
// Show feedback message
function showFeedback(message, type) {
elements.feedback.innerHTML = '';
const feedbackEl = document.createElement('div');
feedbackEl.className = `px-4 py-2 rounded-lg font-medium fade-in ${type === 'success' ? 'bg-green-500/20 text-green-200' : 'bg-red-500/20 text-red-200'}`;
feedbackEl.textContent = message;
elements.feedback.appendChild(feedbackEl);
// Remove feedback after 3 seconds
setTimeout(() => {
feedbackEl.classList.remove('fade-in');
feedbackEl.classList.add('opacity-0', 'transition-opacity', 'duration-300');
setTimeout(() => {
if (elements.feedback.contains(feedbackEl)) {
elements.feedback.removeChild(feedbackEl);
}
}, 300);
}, 3000);
}
// Show hint
function showHint() {
if (!gameState.hintShown) {
elements.hintBox.classList.remove('hidden');
gameState.hintShown = true;
// Penalize for using hint
gameState.score = Math.max(0, gameState.score - 5);
updateUI();
}
}
// Helper function to get random integer
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Event listeners
elements.submitBtn.addEventListener('click', checkAnswer);
elements.answer.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
checkAnswer();
}
});
elements.hintBtn.addEventListener('click', showHint);
elements.newGameBtn.addEventListener('click', initGame);
// Start the game
initGame();
</script>
</body>
</html> |