Pp commited on
Commit
2211244
Β·
verified Β·
1 Parent(s): 2b7dfea

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +15 -27
script.js CHANGED
@@ -10,21 +10,18 @@ document.addEventListener('DOMContentLoaded', () => {
10
  const scoreDisplay = document.getElementById('score-display');
11
 
12
  // --- Game Settings ---
13
- const gridSize = 12; // << INCREASED GRID SIZE (e.g., 12x12)
14
- const cellSize = 40; // Keep cell size, or adjust (e.g., 35 for smaller cells on bigger grid)
15
 
16
- // --- Items to Find --- << ADDED MORE ITEMS
17
  const itemsToFind = [
18
  { name: "a sparkling diamond", emoji: "πŸ’Ž" }, { name: "a red apple", emoji: "🍎" },
19
  { name: "a yummy pizza slice", emoji: "πŸ•" }, { name: "a fast rocket", emoji: "πŸš€" },
20
  { name: "a cute puppy", emoji: "🐢" }, { name: "a bright star", emoji: "⭐" },
21
  { name: "a cool robot", emoji: "πŸ€–" }, { name: "a friendly ghost", emoji: "πŸ‘»" },
22
- { name: "a birthday cake", emoji: "πŸŽ‚" }, { name: "a magical unicorn", emoji: "πŸ¦„" },
23
- { name: "a buzzing bee", emoji: "🐝" }, { name: "a soccer ball", emoji: "⚽" },
24
- { name: "a juicy strawberry", emoji: "πŸ“" }, { name: "a treasure chest", emoji: "πŸͺ™" },// Use coin if chest unavailable
25
- { name: "a sleepy koala", emoji: "🐨" }
26
  ];
27
- const totalItems = itemsToFind.length; // **Dynamically get count**
28
 
29
  // --- Game State ---
30
  let targetX = -1, targetY = -1, currentItemIndex = 0, foundCurrent = false, score = 0;
@@ -36,13 +33,13 @@ document.addEventListener('DOMContentLoaded', () => {
36
  yAxisLabelsContainer.innerHTML = '';
37
  score = 0;
38
  currentItemIndex = 0;
39
- updateScoreDisplay(); // **Use dynamic totalItems here**
40
 
41
  document.documentElement.style.setProperty('--cell-size', `${cellSize}px`);
42
 
43
  const gridTotalSize = gridSize * cellSize;
44
- const yAxisWidth = 20;
45
- const yAxisMargin = 5;
46
 
47
  // Configure Grid Container Styles Explicitly
48
  gridContainer.style.width = `${gridTotalSize}px`;
@@ -54,7 +51,6 @@ document.addEventListener('DOMContentLoaded', () => {
54
  // Configure Axis Containers
55
  yAxisLabelsContainer.style.height = `${gridTotalSize}px`;
56
  xAxisLabelsContainer.style.width = `${gridTotalSize}px`;
57
- // Make the outer X container wide enough for Y axis space + grid
58
  xAxisContainer.style.width = `${yAxisWidth + yAxisMargin + gridTotalSize}px`;
59
  xAxisLabelsContainer.style.marginLeft = `${yAxisWidth + yAxisMargin}px`;
60
 
@@ -92,29 +88,23 @@ document.addEventListener('DOMContentLoaded', () => {
92
 
93
  // --- Update Score Display ---
94
  function updateScoreDisplay() {
95
- // **Use totalItems variable**
96
  scoreDisplay.textContent = `Score: ${score} / ${totalItems}`;
97
  }
98
 
99
  // --- Set New Target ---
100
  function setNewTarget() {
101
  foundCurrent = false;
102
-
103
- // Game Over Check
104
  if (currentItemIndex >= totalItems) {
105
  let finalMessage = `All done! Your final score: ${score} / ${totalItems}! πŸŽ‰`;
106
  if (score === totalItems) finalMessage += " Perfect score! πŸ₯³";
107
- else if (score >= totalItems * 0.7) finalMessage += " Great job! πŸ‘"; // Adjust threshold
108
  feedbackDisplay.textContent = finalMessage;
109
  feedbackDisplay.className = 'correct-feedback';
110
  targetCoordsDisplay.textContent = "(βœ”,βœ”)";
111
  itemNameDisplay.textContent = 'all the items';
112
  return;
113
  }
114
-
115
  const currentItem = itemsToFind[currentItemIndex];
116
-
117
- // Find Empty Cell (same logic as before)
118
  let newTargetFound = false, attempts = 0;
119
  const maxAttempts = gridSize * gridSize * 2;
120
  while (!newTargetFound && attempts < maxAttempts) {
@@ -123,20 +113,18 @@ document.addEventListener('DOMContentLoaded', () => {
123
  const potentialCell = gridContainer.querySelector(`.grid-cell[data-x="${targetX}"][data-y="${targetY}"]`);
124
  if (potentialCell && potentialCell.textContent.trim() === '') { newTargetFound = true; }
125
  attempts++;
126
- if(attempts >= maxAttempts){ // Add the check after incrementing attempts
127
  let allFilled = true;
128
  gridContainer.querySelectorAll('.grid-cell').forEach(cell => { if(cell.textContent === '') allFilled = false; });
129
- if(allFilled) { console.warn("Grid appears full!"); break; } // Break if grid full
130
- else { attempts = 0; } // Reset attempts if grid not full but just unlucky
131
  }
132
  }
133
  if (!newTargetFound) {
134
- console.error("Could not find an empty cell after many attempts!");
135
- feedbackDisplay.textContent = "Uh oh, map is too full! Refresh maybe?";
136
  return;
137
  }
138
-
139
- // Update display
140
  itemNameDisplay.textContent = currentItem.name;
141
  targetCoordsDisplay.textContent = `(${targetX}, ${targetY})`;
142
  feedbackDisplay.textContent = `Where is ${currentItem.name}? (${currentItemIndex + 1}/${totalItems})`;
@@ -147,7 +135,7 @@ document.addEventListener('DOMContentLoaded', () => {
147
  console.log(`Round ${currentItemIndex + 1}: Find ${currentItem.name} at (${targetX}, ${targetY})`);
148
  }
149
 
150
- // --- Handle Cell Click (same as before) ---
151
  function handleCellClick(event) {
152
  if (foundCurrent || currentItemIndex >= totalItems) return;
153
  const clickedCell = event.target;
 
10
  const scoreDisplay = document.getElementById('score-display');
11
 
12
  // --- Game Settings ---
13
+ const gridSize = 8; // << REVERTED to 8x8 grid
14
+ const cellSize = 40; // Keep cell size from previous working version
15
 
16
+ // --- Items to Find --- << REVERTED to 10 items for 8x8 grid
17
  const itemsToFind = [
18
  { name: "a sparkling diamond", emoji: "πŸ’Ž" }, { name: "a red apple", emoji: "🍎" },
19
  { name: "a yummy pizza slice", emoji: "πŸ•" }, { name: "a fast rocket", emoji: "πŸš€" },
20
  { name: "a cute puppy", emoji: "🐢" }, { name: "a bright star", emoji: "⭐" },
21
  { name: "a cool robot", emoji: "πŸ€–" }, { name: "a friendly ghost", emoji: "πŸ‘»" },
22
+ { name: "a birthday cake", emoji: "πŸŽ‚" }, { name: "a magical unicorn", emoji: "πŸ¦„" }
 
 
 
23
  ];
24
+ const totalItems = itemsToFind.length; // Dynamic count
25
 
26
  // --- Game State ---
27
  let targetX = -1, targetY = -1, currentItemIndex = 0, foundCurrent = false, score = 0;
 
33
  yAxisLabelsContainer.innerHTML = '';
34
  score = 0;
35
  currentItemIndex = 0;
36
+ updateScoreDisplay(); // Use dynamic totalItems
37
 
38
  document.documentElement.style.setProperty('--cell-size', `${cellSize}px`);
39
 
40
  const gridTotalSize = gridSize * cellSize;
41
+ const yAxisWidth = 20; // Match CSS .y-axis-label width
42
+ const yAxisMargin = 5; // Match CSS #y-axis-labels margin-right
43
 
44
  // Configure Grid Container Styles Explicitly
45
  gridContainer.style.width = `${gridTotalSize}px`;
 
51
  // Configure Axis Containers
52
  yAxisLabelsContainer.style.height = `${gridTotalSize}px`;
53
  xAxisLabelsContainer.style.width = `${gridTotalSize}px`;
 
54
  xAxisContainer.style.width = `${yAxisWidth + yAxisMargin + gridTotalSize}px`;
55
  xAxisLabelsContainer.style.marginLeft = `${yAxisWidth + yAxisMargin}px`;
56
 
 
88
 
89
  // --- Update Score Display ---
90
  function updateScoreDisplay() {
 
91
  scoreDisplay.textContent = `Score: ${score} / ${totalItems}`;
92
  }
93
 
94
  // --- Set New Target ---
95
  function setNewTarget() {
96
  foundCurrent = false;
 
 
97
  if (currentItemIndex >= totalItems) {
98
  let finalMessage = `All done! Your final score: ${score} / ${totalItems}! πŸŽ‰`;
99
  if (score === totalItems) finalMessage += " Perfect score! πŸ₯³";
100
+ else if (score >= totalItems * 0.7) finalMessage += " Great job! πŸ‘";
101
  feedbackDisplay.textContent = finalMessage;
102
  feedbackDisplay.className = 'correct-feedback';
103
  targetCoordsDisplay.textContent = "(βœ”,βœ”)";
104
  itemNameDisplay.textContent = 'all the items';
105
  return;
106
  }
 
107
  const currentItem = itemsToFind[currentItemIndex];
 
 
108
  let newTargetFound = false, attempts = 0;
109
  const maxAttempts = gridSize * gridSize * 2;
110
  while (!newTargetFound && attempts < maxAttempts) {
 
113
  const potentialCell = gridContainer.querySelector(`.grid-cell[data-x="${targetX}"][data-y="${targetY}"]`);
114
  if (potentialCell && potentialCell.textContent.trim() === '') { newTargetFound = true; }
115
  attempts++;
116
+ if(attempts >= maxAttempts){
117
  let allFilled = true;
118
  gridContainer.querySelectorAll('.grid-cell').forEach(cell => { if(cell.textContent === '') allFilled = false; });
119
+ if(allFilled) { console.warn("Grid appears full!"); break; }
120
+ else { attempts = 0; }
121
  }
122
  }
123
  if (!newTargetFound) {
124
+ console.error("Could not find an empty cell!");
125
+ feedbackDisplay.textContent = "Uh oh, map is full! Refresh maybe?";
126
  return;
127
  }
 
 
128
  itemNameDisplay.textContent = currentItem.name;
129
  targetCoordsDisplay.textContent = `(${targetX}, ${targetY})`;
130
  feedbackDisplay.textContent = `Where is ${currentItem.name}? (${currentItemIndex + 1}/${totalItems})`;
 
135
  console.log(`Round ${currentItemIndex + 1}: Find ${currentItem.name} at (${targetX}, ${targetY})`);
136
  }
137
 
138
+ // --- Handle Cell Click ---
139
  function handleCellClick(event) {
140
  if (foundCurrent || currentItemIndex >= totalItems) return;
141
  const clickedCell = event.target;