Pp commited on
Commit
120291b
Β·
verified Β·
1 Parent(s): b419bd3

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +88 -92
index.html CHANGED
@@ -1,115 +1,111 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>πŸ“ Area & Perimeter Adventure πŸ“</title>
7
- <link rel="stylesheet" href="style.css">
8
  </head>
9
  <body>
10
- <div class="game-container">
11
- <h1>πŸ“ Area & Perimeter Adventure πŸ“</h1>
12
 
13
- <div class="controls">
14
- <button id="add-mode" class="mode-active">✏️ Add Squares</button>
15
- <button id="erase-mode">🧽 Erase Squares</button>
16
- <button id="reset-grid">πŸ”„ Reset</button>
17
- </div>
18
 
19
- <div class="grid-wrapper">
20
- <div class="grid" id="grid"></div>
21
- </div>
 
22
 
23
- <div class="stats">
24
- <div class="stat-box">
25
- <div>Area:</div>
26
- <div class="stat-value"><span id="area">0</span> 🟩</div>
27
- </div>
28
- <div class="stat-box">
29
- <div>Perimeter:</div>
30
- <div class="stat-value"><span id="perimeter">0</span> πŸ“</div>
31
- </div>
32
  </div>
 
33
  </div>
 
34
 
35
- <script>
36
- const gridSize = 15;
37
- const grid = document.getElementById('grid');
38
- const areaDisplay = document.getElementById('area');
39
- const perimeterDisplay = document.getElementById('perimeter');
40
- const addModeBtn = document.getElementById('add-mode');
41
- const eraseModeBtn = document.getElementById('erase-mode');
42
- const resetBtn = document.getElementById('reset-grid');
43
 
44
- let mode = 'add';
45
- let filledCells = new Set();
46
 
47
- // Build the grid
48
- function initGrid() {
49
- grid.innerHTML = '';
50
- for (let i = 0; i < gridSize * gridSize; i++) {
51
- const cell = document.createElement('div');
52
- cell.className = 'cell';
53
- cell.dataset.index = i;
54
- cell.addEventListener('click', handleCellClick);
55
- grid.appendChild(cell);
56
- }
57
- }
58
 
59
- // Handle clicks
60
- function handleCellClick(e) {
61
- const index = e.target.dataset.index;
62
- if (mode === 'add' && !filledCells.has(index)) {
63
- e.target.classList.add('filled');
64
- filledCells.add(index);
65
- } else if (mode === 'erase' && filledCells.has(index)) {
66
- e.target.classList.remove('filled');
67
- filledCells.delete(index);
68
- }
69
- updateStats();
70
- }
71
 
72
- // Update area and perimeter
73
- function updateStats() {
74
- const area = filledCells.size;
75
- let perimeter = 0;
76
 
77
- filledCells.forEach(index => {
78
- const i = parseInt(index);
79
- if (!filledCells.has((i - gridSize).toString())) perimeter++; // top
80
- if (!filledCells.has((i + 1).toString()) || i % gridSize === gridSize - 1) perimeter++; // right
81
- if (!filledCells.has((i + gridSize).toString())) perimeter++; // bottom
82
- if (!filledCells.has((i - 1).toString()) || i % gridSize === 0) perimeter++; // left
83
- });
84
 
85
- areaDisplay.textContent = area;
86
- perimeterDisplay.textContent = perimeter;
87
- }
88
 
89
- // Mode toggle
90
- addModeBtn.addEventListener('click', () => {
91
- mode = 'add';
92
- addModeBtn.classList.add('mode-active');
93
- eraseModeBtn.classList.remove('mode-active');
94
- });
95
 
96
- eraseModeBtn.addEventListener('click', () => {
97
- mode = 'erase';
98
- eraseModeBtn.classList.add('mode-active');
99
- addModeBtn.classList.remove('mode-active');
100
- });
101
 
102
- // Reset button
103
- resetBtn.addEventListener('click', () => {
104
- filledCells.clear();
105
- document.querySelectorAll('.cell').forEach(cell => {
106
- cell.classList.remove('filled');
107
- });
108
- updateStats();
109
- });
110
 
111
- // Init
112
- initGrid();
113
- </script>
114
  </body>
115
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
+ <title>πŸ“ Area & Perimeter Adventure πŸ“</title>
7
+ <link rel="stylesheet" href="style.css" />
8
  </head>
9
  <body>
10
+ <div class="game-container">
11
+ <h1>πŸ“ Area & Perimeter Adventure πŸ“</h1>
12
 
13
+ <div class="controls">
14
+ <button id="add-mode" class="mode-active">✏️ Add Squares</button>
15
+ <button id="erase-mode">🧽 Erase Squares</button>
16
+ <button id="reset-grid">πŸ”„ Reset</button>
17
+ </div>
18
 
19
+ <div class="grid-and-stats">
20
+ <div class="grid-wrapper">
21
+ <div class="grid" id="grid"></div>
22
+ </div>
23
 
24
+ <div class="stats">
25
+ <div class="stat-box">
26
+ <div>Area:</div>
27
+ <div class="stat-value"><span id="area">0</span> 🟩</div>
28
+ </div>
29
+ <div class="stat-box">
30
+ <div>Perimeter:</div>
31
+ <div class="stat-value"><span id="perimeter">0</span> πŸ“</div>
 
32
  </div>
33
+ </div>
34
  </div>
35
+ </div>
36
 
37
+ <script>
38
+ const gridSize = 15;
39
+ const grid = document.getElementById('grid');
40
+ const areaDisplay = document.getElementById('area');
41
+ const perimeterDisplay = document.getElementById('perimeter');
42
+ const addModeBtn = document.getElementById('add-mode');
43
+ const eraseModeBtn = document.getElementById('erase-mode');
44
+ const resetBtn = document.getElementById('reset-grid');
45
 
46
+ let mode = 'add';
47
+ let filledCells = new Set();
48
 
49
+ function initGrid() {
50
+ grid.innerHTML = '';
51
+ for (let i = 0; i < gridSize * gridSize; i++) {
52
+ const cell = document.createElement('div');
53
+ cell.className = 'cell';
54
+ cell.dataset.index = i;
55
+ cell.addEventListener('click', handleCellClick);
56
+ grid.appendChild(cell);
57
+ }
58
+ }
 
59
 
60
+ function handleCellClick(e) {
61
+ const index = e.target.dataset.index;
62
+ if (mode === 'add' && !filledCells.has(index)) {
63
+ e.target.classList.add('filled');
64
+ filledCells.add(index);
65
+ } else if (mode === 'erase' && filledCells.has(index)) {
66
+ e.target.classList.remove('filled');
67
+ filledCells.delete(index);
68
+ }
69
+ updateStats();
70
+ }
 
71
 
72
+ function updateStats() {
73
+ const area = filledCells.size;
74
+ let perimeter = 0;
 
75
 
76
+ filledCells.forEach(index => {
77
+ const i = parseInt(index);
78
+ if (!filledCells.has((i - gridSize).toString())) perimeter++; // top
79
+ if (!filledCells.has((i + 1).toString()) || i % gridSize === gridSize - 1) perimeter++; // right
80
+ if (!filledCells.has((i + gridSize).toString())) perimeter++; // bottom
81
+ if (!filledCells.has((i - 1).toString()) || i % gridSize === 0) perimeter++; // left
82
+ });
83
 
84
+ areaDisplay.textContent = area;
85
+ perimeterDisplay.textContent = perimeter;
86
+ }
87
 
88
+ addModeBtn.addEventListener('click', () => {
89
+ mode = 'add';
90
+ addModeBtn.classList.add('mode-active');
91
+ eraseModeBtn.classList.remove('mode-active');
92
+ });
 
93
 
94
+ eraseModeBtn.addEventListener('click', () => {
95
+ mode = 'erase';
96
+ eraseModeBtn.classList.add('mode-active');
97
+ addModeBtn.classList.remove('mode-active');
98
+ });
99
 
100
+ resetBtn.addEventListener('click', () => {
101
+ filledCells.clear();
102
+ document.querySelectorAll('.cell').forEach(cell => {
103
+ cell.classList.remove('filled');
104
+ });
105
+ updateStats();
106
+ });
 
107
 
108
+ initGrid();
109
+ </script>
 
110
  </body>
111
  </html>