File size: 12,131 Bytes
8055c60
 
1780a45
8055c60
f4ddc05
 
 
1780a45
 
8055c60
 
1780a45
 
8055c60
 
 
 
 
6b74c43
1780a45
6b74c43
8055c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d32863f
8055c60
 
 
 
 
 
d32863f
a7c5b6a
 
d32863f
1780a45
d32863f
 
 
 
 
 
 
 
 
 
 
 
 
1780a45
d32863f
1780a45
 
d32863f
a7c5b6a
6b74c43
8055c60
 
 
 
 
 
 
 
 
 
 
 
 
 
1780a45
8055c60
 
 
 
1780a45
8055c60
 
 
 
 
 
1780a45
8055c60
 
1780a45
 
8055c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d32863f
8055c60
 
 
 
d32863f
1780a45
 
 
 
 
8055c60
d32863f
 
 
 
 
 
 
 
1780a45
8055c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1780a45
 
 
8055c60
 
 
 
 
 
 
 
 
 
d32863f
 
 
8055c60
 
d32863f
8055c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
document.addEventListener('DOMContentLoaded', () => {
    // Game constants
    const GRID_SIZE = 41;
    const CELL_COUNT = GRID_SIZE * GRID_SIZE;
    const ENEMY_COUNT = 5;
    const FOOD_COUNT = 5;
const MAX_HEALTH = 5;
    const ENEMY_SCORE = 100;
const FOOD_SCORE = 50;
    
    // Game state
    let playerPosition = (2 * GRID_SIZE) + 2; // 2 rows down, 2 columns in
let health = MAX_HEALTH;
    let score = 0;
    let gameOver = false;
    let maze = [];
    let enemies = [];
    let foodPositions = [];
    let waterPosition = Math.floor((GRID_SIZE * GRID_SIZE) / 2); // Center
    let goalPosition = (GRID_SIZE * (GRID_SIZE - 2)) + (GRID_SIZE - 2); // 2 from bottom, 2 from right
// DOM elements
    const mazeGrid = document.querySelector('.maze-grid');
    const scoreValue = document.getElementById('scoreValue');
    const healthBars = document.querySelectorAll('.health-bars div');
    const newMazeBtn = document.getElementById('newMazeBtn');
    const resetBtn = document.getElementById('resetBtn');
    
    // Initialize game
    function initGame() {
        generateMaze();
        placePlayer();
        placeEnemies();
        placeFood();
        placeWater();
        updateUI();
        addEventListeners();
    }
    // Generate maze with walls and paths ensuring a path exists
    function generateMaze() {
        mazeGrid.innerHTML = '';
        maze = Array(CELL_COUNT).fill().map((_, i) => {
            const cell = document.createElement('div');
            cell.className = 'cell';
            cell.dataset.index = i;
            
            const row = Math.floor(i / GRID_SIZE);
            const col = i % GRID_SIZE;
            
            // Solid outer perimeter walls
            let isWall = (row === 0 || row === GRID_SIZE-1 || col === 0 || col === GRID_SIZE-1);
            
            // Create a guaranteed path from start to goal
            if (!isWall) {
                // Create a diagonal path from start to goal
                const isOnPath = 
                    (row === Math.floor(col * (GRID_SIZE-4)/(GRID_SIZE-4)) + 2) || 
                    (col === Math.floor(row * (GRID_SIZE-4)/(GRID_SIZE-4)) + 2);
                
                // Add some randomness to the path
                if (!isOnPath) {
                    isWall = Math.random() < 0.3;
                }
            }
            
            // Ensure start, water and goal positions are accessible
            if (i === playerPosition || i === waterPosition || i === goalPosition) {
                isWall = false;
            }
if (isWall) {
                cell.classList.add('wall');
            } else {
                cell.classList.add('path');
            }
            
            mazeGrid.appendChild(cell);
            return {
                element: cell,
                isWall: isWall
            };
        });
    }
    // Place player at start position
    function placePlayer() {
        playerPosition = (2 * GRID_SIZE) + 2;
        const cell = maze[playerPosition].element;
        cell.classList.add('player');
        cell.innerHTML = '🌱';
    }
// Place enemies randomly
    function placeEnemies() {
        enemies = [];
        
        while (enemies.length < ENEMY_COUNT) {
            const pos = Math.floor(Math.random() * CELL_COUNT);
            
            if (!maze[pos].isWall &&
                pos !== playerPosition && 
                pos !== waterPosition && 
                pos !== goalPosition && 
!enemies.includes(pos)) {
                
                enemies.push(pos);
                const cell = maze[pos].element;
                cell.classList.add('enemy');
                cell.innerHTML = '🌸';
            }
        }
    }
    
    // Place food randomly
    function placeFood() {
        foodPositions = [];
        
        while (foodPositions.length < FOOD_COUNT) {
            const pos = Math.floor(Math.random() * CELL_COUNT);
            
            if (!maze[pos].isWall && 
                pos !== playerPosition && 
                pos !== waterPosition && 
                !enemies.includes(pos) && 
                !foodPositions.includes(pos)) {
                
                foodPositions.push(pos);
                const cell = maze[pos].element;
                cell.classList.add('food');
                cell.innerHTML = 'πŸ•';
            }
        }
    }
    // Place water at center with random outcome
    function placeWater() {
        const cell = maze[waterPosition].element;
        cell.classList.add('water');
        cell.innerHTML = 'πŸ’§';
        updateWaterColor();
        
        // Mark goal position
        const goalCell = maze[goalPosition].element;
        goalCell.classList.add('goal');
        goalCell.innerHTML = '🏁';
    }

    // Update water color based on random outcome
    function updateWaterColor() {
        const waterCell = maze[waterPosition].element;
        const willWin = Math.random() > 0.5;
        waterCell.style.backgroundColor = willWin ? '#bbf7d0' : '#fecaca';
        waterCell.style.border = willWin ? '1px solid #4ade80' : '1px solid #ef4444';
    }
// Update UI elements
    function updateUI() {
        scoreValue.textContent = score;
        
        // Update health display
        healthBars.forEach((bar, i) => {
            if (i < health) {
                bar.classList.add('bg-green-500');
                bar.classList.remove('bg-gray-300');
            } else {
                bar.classList.add('bg-gray-300');
                bar.classList.remove('bg-green-500');
            }
        });
    }
    
    // Handle player movement
    function movePlayer(direction) {
        if (gameOver) return;
        
        let newPosition = playerPosition;
        const row = Math.floor(playerPosition / GRID_SIZE);
        const col = playerPosition % GRID_SIZE;
        
        switch (direction) {
            case 'up':
                if (row > 0) newPosition = playerPosition - GRID_SIZE;
                break;
            case 'down':
                if (row < GRID_SIZE - 1) newPosition = playerPosition + GRID_SIZE;
                break;
            case 'left':
                if (col > 0) newPosition = playerPosition - 1;
                break;
            case 'right':
                if (col < GRID_SIZE - 1) newPosition = playerPosition + 1;
                break;
        }
        
        // Check if new position is valid (not a wall)
        if (newPosition !== playerPosition && !maze[newPosition].isWall) {
            // Clear current player position
            const currentCell = maze[playerPosition].element;
            currentCell.classList.remove('player');
            currentCell.innerHTML = '';
            
            // Check what's at new position
            const newCell = maze[newPosition].element;
            
            // Check for enemy collision
            if (enemies.includes(newPosition)) {
                health--;
                score += ENEMY_SCORE;
                enemies = enemies.filter(pos => pos !== newPosition);
                newCell.classList.remove('enemy');
                
                if (health <= 0) {
                    endGame(false);
                    return;
                }
            }
            
            // Check for food collection
            if (foodPositions.includes(newPosition)) {
                health = Math.min(health + 1, MAX_HEALTH);
                score += FOOD_SCORE;
                foodPositions = foodPositions.filter(pos => pos !== newPosition);
                newCell.classList.remove('food');
            }
            
            // Check for water pot
            if (newPosition === waterPosition) {
                const isWin = Math.random() > 0.5;
                endGame(isWin);
                return;
            }
            // Check for win condition (2 from bottom, 2 from right)
            if (newPosition === goalPosition) {
endGame(true);
                return;
            }
            // Move player
            playerPosition = newPosition;
            newCell.classList.add('player');
            newCell.innerHTML = '🌱';
            
            // Move enemies
            moveEnemies();
            
            // Update water color with each move
            updateWaterColor();
            
            // Update UI
            updateUI();
}
    }
    
    // Move enemies towards player
    function moveEnemies() {
        enemies.forEach((enemyPos, index) => {
            const enemyCell = maze[enemyPos].element;
            enemyCell.classList.remove('enemy');
            enemyCell.innerHTML = '';
            
            const playerRow = Math.floor(playerPosition / GRID_SIZE);
            const playerCol = playerPosition % GRID_SIZE;
            const enemyRow = Math.floor(enemyPos / GRID_SIZE);
            const enemyCol = enemyPos % GRID_SIZE;
            
            // Simple AI: move towards player
            let newRow = enemyRow;
            let newCol = enemyCol;
            
            if (enemyRow < playerRow) newRow++;
            else if (enemyRow > playerRow) newRow--;
            
            if (enemyCol < playerCol) newCol++;
            else if (enemyCol > playerCol) newCol--;
            
            // Randomize direction sometimes
            if (Math.random() < 0.3) {
                if (Math.random() < 0.5) {
                    newRow = enemyRow + (Math.random() < 0.5 ? 1 : -1);
                } else {
                    newCol = enemyCol + (Math.random() < 0.5 ? 1 : -1);
                }
            }
            
            // Ensure new position is within bounds
            newRow = Math.max(0, Math.min(GRID_SIZE - 1, newRow));
            newCol = Math.max(0, Math.min(GRID_SIZE - 1, newCol));
            
            const newEnemyPos = newRow * GRID_SIZE + newCol;
            
            // Only move if not a wall and not another enemy
            if (!maze[newEnemyPos].isWall && !enemies.includes(newEnemyPos) && newEnemyPos !== playerPosition) {
                enemies[index] = newEnemyPos;
            }
            
            // Update enemy display
            const newEnemyCell = maze[enemies[index]].element;
            newEnemyCell.classList.add('enemy');
            newEnemyCell.innerHTML = '🌸';
        });
    }
    
    // End game with win/lose
    function endGame(isWin) {
        gameOver = true;
        
        const gameOverDiv = document.createElement('div');
        gameOverDiv.className = 'game-over';
        gameOverDiv.innerHTML = `
            <h2>${isWin ? 'You Won! πŸŽ‰' : 'Game Over! πŸ’€'}</h2>
            <p>Final Score: ${score}</p>
            <button onclick="location.reload()">Play Again</button>
        `;
        
        mazeGrid.appendChild(gameOverDiv);
    }
    
    // Add event listeners
    function addEventListeners() {
        // Keyboard controls
        document.addEventListener('keydown', (e) => {
            switch (e.key) {
                case 'ArrowUp':
                    movePlayer('up');
                    break;
                case 'ArrowDown':
                    movePlayer('down');
                    break;
                case 'ArrowLeft':
                    movePlayer('left');
                    break;
                case 'ArrowRight':
                    movePlayer('right');
                    break;
            }
        });
        
        // New maze button
        newMazeBtn.addEventListener('click', () => {
            generateMaze();
            placePlayer();
            placeEnemies();
            placeFood();
            placeWater();
            gameOver = false;
            score = 0;
            health = MAX_HEALTH;
            updateUI();
        });
        
        // Reset button
        resetBtn.addEventListener('click', () => {
            placePlayer();
            placeEnemies();
            placeFood();
            gameOver = false;
            score = 0;
            health = MAX_HEALTH;
            updateUI();
        });
    }
    
    // Start the game
    initGame();
});