-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
73 lines (61 loc) · 2.21 KB
/
game.js
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
const maze = [
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],
['#', '\uD83D\uDC39', ' ', ' ', ' ', ' ', '#', ' ', '\uD83C\uDF3B', '#'],
['#', ' ', '#', '#', ' ', '#', '#', ' ', ' ', '#'],
['#', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', '#'],
['#', ' ', ' ', ' ', '#', '#', ' ', ' ', ' ', '#'],
['#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#', '#'],
['#', ' ', '#', '#', ' ', '#', '#', ' ', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#'],
['#', ' ', '#', '#', '#', '#', '#', ' ', '\uD83C\uDF3B', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],
];
const gerbil = {
x: 1,
y: 1,
};
function drawMaze() {
const mazeTable = document.getElementById('maze');
mazeTable.innerHTML = '';
for (const row of maze) {
const tr = document.createElement('tr');
for (const cell of row) {
const td = document.createElement('td');
td.textContent = cell;
tr.appendChild(td);
}
mazeTable.appendChild(tr);
}
};
function move(direction) {
const newX = gerbil.x + direction.x;
const newY = gerbil.y + direction.y;
if (maze[newY][newX] !== '#') {
maze[gerbil.y][gerbil.x] = ' ';
gerbil.x = newX;
gerbil.y = newY;
// Check if the gerbil is on a seed
if (maze[gerbil.y][gerbil.x] === '\uD83C\uDF3B') {
// Change the seed to an empty space
maze[gerbil.y][gerbil.x] = ' ';
// Display the "Yum!" message
const messageElement = document.getElementById('message');
messageElement.textContent = 'Yum!';
setTimeout(() => {
messageElement.textContent = '';
}, 1000);
// Check if all seeds are eaten
const hasRemainingSeeds = maze.some(row => row.includes('\uD83C\uDF3B'));
if (!hasRemainingSeeds) {
messageElement.textContent = 'Congrats!';
}
}
maze[gerbil.y][gerbil.x] = '\uD83D\uDC39';
drawMaze();
}
}
document.getElementById('btnUp').addEventListener('click', () => move({ x: 0, y: -1 }));
document.getElementById('btnDown').addEventListener('click', () => move({ x: 0, y: 1 }));
document.getElementById('btnLeft').addEventListener('click', () => move({ x: -1, y: 0 }));
document.getElementById('btnRight').addEventListener('click', () => move({ x: 1, y: 0 }));
drawMaze();