Update script.js
Browse files
script.js
CHANGED
|
@@ -10,25 +10,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 10 |
const scoreDisplay = document.getElementById('score-display');
|
| 11 |
|
| 12 |
// --- Game Settings ---
|
| 13 |
-
const gridSize =
|
| 14 |
-
const cellSize = 40; //
|
| 15 |
|
| 16 |
-
// --- Items to Find ---
|
| 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;
|
| 25 |
|
| 26 |
// --- Game State ---
|
| 27 |
-
let targetX = -1;
|
| 28 |
-
let targetY = -1;
|
| 29 |
-
let currentItemIndex = 0;
|
| 30 |
-
let foundCurrent = false;
|
| 31 |
-
let score = 0;
|
| 32 |
|
| 33 |
// --- Initialize Grid and Axes ---
|
| 34 |
function initializeGameArea() {
|
|
@@ -37,30 +36,29 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 37 |
yAxisLabelsContainer.innerHTML = '';
|
| 38 |
score = 0;
|
| 39 |
currentItemIndex = 0;
|
| 40 |
-
updateScoreDisplay();
|
| 41 |
|
| 42 |
-
// CSS variable for reference if needed, though direct style is used now
|
| 43 |
document.documentElement.style.setProperty('--cell-size', `${cellSize}px`);
|
| 44 |
|
| 45 |
const gridTotalSize = gridSize * cellSize;
|
| 46 |
-
const yAxisWidth = 20;
|
| 47 |
-
const yAxisMargin = 5;
|
| 48 |
|
| 49 |
-
//
|
| 50 |
gridContainer.style.width = `${gridTotalSize}px`;
|
| 51 |
gridContainer.style.height = `${gridTotalSize}px`;
|
| 52 |
-
gridContainer.style.display = 'grid';
|
| 53 |
-
gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, ${cellSize}px)`;
|
| 54 |
-
gridContainer.style.gridTemplateRows = `repeat(${gridSize}, ${cellSize}px)`;
|
| 55 |
-
// ---
|
| 56 |
|
| 57 |
// Configure Axis Containers
|
| 58 |
-
yAxisLabelsContainer.style.height = `${gridTotalSize}px`;
|
| 59 |
-
xAxisLabelsContainer.style.width = `${gridTotalSize}px`;
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
// Create Y-axis labels
|
| 64 |
for (let y = 1; y <= gridSize; y++) {
|
| 65 |
const label = document.createElement('div');
|
| 66 |
label.classList.add('axis-label', 'y-axis-label');
|
|
@@ -69,7 +67,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 69 |
yAxisLabelsContainer.appendChild(label);
|
| 70 |
}
|
| 71 |
|
| 72 |
-
// Create X-axis labels
|
| 73 |
for (let x = 1; x <= gridSize; x++) {
|
| 74 |
const label = document.createElement('div');
|
| 75 |
label.classList.add('axis-label', 'x-axis-label');
|
|
@@ -79,140 +77,106 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 79 |
}
|
| 80 |
|
| 81 |
// Create grid cells
|
| 82 |
-
// Loop Y from gridSize down to 1 so that Y=1 is the bottom row visually
|
| 83 |
for (let y = gridSize; y >= 1; y--) {
|
| 84 |
-
// Loop X from 1 to gridSize as usual (left-to-right)
|
| 85 |
for (let x = 1; x <= gridSize; x++) {
|
| 86 |
const cell = document.createElement('div');
|
| 87 |
cell.classList.add('grid-cell');
|
| 88 |
-
cell.dataset.x = x;
|
| 89 |
-
cell.
|
| 90 |
-
cell.style.width = `${cellSize}px`; // Explicit size
|
| 91 |
-
cell.style.height = `${cellSize}px`; // Explicit size
|
| 92 |
cell.addEventListener('click', handleCellClick);
|
| 93 |
gridContainer.appendChild(cell);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
-
console.log(
|
| 97 |
}
|
| 98 |
|
| 99 |
// --- Update Score Display ---
|
| 100 |
function updateScoreDisplay() {
|
|
|
|
| 101 |
scoreDisplay.textContent = `Score: ${score} / ${totalItems}`;
|
| 102 |
}
|
| 103 |
|
| 104 |
-
// --- Set
|
| 105 |
function setNewTarget() {
|
| 106 |
foundCurrent = false;
|
| 107 |
|
| 108 |
-
//
|
| 109 |
if (currentItemIndex >= totalItems) {
|
| 110 |
let finalMessage = `All done! Your final score: ${score} / ${totalItems}! π`;
|
| 111 |
-
if (score === totalItems)
|
| 112 |
-
|
| 113 |
-
} else if (score >= totalItems / 2) {
|
| 114 |
-
finalMessage += " Great job! π";
|
| 115 |
-
}
|
| 116 |
feedbackDisplay.textContent = finalMessage;
|
| 117 |
feedbackDisplay.className = 'correct-feedback';
|
| 118 |
targetCoordsDisplay.textContent = "(β,β)";
|
| 119 |
itemNameDisplay.textContent = 'all the items';
|
| 120 |
-
// Optional: Add a visible reset button here instead of forcing refresh
|
| 121 |
return;
|
| 122 |
}
|
| 123 |
|
| 124 |
const currentItem = itemsToFind[currentItemIndex];
|
| 125 |
|
| 126 |
-
//
|
| 127 |
-
let newTargetFound = false;
|
| 128 |
-
|
| 129 |
-
const maxAttempts = gridSize * gridSize * 2; // Generous limit
|
| 130 |
-
|
| 131 |
while (!newTargetFound && attempts < maxAttempts) {
|
| 132 |
targetX = Math.floor(Math.random() * gridSize) + 1;
|
| 133 |
targetY = Math.floor(Math.random() * gridSize) + 1;
|
| 134 |
const potentialCell = gridContainer.querySelector(`.grid-cell[data-x="${targetX}"][data-y="${targetY}"]`);
|
| 135 |
-
|
| 136 |
-
// Check if the cell exists AND is visually empty (no emoji)
|
| 137 |
-
if (potentialCell && potentialCell.textContent.trim() === '') {
|
| 138 |
-
newTargetFound = true;
|
| 139 |
-
}
|
| 140 |
attempts++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
feedbackDisplay.textContent = "Uh oh, the map is full! Please refresh.";
|
| 147 |
-
return;
|
| 148 |
}
|
| 149 |
|
| 150 |
-
//
|
| 151 |
itemNameDisplay.textContent = currentItem.name;
|
| 152 |
targetCoordsDisplay.textContent = `(${targetX}, ${targetY})`;
|
| 153 |
feedbackDisplay.textContent = `Where is ${currentItem.name}? (${currentItemIndex + 1}/${totalItems})`;
|
| 154 |
feedbackDisplay.className = '';
|
| 155 |
-
|
| 156 |
-
// Reset temporary styles from previous round
|
| 157 |
document.querySelectorAll('.grid-cell').forEach(cell => {
|
| 158 |
cell.classList.remove('just-found', 'incorrect');
|
| 159 |
});
|
| 160 |
console.log(`Round ${currentItemIndex + 1}: Find ${currentItem.name} at (${targetX}, ${targetY})`);
|
| 161 |
}
|
| 162 |
|
| 163 |
-
// --- Handle Cell Click ---
|
| 164 |
function handleCellClick(event) {
|
| 165 |
-
// Prevent action if round/game is over or cell already found
|
| 166 |
if (foundCurrent || currentItemIndex >= totalItems) return;
|
| 167 |
-
|
| 168 |
const clickedCell = event.target;
|
| 169 |
if (clickedCell.classList.contains('found-item')) return;
|
| 170 |
|
| 171 |
const clickedX = parseInt(clickedCell.dataset.x);
|
| 172 |
const clickedY = parseInt(clickedCell.dataset.y);
|
|
|
|
| 173 |
|
| 174 |
-
// Clear any lingering incorrect styles
|
| 175 |
-
document.querySelectorAll('.grid-cell.incorrect').forEach(cell => {
|
| 176 |
-
cell.classList.remove('incorrect');
|
| 177 |
-
});
|
| 178 |
-
|
| 179 |
-
// --- Check if Correct ---
|
| 180 |
if (clickedX === targetX && clickedY === targetY) {
|
| 181 |
-
foundCurrent = true;
|
| 182 |
-
score++;
|
| 183 |
-
updateScoreDisplay();
|
| 184 |
-
|
| 185 |
const currentItem = itemsToFind[currentItemIndex];
|
| 186 |
-
|
| 187 |
feedbackDisplay.textContent = `You found ${currentItem.name}! π`;
|
| 188 |
feedbackDisplay.className = 'correct-feedback';
|
| 189 |
-
|
| 190 |
-
clickedCell.
|
| 191 |
-
clickedCell.classList.add('just-found'); // Temporary highlight
|
| 192 |
-
|
| 193 |
-
// After highlight animation, switch to permanent 'found' style
|
| 194 |
setTimeout(() => {
|
| 195 |
clickedCell.classList.remove('just-found');
|
| 196 |
clickedCell.classList.add('found-item');
|
| 197 |
-
}, 600);
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
setTimeout(setNewTarget, 1800); // Delay before next target
|
| 202 |
-
|
| 203 |
} else {
|
| 204 |
-
// --- Incorrect Guess ---
|
| 205 |
feedbackDisplay.textContent = "Not quite! Try again. π€";
|
| 206 |
feedbackDisplay.className = 'incorrect-feedback';
|
| 207 |
clickedCell.classList.add('incorrect');
|
| 208 |
-
|
| 209 |
-
// Remove incorrect style after shake animation
|
| 210 |
setTimeout(() => {
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
clickedCell.classList.remove('incorrect');
|
| 214 |
-
}
|
| 215 |
-
}, 500); // Match shake animation duration
|
| 216 |
}
|
| 217 |
}
|
| 218 |
|
|
|
|
| 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
// --- Initialize Grid and Axes ---
|
| 33 |
function initializeGameArea() {
|
|
|
|
| 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`;
|
| 49 |
gridContainer.style.height = `${gridTotalSize}px`;
|
| 50 |
+
gridContainer.style.display = 'grid';
|
| 51 |
+
gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, ${cellSize}px)`;
|
| 52 |
+
gridContainer.style.gridTemplateRows = `repeat(${gridSize}, ${cellSize}px)`;
|
|
|
|
| 53 |
|
| 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 |
|
| 61 |
+
// Create Y-axis labels
|
| 62 |
for (let y = 1; y <= gridSize; y++) {
|
| 63 |
const label = document.createElement('div');
|
| 64 |
label.classList.add('axis-label', 'y-axis-label');
|
|
|
|
| 67 |
yAxisLabelsContainer.appendChild(label);
|
| 68 |
}
|
| 69 |
|
| 70 |
+
// Create X-axis labels
|
| 71 |
for (let x = 1; x <= gridSize; x++) {
|
| 72 |
const label = document.createElement('div');
|
| 73 |
label.classList.add('axis-label', 'x-axis-label');
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
// Create grid cells
|
|
|
|
| 80 |
for (let y = gridSize; y >= 1; y--) {
|
|
|
|
| 81 |
for (let x = 1; x <= gridSize; x++) {
|
| 82 |
const cell = document.createElement('div');
|
| 83 |
cell.classList.add('grid-cell');
|
| 84 |
+
cell.dataset.x = x; cell.dataset.y = y;
|
| 85 |
+
cell.style.width = `${cellSize}px`; cell.style.height = `${cellSize}px`;
|
|
|
|
|
|
|
| 86 |
cell.addEventListener('click', handleCellClick);
|
| 87 |
gridContainer.appendChild(cell);
|
| 88 |
}
|
| 89 |
}
|
| 90 |
+
console.log(`Grid (${gridSize}x${gridSize}), Axes, and Score Initialized.`);
|
| 91 |
}
|
| 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) {
|
| 121 |
targetX = Math.floor(Math.random() * gridSize) + 1;
|
| 122 |
targetY = Math.floor(Math.random() * gridSize) + 1;
|
| 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})`;
|
| 143 |
feedbackDisplay.className = '';
|
|
|
|
|
|
|
| 144 |
document.querySelectorAll('.grid-cell').forEach(cell => {
|
| 145 |
cell.classList.remove('just-found', 'incorrect');
|
| 146 |
});
|
| 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;
|
| 154 |
if (clickedCell.classList.contains('found-item')) return;
|
| 155 |
|
| 156 |
const clickedX = parseInt(clickedCell.dataset.x);
|
| 157 |
const clickedY = parseInt(clickedCell.dataset.y);
|
| 158 |
+
document.querySelectorAll('.grid-cell.incorrect').forEach(cell => { cell.classList.remove('incorrect'); });
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
if (clickedX === targetX && clickedY === targetY) {
|
| 161 |
+
foundCurrent = true; score++; updateScoreDisplay();
|
|
|
|
|
|
|
|
|
|
| 162 |
const currentItem = itemsToFind[currentItemIndex];
|
|
|
|
| 163 |
feedbackDisplay.textContent = `You found ${currentItem.name}! π`;
|
| 164 |
feedbackDisplay.className = 'correct-feedback';
|
| 165 |
+
clickedCell.textContent = currentItem.emoji;
|
| 166 |
+
clickedCell.classList.add('just-found');
|
|
|
|
|
|
|
|
|
|
| 167 |
setTimeout(() => {
|
| 168 |
clickedCell.classList.remove('just-found');
|
| 169 |
clickedCell.classList.add('found-item');
|
| 170 |
+
}, 600);
|
| 171 |
+
currentItemIndex++;
|
| 172 |
+
setTimeout(setNewTarget, 1800);
|
|
|
|
|
|
|
|
|
|
| 173 |
} else {
|
|
|
|
| 174 |
feedbackDisplay.textContent = "Not quite! Try again. π€";
|
| 175 |
feedbackDisplay.className = 'incorrect-feedback';
|
| 176 |
clickedCell.classList.add('incorrect');
|
|
|
|
|
|
|
| 177 |
setTimeout(() => {
|
| 178 |
+
if (clickedCell.classList.contains('incorrect')) { clickedCell.classList.remove('incorrect'); }
|
| 179 |
+
}, 500);
|
|
|
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|