-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaze.js
More file actions
77 lines (66 loc) · 1.52 KB
/
maze.js
File metadata and controls
77 lines (66 loc) · 1.52 KB
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
var player = document.getElementById("player");
var steps = 0;
var values = [[0,0,0,0,0,1,1,0,0,0],[0,0,0,1,0,0,0,0,1,0],[0,1,0,0,1,0,0,1,0,0],[0,0,1,1,0,1,0,1,0,1],[1,0,0,0,0,1,0,1,0,0],[1,1,1,0,0,0,0,1,1,0],[0,1,0,0,1,1,1,1,0,0],[0,1,0,0,0,1,0,1,1,0],[0,0,0,1,1,0,0,0,1,0],[1,1,0,0,0,0,1,0,0,0]];
var x = 0, y = 0;
function resetPosition(){
player.style.left = 0;
x=0;
player.style.top = 0;
y=0;
}
function check(){
if(x==9 && y==9){
window.alert("Congrats! You completed the maze in "+steps+" steps.");
steps = 0;
resetPosition();
}
}
function up(){
if(y!=0 && values[x][y-1]!=1){
y--;
player.style.top = y*50 + "px";
steps++;
}
check();
}
function right(){
if(x!=9 && values[x+1][y]!=1){
x++;
player.style.left = x*50 + "px";
steps++;
}
check();
}
function down(){
if(y!=9 && values[x][y+1]!=1){
y++;
player.style.top = y*50 + "px";
steps++;
}
check();
}
function left(){
if(x!=0 && values[x-1][y]!=1){
x--;
player.style.left = x*50 + "px";
steps++;
}
check();
}
document.onkeydown = function(e){
switch(e.keyCode){
case 37:
left();
break;
case 38:
up();
break;
case 39:
right();
break;
case 40:
down();
break;
}
};
//document.addEventListener("onkeydown",function(event){console.log(event.keyCode)},false);