-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.js
70 lines (61 loc) · 1.62 KB
/
maze.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
/**
* @module maze.js
*
* Provides the interaction logic to move a rabbit through a maze, to reach
* the 'goal' at the centre.
*/
const layout = [
'+--------+-----+---+',
'| | | |',
'| +----+ +---- | | |',
' | | | | | |',
'| | +--+ | +---+-| |',
'| | | | | | |',
'| | | +--+ | +-+ | |',
'| | | | | | | |',
'| | | +----+ | | | |',
'| | | | | | | |',
'| | | | ---+ | | | |',
'| | | | x| | | | |',
'| | | +--+-+ | | |',
'| | | | +---+ | |',
'| | +--+ | | |',
'| | | +---- --+ |',
'| +--+ | | |',
'| | +-- ------+ |',
'+--+ | |',
'++ | +-------------|',
'| | | |',
'| ++ +------------ |',
'| |',
'+------------------+'
]
const hedge = 'images/thujenhecke-1487722_640.jpg'
const path = 'images/imageBlank.png'
const goal = 'images/carrot-6300849_640.png'
const maze = document.getElementById('maze')
const rabbit = document.getElementById('rabbit')
let row, col
function moveRabbit(r, c) {
rabbit.style.top = r * 40 + 'px'
rabbit.style.left = c * 40 + 'px'
row = r
col = c
if (mazeAt(r, c) === 'x') alert('Yay! Reached the goal!')
}
function mazeAt(r, c) {
const row = layout[r]
return row && row[c]
}
// Set up maze:
for (const row of layout) {
for (const c of row) {
let el = new Image(40, 40)
el.src = c === 'x' ? goal : c === ' ' ? path : hedge
maze.appendChild(el)
}
}
moveRabbit(3, 0)
// Put your code here. You need to catch the button click events and then test
// whether the rabbit is able to move in that direction, before updating its
// position.