File size: 39,776 Bytes
28ad2a0 | 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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RoboMath - Apprendre la programmation et les maths</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>
.code-block {
font-family: 'Courier New', monospace;
background-color: #2d3748;
color: #f7fafc;
border-radius: 0.5rem;
padding: 1rem;
min-height: 150px;
}
.robot {
transition: all 0.5s ease;
position: absolute;
bottom: 1rem;
left: 1rem;
}
.command-btn {
transition: all 0.2s ease;
}
.command-btn:hover {
transform: translateY(-2px);
}
.droppable {
min-height: 50px;
background-color: #4a5568;
border: 2px dashed #718096;
border-radius: 0.5rem;
}
.draggable {
cursor: move;
user-select: none;
}
.progress-track {
background-image: linear-gradient(45deg, rgba(255,255,255,0.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,0.15) 50%, rgba(255,255,255,0.15) 75%, transparent 75%, transparent);
background-size: 1rem 1rem;
}
.modal {
transition: opacity 0.3s ease;
}
</style>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
<!-- Header -->
<header class="flex flex-col md:flex-row justify-between items-center mb-8">
<div class="flex items-center mb-4 md:mb-0">
<i class="fas fa-robot text-4xl text-blue-400 mr-3"></i>
<h1 class="text-3xl font-bold bg-gradient-to-r from-blue-400 to-purple-500 bg-clip-text text-transparent">RoboMath</h1>
</div>
<div class="flex items-center space-x-4">
<button id="help-btn" class="px-3 py-1 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-medium transition">
<i class="fas fa-question-circle mr-1"></i> Aide
</button>
<div class="flex items-center">
<span class="mr-2 text-yellow-300"><i class="fas fa-star"></i></span>
<span id="score" class="font-bold">0</span>
</div>
<div class="relative">
<div class="h-3 w-32 bg-gray-700 rounded-full overflow-hidden">
<div id="progress-bar" class="h-full bg-gradient-to-r from-green-400 to-blue-500 rounded-full" style="width: 0%"></div>
</div>
<span id="level" class="absolute -top-6 left-0 text-xs font-bold">Niveau 1</span>
</div>
<button id="reset-btn" class="px-3 py-1 bg-red-600 hover:bg-red-700 rounded-md text-sm font-medium transition">
<i class="fas fa-redo mr-1"></i> Réinitialiser
</button>
</div>
</header>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
<!-- Left Panel - Game Area -->
<div class="lg:col-span-2 space-y-6">
<!-- Problem Statement -->
<div class="bg-gray-800 rounded-xl p-6 shadow-lg">
<h2 class="text-xl font-bold mb-4 text-blue-300 flex items-center">
<i class="fas fa-question-circle mr-2"></i> Problème Mathématique
</h2>
<div id="problem-statement" class="text-lg">
Programmez le robot pour qu'il calcule la somme des nombres de 1 à 5 et affiche le résultat.
</div>
<div id="math-expression" class="mt-4 p-3 bg-gray-700 rounded-md text-center text-xl font-mono">
1 + 2 + 3 + 4 + 5 = ?
</div>
</div>
<!-- Robot Visualization -->
<div class="bg-gray-800 rounded-xl p-6 shadow-lg">
<h2 class="text-xl font-bold mb-4 text-purple-300 flex items-center">
<i class="fas fa-robot mr-2"></i> Zone du Robot
</h2>
<div class="flex flex-col items-center">
<div id="robot-container" class="relative w-full h-48 bg-gray-700 rounded-lg mb-4 overflow-hidden">
<div id="robot" class="robot w-16 h-24 bg-blue-500 rounded-t-lg flex flex-col items-center">
<div class="w-12 h-8 bg-blue-400 rounded-t-full mt-1 flex justify-center items-center">
<div class="w-2 h-2 bg-yellow-300 rounded-full mx-1"></div>
<div class="w-2 h-2 bg-yellow-300 rounded-full mx-1"></div>
</div>
<div class="w-14 h-12 bg-blue-400 rounded-b-lg mt-1 flex justify-center items-center text-white font-bold">
R-1
</div>
</div>
<div id="output-display" class="absolute bottom-4 right-4 w-32 h-10 bg-gray-600 rounded flex items-center justify-center font-mono text-lg">
?
</div>
</div>
<div class="w-full bg-gray-700 h-2 rounded-full overflow-hidden">
<div id="progress-track" class="progress-track h-full bg-gradient-to-r from-blue-500 to-purple-500" style="width: 0%"></div>
</div>
</div>
</div>
</div>
<!-- Right Panel - Programming Area -->
<div class="space-y-6">
<!-- Command Palette -->
<div class="bg-gray-800 rounded-xl p-6 shadow-lg">
<h2 class="text-xl font-bold mb-4 text-green-300 flex items-center">
<i class="fas fa-code mr-2"></i> Commandes Disponibles
</h2>
<div class="grid grid-cols-2 gap-2">
<button data-command="moveForward()" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-arrow-right mr-2"></i> Avancer
</button>
<button data-command="turnLeft()" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-undo mr-2"></i> Tourner Gauche
</button>
<button data-command="turnRight()" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-redo mr-2"></i> Tourner Droite
</button>
<button data-command="setVariable('x', 0)" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-memory mr-2"></i> Définir Variable
</button>
<button data-command="increment('x')" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-plus-circle mr-2"></i> Incrémenter
</button>
<button data-command="addTo('x', 1)" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-plus mr-2"></i> Ajouter à
</button>
<button data-command="print('x')" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-print mr-2"></i> Afficher
</button>
<button data-command="loop(5)" class="command-btn draggable px-3 py-2 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-mono flex items-center">
<i class="fas fa-repeat mr-2"></i> Boucle
</button>
</div>
</div>
<!-- Code Editor -->
<div class="bg-gray-800 rounded-xl p-6 shadow-lg">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-bold text-yellow-300 flex items-center">
<i class="fas fa-laptop-code mr-2"></i> Votre Programme
</h2>
<button id="clear-btn" class="px-3 py-1 bg-gray-700 hover:bg-gray-600 rounded-md text-sm font-medium transition">
<i class="fas fa-trash-alt mr-1"></i> Effacer
</button>
</div>
<div id="code-block" class="code-block droppable">
<div id="code-placeholder" class="text-gray-400 italic">Glissez les commandes ici ou cliquez pour les ajouter</div>
</div>
<div class="mt-4 flex justify-between">
<button id="run-btn" class="px-4 py-2 bg-green-600 hover:bg-green-700 rounded-md font-medium flex items-center transition">
<i class="fas fa-play mr-2"></i> Exécuter
</button>
<button id="debug-btn" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-md font-medium flex items-center transition">
<i class="fas fa-bug mr-2"></i> Déboguer
</button>
</div>
</div>
<!-- Variables Panel -->
<div class="bg-gray-800 rounded-xl p-6 shadow-lg">
<h2 class="text-xl font-bold mb-4 text-pink-300 flex items-center">
<i class="fas fa-memory mr-2"></i> Variables
</h2>
<div id="variables-panel" class="grid grid-cols-2 gap-2">
<div class="bg-gray-700 p-2 rounded">
<div class="text-xs text-gray-400">x</div>
<div id="var-x" class="font-mono">0</div>
</div>
<div class="bg-gray-700 p-2 rounded">
<div class="text-xs text-gray-400">y</div>
<div id="var-y" class="font-mono">0</div>
</div>
<div class="bg-gray-700 p-2 rounded">
<div class="text-xs text-gray-400">z</div>
<div id="var-z" class="font-mono">0</div>
</div>
<div class="bg-gray-700 p-2 rounded">
<div class="text-xs text-gray-400">result</div>
<div id="var-result" class="font-mono">0</div>
</div>
</div>
</div>
</div>
</div>
<!-- Console Output -->
<div class="mt-6 bg-gray-800 rounded-xl p-6 shadow-lg">
<h2 class="text-xl font-bold mb-4 text-red-300 flex items-center">
<i class="fas fa-terminal mr-2"></i> Console
</h2>
<div id="console-output" class="code-block h-24 overflow-y-auto">
<div class="text-gray-400">Bienvenue dans RoboMath! Commencez à programmer votre robot.</div>
</div>
</div>
</div>
<!-- Success Modal -->
<div id="success-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
<div class="bg-gray-800 rounded-xl p-8 max-w-md w-full mx-4 text-center">
<div class="text-6xl text-yellow-400 mb-4">
<i class="fas fa-trophy"></i>
</div>
<h2 class="text-2xl font-bold text-green-400 mb-2">Félicitations!</h2>
<p id="success-message" class="mb-6">Vous avez résolu le problème avec succès!</p>
<div class="flex justify-center space-x-4">
<button id="next-level-btn" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 rounded-md font-medium">
Niveau Suivant
</button>
<button id="close-modal-btn" class="px-6 py-2 bg-gray-600 hover:bg-gray-700 rounded-md font-medium">
Fermer
</button>
</div>
</div>
</div>
<!-- Help Modal -->
<div id="help-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
<div class="bg-gray-800 rounded-xl p-8 max-w-2xl w-full mx-4 max-h-[80vh] overflow-y-auto">
<div class="flex justify-between items-center mb-6">
<h2 class="text-2xl font-bold text-blue-400 flex items-center">
<i class="fas fa-question-circle mr-3"></i> Aide RoboMath
</h2>
<button id="close-help-btn" class="text-gray-400 hover:text-white">
<i class="fas fa-times text-2xl"></i>
</button>
</div>
<div class="space-y-6">
<div>
<h3 class="text-xl font-bold text-purple-300 mb-3">Comment jouer</h3>
<p class="mb-4">RoboMath est un jeu éducatif qui combine programmation et mathématiques. Votre objectif est de programmer un robot virtuel pour résoudre des problèmes mathématiques.</p>
<div class="bg-gray-700 p-4 rounded-lg mb-4">
<h4 class="font-bold text-yellow-300 mb-2">Étapes pour réussir :</h4>
<ol class="list-decimal pl-5 space-y-2">
<li>Lisez attentivement le problème mathématique affiché</li>
<li>Construisez un programme en glissant-déposant les commandes disponibles</li>
<li>Exécutez votre programme avec le bouton "Exécuter"</li>
<li>Observez le résultat affiché par le robot</li>
<li>Si le résultat est correct, passez au niveau suivant!</li>
</ol>
</div>
</div>
<div>
<h3 class="text-xl font-bold text-green-300 mb-3">Commandes disponibles</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-arrow-right mr-2"></i> Avancer</h4>
<p class="text-sm mt-1">Fait avancer le robot dans la direction actuelle</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-undo mr-2"></i> Tourner Gauche</h4>
<p class="text-sm mt-1">Tourne le robot de 90° vers la gauche</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-redo mr-2"></i> Tourner Droite</h4>
<p class="text-sm mt-1">Tourne le robot de 90° vers la droite</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-memory mr-2"></i> Définir Variable</h4>
<p class="text-sm mt-1">Initialise une variable avec une valeur</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-plus-circle mr-2"></i> Incrémenter</h4>
<p class="text-sm mt-1">Augmente la valeur d'une variable de 1</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-plus mr-2"></i> Ajouter à</h4>
<p class="text-sm mt-1">Ajoute une valeur spécifique à une variable</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-print mr-2"></i> Afficher</h4>
<p class="text-sm mt-1">Affiche la valeur d'une variable</p>
</div>
<div class="bg-gray-700 p-3 rounded-lg">
<h4 class="font-bold flex items-center"><i class="fas fa-repeat mr-2"></i> Boucle</h4>
<p class="text-sm mt-1">Répète un bloc d'instructions plusieurs fois</p>
</div>
</div>
</div>
<div>
<h3 class="text-xl font-bold text-red-300 mb-3">Conseils pour réussir</h3>
<div class="bg-gray-700 p-4 rounded-lg">
<ul class="list-disc pl-5 space-y-2">
<li>Commencez par analyser le problème mathématique</li>
<li>Décomposez le problème en étapes simples</li>
<li>Utilisez des variables pour stocker des résultats intermédiaires</li>
<li>Les boucles sont utiles pour répéter des opérations similaires</li>
<li>Testez souvent votre programme avec des valeurs simples</li>
<li>Consultez la console pour voir l'exécution pas à pas</li>
</ul>
</div>
</div>
<div class="text-center pt-4">
<button id="close-help-btn-2" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 rounded-md font-medium">
J'ai compris!
</button>
</div>
</div>
</div>
</div>
<script>
// Game state
const gameState = {
level: 1,
score: 0,
progress: 0,
variables: {
x: 0,
y: 0,
z: 0,
result: 0
},
program: [],
problems: [
{
statement: "Programmez le robot pour qu'il calcule la somme des nombres de 1 à 5 et affiche le résultat.",
expression: "1 + 2 + 3 + 4 + 5 = ?",
solution: 15,
hint: "Utilisez une boucle et une variable pour accumuler la somme."
},
{
statement: "Calculez le produit des nombres de 1 à 4 (factorielle de 4) et affichez le résultat.",
expression: "1 × 2 × 3 × 4 = ?",
solution: 24,
hint: "Initialisez une variable à 1, puis multipliez-la par chaque nombre dans une boucle."
},
{
statement: "Trouvez la somme des nombres pairs entre 2 et 10 et affichez le résultat.",
expression: "2 + 4 + 6 + 8 + 10 = ?",
solution: 30,
hint: "Utilisez une condition dans votre boucle pour n'ajouter que les nombres pairs."
}
]
};
// DOM elements
const elements = {
level: document.getElementById('level'),
score: document.getElementById('score'),
progressBar: document.getElementById('progress-bar'),
problemStatement: document.getElementById('problem-statement'),
mathExpression: document.getElementById('math-expression'),
robot: document.getElementById('robot'),
robotContainer: document.getElementById('robot-container'),
outputDisplay: document.getElementById('output-display'),
progressTrack: document.getElementById('progress-track'),
codeBlock: document.getElementById('code-block'),
codePlaceholder: document.getElementById('code-placeholder'),
consoleOutput: document.getElementById('console-output'),
varX: document.getElementById('var-x'),
varY: document.getElementById('var-y'),
varZ: document.getElementById('var-z'),
varResult: document.getElementById('var-result'),
runBtn: document.getElementById('run-btn'),
debugBtn: document.getElementById('debug-btn'),
clearBtn: document.getElementById('clear-btn'),
resetBtn: document.getElementById('reset-btn'),
helpBtn: document.getElementById('help-btn'),
commandBtns: document.querySelectorAll('.command-btn'),
successModal: document.getElementById('success-modal'),
successMessage: document.getElementById('success-message'),
nextLevelBtn: document.getElementById('next-level-btn'),
closeModalBtn: document.getElementById('close-modal-btn'),
helpModal: document.getElementById('help-modal'),
closeHelpBtn: document.getElementById('close-help-btn'),
closeHelpBtn2: document.getElementById('close-help-btn-2')
};
// Initialize the game
function initGame() {
updateGameState();
setupEventListeners();
loadLevel(gameState.level);
}
// Update UI based on game state
function updateGameState() {
elements.level.textContent = `Niveau ${gameState.level}`;
elements.score.textContent = gameState.score;
elements.progressBar.style.width = `${gameState.progress}%`;
// Update variables display
elements.varX.textContent = gameState.variables.x;
elements.varY.textContent = gameState.variables.y;
elements.varZ.textContent = gameState.variables.z;
elements.varResult.textContent = gameState.variables.result;
}
// Load a specific level
function loadLevel(level) {
const problem = gameState.problems[level - 1];
if (!problem) {
// No more levels
elements.problemStatement.textContent = "Vous avez terminé tous les niveaux!";
elements.mathExpression.textContent = "Bravo!";
return;
}
elements.problemStatement.textContent = problem.statement;
elements.mathExpression.textContent = problem.expression;
// Reset variables and program
gameState.variables = {
x: 0,
y: 0,
z: 0,
result: 0
};
gameState.program = [];
updateCodeDisplay();
// Reset robot position
elements.robot.style.left = '1rem';
elements.robot.style.bottom = '1rem';
elements.robot.style.transform = 'rotate(0deg)';
elements.outputDisplay.textContent = '?';
elements.progressTrack.style.width = '0%';
// Clear console
elements.consoleOutput.innerHTML = '<div class="text-gray-400">Bienvenue dans RoboMath! Commencez à programmer votre robot.</div>';
updateGameState();
}
// Update the code display area
function updateCodeDisplay() {
if (gameState.program.length === 0) {
elements.codePlaceholder.classList.remove('hidden');
elements.codeBlock.innerHTML = '<div id="code-placeholder" class="text-gray-400 italic">Glissez les commandes ici ou cliquez pour les ajouter</div>';
return;
}
elements.codePlaceholder.classList.add('hidden');
elements.codeBlock.innerHTML = '';
gameState.program.forEach((cmd, index) => {
const cmdElement = document.createElement('div');
cmdElement.className = 'flex items-center mb-1 draggable';
cmdElement.dataset.index = index;
const cmdText = document.createElement('span');
cmdText.className = 'font-mono text-sm bg-gray-700 px-2 py-1 rounded mr-2';
cmdText.textContent = cmd;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'text-red-400 hover:text-red-300';
deleteBtn.innerHTML = '<i class="fas fa-times"></i>';
deleteBtn.onclick = () => removeCommand(index);
cmdElement.appendChild(cmdText);
cmdElement.appendChild(deleteBtn);
elements.codeBlock.appendChild(cmdElement);
});
}
// Add a command to the program
function addCommand(command) {
gameState.program.push(command);
updateCodeDisplay();
}
// Remove a command from the program
function removeCommand(index) {
gameState.program.splice(index, 1);
updateCodeDisplay();
}
// Execute the program
function executeProgram() {
// Reset variables
gameState.variables = {
x: 0,
y: 0,
z: 0,
result: 0
};
// Reset robot position
elements.robot.style.left = '1rem';
elements.robot.style.bottom = '1rem';
elements.robot.style.transform = 'rotate(0deg)';
elements.outputDisplay.textContent = '?';
elements.progressTrack.style.width = '0%';
// Clear console
elements.consoleOutput.innerHTML = '';
// Add execution started message
addConsoleMessage("Début de l'exécution du programme...", "info");
// Execute each command with a delay for visualization
let position = { x: 16, y: 0 };
let rotation = 0;
let loopStack = [];
let loopCounters = {};
let currentLoop = null;
const executeNextCommand = (index) => {
if (index >= gameState.program.length) {
// Program finished
addConsoleMessage("Programme terminé.", "success");
checkSolution();
return;
}
const command = gameState.program[index];
let delay = 500; // Default delay between commands
// Parse and execute command
if (command.startsWith("moveForward()")) {
// Calculate new position based on rotation
const moveAmount = 40;
if (rotation === 0) {
position.x += moveAmount;
} else if (rotation === 90) {
position.y -= moveAmount;
} else if (rotation === 180) {
position.x -= moveAmount;
} else if (rotation === 270) {
position.y += moveAmount;
}
// Limit movement to container
const containerWidth = elements.robotContainer.offsetWidth;
const containerHeight = elements.robotContainer.offsetHeight;
const robotWidth = 64;
const robotHeight = 96;
position.x = Math.max(16, Math.min(position.x, containerWidth - robotWidth - 16));
position.y = Math.max(-containerHeight + robotHeight + 16, Math.min(position.y, 0));
elements.robot.style.left = `${position.x}px`;
elements.robot.style.bottom = `${-position.y}px`;
addConsoleMessage(`Robot avance (direction: ${rotation}°)`, "command");
}
else if (command.startsWith("turnLeft()")) {
rotation = (rotation - 90 + 360) % 360;
elements.robot.style.transform = `rotate(${rotation}deg)`;
addConsoleMessage("Robot tourne à gauche", "command");
}
else if (command.startsWith("turnRight()")) {
rotation = (rotation + 90) % 360;
elements.robot.style.transform = `rotate(${rotation}deg)`;
addConsoleMessage("Robot tourne à droite", "command");
}
else if (command.startsWith("setVariable(")) {
const matches = command.match(/setVariable\('(\w+)', (\d+)\)/);
if (matches) {
const varName = matches[1];
const value = parseInt(matches[2]);
gameState.variables[varName] = value;
updateGameState();
addConsoleMessage(`Variable ${varName} définie à ${value}`, "command");
}
}
else if (command.startsWith("increment(")) {
const varName = command.match(/increment\('(\w+)'\)/)[1];
gameState.variables[varName]++;
updateGameState();
addConsoleMessage(`Variable ${varName} incrémentée à ${gameState.variables[varName]}`, "command");
}
else if (command.startsWith("addTo(")) {
const matches = command.match(/addTo\('(\w+)', (\d+)\)/);
if (matches) {
const varName = matches[1];
const value = parseInt(matches[2]);
gameState.variables[varName] += value;
updateGameState();
addConsoleMessage(`Ajouté ${value} à ${varName} (nouvelle valeur: ${gameState.variables[varName]})`, "command");
}
}
else if (command.startsWith("print(")) {
const varName = command.match(/print\('(\w+)'\)/)[1];
const value = gameState.variables[varName];
elements.outputDisplay.textContent = value;
addConsoleMessage(`Affichage: ${varName} = ${value}`, "output");
}
else if (command.startsWith("loop(")) {
const loopCount = parseInt(command.match(/loop\((\d+)\)/)[1]);
loopStack.push({
startIndex: index,
count: loopCount,
current: 0
});
addConsoleMessage(`Début de boucle (${loopCount} itérations)`, "command");
delay = 0; // No delay for loop start
}
else if (command === "endLoop") {
const currentLoop = loopStack[loopStack.length - 1];
currentLoop.current++;
if (currentLoop.current < currentLoop.count) {
// Next iteration
index = currentLoop.startIndex;
addConsoleMessage(`Itération ${currentLoop.current + 1} de ${currentLoop.count}`, "command");
delay = 0; // No delay for loop iteration
} else {
// Loop finished
loopStack.pop();
addConsoleMessage("Fin de boucle", "command");
}
}
// Update progress track
const progress = (index + 1) / gameState.program.length * 100;
elements.progressTrack.style.width = `${progress}%`;
// Execute next command after delay
setTimeout(() => executeNextCommand(index + 1), delay);
};
// Start execution
executeNextCommand(0);
}
// Check if the solution is correct
function checkSolution() {
const currentProblem = gameState.problems[gameState.level - 1];
if (!currentProblem) return;
const userResult = parseInt(elements.outputDisplay.textContent);
if (userResult === currentProblem.solution) {
// Correct solution
gameState.score += 100;
gameState.progress += 33;
// Show success modal
elements.successMessage.textContent = `Vous avez résolu le problème! La réponse correcte était ${currentProblem.solution}.`;
elements.successModal.classList.remove('hidden');
// Disable next level button if no more levels
if (gameState.level >= gameState.problems.length) {
elements.nextLevelBtn.disabled = true;
elements.nextLevelBtn.textContent = "Niveau Max Atteint";
elements.nextLevelBtn.classList.add('opacity-50', 'cursor-not-allowed');
}
updateGameState();
} else {
// Incorrect solution
addConsoleMessage(`Le résultat ${userResult} est incorrect. Essayez encore!`, "error");
addConsoleMessage(`Indice: ${currentProblem.hint}`, "info");
}
}
// Add a message to the console
function addConsoleMessage(message, type = "info") {
const messageElement = document.createElement('div');
messageElement.className = `mb-1 ${type === 'error' ? 'text-red-400' : type === 'success' ? 'text-green-400' : type === 'command' ? 'text-blue-300' : type === 'output' ? 'text-yellow-300' : 'text-gray-300'}`;
const timestamp = new Date().toLocaleTimeString();
messageElement.innerHTML = `<span class="text-gray-500 text-xs mr-2">[${timestamp}]</span> ${message}`;
elements.consoleOutput.appendChild(messageElement);
elements.consoleOutput.scrollTop = elements.consoleOutput.scrollHeight;
}
// Setup event listeners
function setupEventListeners() {
// Command buttons
elements.commandBtns.forEach(btn => {
btn.addEventListener('click', () => {
addCommand(btn.dataset.command);
});
// Drag and drop functionality
btn.setAttribute('draggable', 'true');
btn.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('text/plain', btn.dataset.command);
});
});
// Code block drop zone
elements.codeBlock.addEventListener('dragover', (e) => {
e.preventDefault();
elements.codeBlock.classList.add('bg-gray-700');
});
elements.codeBlock.addEventListener('dragleave', () => {
elements.codeBlock.classList.remove('bg-gray-700');
});
elements.codeBlock.addEventListener('drop', (e) => {
e.preventDefault();
elements.codeBlock.classList.remove('bg-gray-700');
const command = e.dataTransfer.getData('text/plain');
if (command) {
addCommand(command);
}
});
// Action buttons
elements.runBtn.addEventListener('click', () => {
if (gameState.program.length === 0) {
addConsoleMessage("Aucune commande à exécuter. Ajoutez des commandes d'abord.", "error");
return;
}
executeProgram();
});
elements.debugBtn.addEventListener('click', () => {
addConsoleMessage("Mode débogage activé (fonctionnalité à venir)", "info");
});
elements.clearBtn.addEventListener('click', () => {
gameState.program = [];
updateCodeDisplay();
addConsoleMessage("Programme effacé", "info");
});
elements.resetBtn.addEventListener('click', () => {
gameState.score = 0;
gameState.progress = 0;
loadLevel(gameState.level);
addConsoleMessage("Jeu réinitialisé", "info");
});
// Help button
elements.helpBtn.addEventListener('click', () => {
elements.helpModal.classList.remove('hidden');
});
// Modal buttons
elements.nextLevelBtn.addEventListener('click', () => {
gameState.level++;
loadLevel(gameState.level);
elements.successModal.classList.add('hidden');
});
elements.closeModalBtn.addEventListener('click', () => {
elements.successModal.classList.add('hidden');
});
// Help modal buttons
elements.closeHelpBtn.addEventListener('click', () => {
elements.helpModal.classList.add('hidden');
});
elements.closeHelpBtn2.addEventListener('click', () => {
elements.helpModal.classList.add('hidden');
});
}
// Initialize the game when DOM is loaded
document.addEventListener('DOMContentLoaded', initGame);
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=olinux/robomath" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |