File size: 8,955 Bytes
a91cf94
 
 
 
 
 
 
 
 
 
 
 
07f11de
 
a91cf94
07f11de
a91cf94
 
 
 
 
07f11de
 
 
 
a91cf94
07f11de
a91cf94
 
07f11de
a91cf94
 
 
 
 
 
 
 
07f11de
a91cf94
 
 
 
07f11de
 
a91cf94
07f11de
a91cf94
 
07f11de
 
 
a91cf94
 
07f11de
 
 
 
 
a91cf94
07f11de
a91cf94
 
 
 
 
 
 
 
07f11de
a91cf94
 
 
 
 
 
 
 
 
 
 
 
 
07f11de
 
a91cf94
 
 
 
07f11de
a91cf94
 
 
 
07f11de
a91cf94
 
 
07f11de
a91cf94
 
 
07f11de
a91cf94
 
07f11de
 
a91cf94
 
 
 
 
 
 
 
 
07f11de
 
 
a91cf94
 
 
 
07f11de
a91cf94
07f11de
 
 
 
 
 
a91cf94
07f11de
 
 
 
a91cf94
 
07f11de
a91cf94
 
 
 
 
 
 
 
 
 
07f11de
a91cf94
 
 
 
 
 
 
07f11de
a91cf94
 
07f11de
a91cf94
 
 
07f11de
 
a91cf94
 
 
07f11de
 
 
a91cf94
 
 
 
 
07f11de
 
a91cf94
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
document.addEventListener('DOMContentLoaded', () => {
    // --- Get Elements ---
    const gridContainer = document.getElementById('grid-container');
    const targetCoordsDisplay = document.getElementById('target-coords');
    const feedbackDisplay = document.getElementById('feedback');
    const itemNameDisplay = document.getElementById('item-name');
    const yAxisLabelsContainer = document.getElementById('y-axis-labels');
    const xAxisLabelsContainer = document.getElementById('x-axis-labels');
    const xAxisContainer = document.getElementById('x-axis-container');
    const scoreDisplay = document.getElementById('score-display');

    // --- Game Settings ---
    const gridSize = 12; // << INCREASED GRID SIZE (e.g., 12x12)
    const cellSize = 40; // Keep cell size, or adjust (e.g., 35 for smaller cells on bigger grid)

    // --- Items to Find --- << ADDED MORE ITEMS
    const itemsToFind = [
        { name: "a sparkling diamond", emoji: "πŸ’Ž" }, { name: "a red apple", emoji: "🍎" },
        { name: "a yummy pizza slice", emoji: "πŸ•" }, { name: "a fast rocket", emoji: "πŸš€" },
        { name: "a cute puppy", emoji: "🐢" }, { name: "a bright star", emoji: "⭐" },
        { name: "a cool robot", emoji: "πŸ€–" }, { name: "a friendly ghost", emoji: "πŸ‘»" },
        { name: "a birthday cake", emoji: "πŸŽ‚" }, { name: "a magical unicorn", emoji: "πŸ¦„" },
        { name: "a buzzing bee", emoji: "🐝" }, { name: "a soccer ball", emoji: "⚽" },
        { name: "a juicy strawberry", emoji: "πŸ“" }, { name: "a treasure chest", emoji: "πŸͺ™" },// Use coin if chest unavailable
        { name: "a sleepy koala", emoji: "🐨" }
    ];
    const totalItems = itemsToFind.length; // **Dynamically get count**

    // --- Game State ---
    let targetX = -1, targetY = -1, currentItemIndex = 0, foundCurrent = false, score = 0;

    // --- Initialize Grid and Axes ---
    function initializeGameArea() {
        gridContainer.innerHTML = '';
        xAxisLabelsContainer.innerHTML = '';
        yAxisLabelsContainer.innerHTML = '';
        score = 0;
        currentItemIndex = 0;
        updateScoreDisplay(); // **Use dynamic totalItems here**

        document.documentElement.style.setProperty('--cell-size', `${cellSize}px`);

        const gridTotalSize = gridSize * cellSize;
        const yAxisWidth = 20;
        const yAxisMargin = 5;

        // Configure Grid Container Styles Explicitly
        gridContainer.style.width = `${gridTotalSize}px`;
        gridContainer.style.height = `${gridTotalSize}px`;
        gridContainer.style.display = 'grid';
        gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, ${cellSize}px)`;
        gridContainer.style.gridTemplateRows = `repeat(${gridSize}, ${cellSize}px)`;

        // Configure Axis Containers
        yAxisLabelsContainer.style.height = `${gridTotalSize}px`;
        xAxisLabelsContainer.style.width = `${gridTotalSize}px`;
        // Make the outer X container wide enough for Y axis space + grid
        xAxisContainer.style.width = `${yAxisWidth + yAxisMargin + gridTotalSize}px`;
        xAxisLabelsContainer.style.marginLeft = `${yAxisWidth + yAxisMargin}px`;

        // Create Y-axis labels
        for (let y = 1; y <= gridSize; y++) {
            const label = document.createElement('div');
            label.classList.add('axis-label', 'y-axis-label');
            label.textContent = y;
            label.style.height = `${cellSize}px`;
            yAxisLabelsContainer.appendChild(label);
        }

        // Create X-axis labels
        for (let x = 1; x <= gridSize; x++) {
            const label = document.createElement('div');
            label.classList.add('axis-label', 'x-axis-label');
            label.textContent = x;
            label.style.width = `${cellSize}px`;
            xAxisLabelsContainer.appendChild(label);
        }

        // Create grid cells
        for (let y = gridSize; y >= 1; y--) {
            for (let x = 1; x <= gridSize; x++) {
                const cell = document.createElement('div');
                cell.classList.add('grid-cell');
                cell.dataset.x = x; cell.dataset.y = y;
                cell.style.width = `${cellSize}px`; cell.style.height = `${cellSize}px`;
                cell.addEventListener('click', handleCellClick);
                gridContainer.appendChild(cell);
            }
        }
        console.log(`Grid (${gridSize}x${gridSize}), Axes, and Score Initialized.`);
    }

     // --- Update Score Display ---
     function updateScoreDisplay() {
        // **Use totalItems variable**
        scoreDisplay.textContent = `Score: ${score} / ${totalItems}`;
    }

    // --- Set New Target ---
    function setNewTarget() {
        foundCurrent = false;

        // Game Over Check
        if (currentItemIndex >= totalItems) {
            let finalMessage = `All done! Your final score: ${score} / ${totalItems}! πŸŽ‰`;
            if (score === totalItems) finalMessage += " Perfect score! πŸ₯³";
            else if (score >= totalItems * 0.7) finalMessage += " Great job! πŸ‘"; // Adjust threshold
            feedbackDisplay.textContent = finalMessage;
            feedbackDisplay.className = 'correct-feedback';
            targetCoordsDisplay.textContent = "(βœ”,βœ”)";
            itemNameDisplay.textContent = 'all the items';
            return;
        }

        const currentItem = itemsToFind[currentItemIndex];

        // Find Empty Cell (same logic as before)
        let newTargetFound = false, attempts = 0;
        const maxAttempts = gridSize * gridSize * 2;
        while (!newTargetFound && attempts < maxAttempts) {
            targetX = Math.floor(Math.random() * gridSize) + 1;
            targetY = Math.floor(Math.random() * gridSize) + 1;
            const potentialCell = gridContainer.querySelector(`.grid-cell[data-x="${targetX}"][data-y="${targetY}"]`);
            if (potentialCell && potentialCell.textContent.trim() === '') { newTargetFound = true; }
            attempts++;
            if(attempts >= maxAttempts){ // Add the check after incrementing attempts
                 let allFilled = true;
                 gridContainer.querySelectorAll('.grid-cell').forEach(cell => { if(cell.textContent === '') allFilled = false; });
                 if(allFilled) { console.warn("Grid appears full!"); break; } // Break if grid full
                 else { attempts = 0; } // Reset attempts if grid not full but just unlucky
             }
        }
         if (!newTargetFound) {
              console.error("Could not find an empty cell after many attempts!");
              feedbackDisplay.textContent = "Uh oh, map is too full! Refresh maybe?";
              return;
         }

        // Update display
        itemNameDisplay.textContent = currentItem.name;
        targetCoordsDisplay.textContent = `(${targetX}, ${targetY})`;
        feedbackDisplay.textContent = `Where is ${currentItem.name}? (${currentItemIndex + 1}/${totalItems})`;
        feedbackDisplay.className = '';
        document.querySelectorAll('.grid-cell').forEach(cell => {
            cell.classList.remove('just-found', 'incorrect');
        });
        console.log(`Round ${currentItemIndex + 1}: Find ${currentItem.name} at (${targetX}, ${targetY})`);
    }

    // --- Handle Cell Click (same as before) ---
    function handleCellClick(event) {
        if (foundCurrent || currentItemIndex >= totalItems) return;
        const clickedCell = event.target;
        if (clickedCell.classList.contains('found-item')) return;

        const clickedX = parseInt(clickedCell.dataset.x);
        const clickedY = parseInt(clickedCell.dataset.y);
        document.querySelectorAll('.grid-cell.incorrect').forEach(cell => { cell.classList.remove('incorrect'); });

        if (clickedX === targetX && clickedY === targetY) {
            foundCurrent = true; score++; updateScoreDisplay();
            const currentItem = itemsToFind[currentItemIndex];
            feedbackDisplay.textContent = `You found ${currentItem.name}! πŸ‘`;
            feedbackDisplay.className = 'correct-feedback';
            clickedCell.textContent = currentItem.emoji;
            clickedCell.classList.add('just-found');
            setTimeout(() => {
                clickedCell.classList.remove('just-found');
                clickedCell.classList.add('found-item');
            }, 600);
            currentItemIndex++;
            setTimeout(setNewTarget, 1800);
        } else {
            feedbackDisplay.textContent = "Not quite! Try again. πŸ€”";
            feedbackDisplay.className = 'incorrect-feedback';
            clickedCell.classList.add('incorrect');
            setTimeout(() => {
                if (clickedCell.classList.contains('incorrect')) { clickedCell.classList.remove('incorrect'); }
            }, 500);
        }
    }

    // --- Start the Game ---
    initializeGameArea();
    setNewTarget();

}); // End DOMContentLoaded