Code Your Robot Game

Code Your Robot Game

Code Your Robot Game

Robot Grid

Your browser does not support the canvas element.

Goal: Reach the Green Square!

Command Input

`; // Draw current state onto the PDF canvas const pdfCanvas = cyr_pdfRenderClone.querySelector('#pdf-canvas'); if (!pdfCanvas) return; // Guard clause const pdfCtx = pdfCanvas.getContext('2d'); if (!pdfCtx) return; // Guard clause // Temporarily replace global context for drawing function const originalCtx = cyr_ctx; cyr_ctx = pdfCtx; cyr_drawGame(); // Draw using current game state cyr_ctx = originalCtx; // Restore global context // Add feedback message const pdfFeedback = cyr_pdfRenderClone.querySelector('#pdf-feedback'); if (pdfFeedback) { pdfFeedback.textContent = cyr_feedbackArea.textContent; // Copy classes carefully, removing problematic ones pdfFeedback.className = cyr_feedbackArea.className.replace('h-16','').replace('overflow-y-auto','').trim(); } } /** * Generates and downloads a PDF of the results */ async function cyr_downloadPDF() { if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') { console.error("CYR Tool Error: jsPDF or html2canvas library not loaded."); alert("Error: PDF libraries failed to load. Please check console."); return; } cyr_renderPdfClone(); // Create and populate the clone const { jsPDF } = window.jspdf; try { // Target the entire clone div const canvas = await html2canvas(cyr_pdfRenderClone, { scale: 1.5, // Increase resolution slightly useCORS: true, // Ensure rendering happens correctly onclone: (document) => { // Redraw canvas within the clone just before capture const pdfCanvas = document.getElementById('pdf-canvas'); if (!pdfCanvas) return; const pdfCtx = pdfCanvas.getContext('2d'); if (!pdfCtx) return; const originalCtx = cyr_ctx; // backup global cyr_ctx = pdfCtx; cyr_drawGame(); // draw current state cyr_ctx = originalCtx; // restore } }); const imgData = canvas.toDataURL('image/png'); const imgWidth = canvas.width; const imgHeight = canvas.height; const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const ratio = Math.min((pdfWidth - 80) / imgWidth, (pdfHeight - 80) / imgHeight); const w = imgWidth * ratio; const h = imgHeight * ratio; const x = (pdfWidth - w) / 2; // Center horizontally const y = 40; // Top margin pdf.addImage(imgData, 'PNG', x, y, w, h); pdf.save('Code_Your_Robot_Summary.pdf'); } catch (error) { console.error("CYR Tool Error: PDF generation failed.", error); alert("An error occurred while generating the PDF. Please try again."); } } // --- EVENT LISTENERS --- // Tab link clicks cyr_tabLinks.forEach((link, index) => { link.addEventListener('click', () => cyr_switchTab(index)); }); // Next/Prev button clicks if (cyr_prevButton) { cyr_prevButton.addEventListener('click', () => { if (cyr_currentTab > 0) cyr_switchTab(cyr_currentTab - 1); }); } if (cyr_nextButton) { cyr_nextButton.addEventListener('click', () => { if (cyr_currentTab < cyr_tabLinks.length - 1) cyr_switchTab(cyr_currentTab + 1); }); } // PDF download if (cyr_downloadPdfButton) { cyr_downloadPdfButton.addEventListener('click', cyr_downloadPDF); } // Game Controls if (cyr_runCodeButton) { cyr_runCodeButton.addEventListener('click', cyr_runCommands); } if (cyr_resetGameButton) { cyr_resetGameButton.addEventListener('click', cyr_resetGame); } // --- INITIALIZATION --- // Calculate canvas pixel size based on grid and cell size cyr_canvas.width = GRID_SIZE * CELL_SIZE; cyr_canvas.height = GRID_SIZE * CELL_SIZE; cyr_resetGame(); // Initialize game state and draw initial board // Set initial tab state cyr_tabPanes.forEach((pane, index) => { // FIX: Ensure correct initial visibility pane.classList.toggle('hidden', index !== 0); pane.classList.toggle('cyr-active', index === 0); }); // FIX: Ensure initial link styles are correct cyr_tabLinks.forEach((link, index) => { TAB_CLASSES.active.forEach(cls => link.classList.remove(cls)); TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls)); if (index === 0) { TAB_CLASSES.active.forEach(cls => link.classList.add(cls)); } else { TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls)); } }); });