Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>π Area & Perimeter Adventure π</title> | |
| <link rel="stylesheet" href="style.css"> | |
| </head> | |
| <body> | |
| <div class="game-container"> | |
| <h1>π Area & Perimeter Adventure π</h1> | |
| <div class="controls"> | |
| <button id="add-mode" class="mode-active">βοΈ Add Squares</button> | |
| <button id="erase-mode">π§½ Erase Squares</button> | |
| <button id="reset-grid">π Reset</button> | |
| </div> | |
| <div class="grid-wrapper"> | |
| <div class="grid" id="grid"></div> | |
| </div> | |
| <div class="stats"> | |
| <div class="stat-box"> | |
| <div>Area:</div> | |
| <div class="stat-value"><span id="area">0</span> π©</div> | |
| </div> | |
| <div class="stat-box"> | |
| <div>Perimeter:</div> | |
| <div class="stat-value"><span id="perimeter">0</span> π</div> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| const gridSize = 15; | |
| const grid = document.getElementById('grid'); | |
| const areaDisplay = document.getElementById('area'); | |
| const perimeterDisplay = document.getElementById('perimeter'); | |
| const addModeBtn = document.getElementById('add-mode'); | |
| const eraseModeBtn = document.getElementById('erase-mode'); | |
| const resetBtn = document.getElementById('reset-grid'); | |
| let mode = 'add'; | |
| let filledCells = new Set(); | |
| // Build the grid | |
| function initGrid() { | |
| grid.innerHTML = ''; | |
| for (let i = 0; i < gridSize * gridSize; i++) { | |
| const cell = document.createElement('div'); | |
| cell.className = 'cell'; | |
| cell.dataset.index = i; | |
| cell.addEventListener('click', handleCellClick); | |
| grid.appendChild(cell); | |
| } | |
| } | |
| // Handle clicks | |
| function handleCellClick(e) { | |
| const index = e.target.dataset.index; | |
| if (mode === 'add' && !filledCells.has(index)) { | |
| e.target.classList.add('filled'); | |
| filledCells.add(index); | |
| } else if (mode === 'erase' && filledCells.has(index)) { | |
| e.target.classList.remove('filled'); | |
| filledCells.delete(index); | |
| } | |
| updateStats(); | |
| } | |
| // Update area and perimeter | |
| function updateStats() { | |
| const area = filledCells.size; | |
| let perimeter = 0; | |
| filledCells.forEach(index => { | |
| const i = parseInt(index); | |
| if (!filledCells.has((i - gridSize).toString())) perimeter++; // top | |
| if (!filledCells.has((i + 1).toString()) || i % gridSize === gridSize - 1) perimeter++; // right | |
| if (!filledCells.has((i + gridSize).toString())) perimeter++; // bottom | |
| if (!filledCells.has((i - 1).toString()) || i % gridSize === 0) perimeter++; // left | |
| }); | |
| areaDisplay.textContent = area; | |
| perimeterDisplay.textContent = perimeter; | |
| } | |
| // Mode toggle | |
| addModeBtn.addEventListener('click', () => { | |
| mode = 'add'; | |
| addModeBtn.classList.add('mode-active'); | |
| eraseModeBtn.classList.remove('mode-active'); | |
| }); | |
| eraseModeBtn.addEventListener('click', () => { | |
| mode = 'erase'; | |
| eraseModeBtn.classList.add('mode-active'); | |
| addModeBtn.classList.remove('mode-active'); | |
| }); | |
| // Reset button | |
| resetBtn.addEventListener('click', () => { | |
| filledCells.clear(); | |
| document.querySelectorAll('.cell').forEach(cell => { | |
| cell.classList.remove('filled'); | |
| }); | |
| updateStats(); | |
| }); | |
| // Init | |
| initGrid(); | |
| </script> | |
| </body> | |
| </html> | |