forked from virajj014/JS_RatInAMaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
256 lines (215 loc) · 6.72 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #f46060;
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width: 100vw;
height: 100vh;
}
h1 {
font-size: 50px;
color: white;
margin-bottom: 20px;
font-family: 'Courier New', Courier, monospace;
}
#maze-container {
border: 10px solid black;
position: relative;
display: flex;
flex-direction: column;
width: max-content;
height: max-content;
border-radius: 10px;
}
.row {
display: flex;
}
.cell {
width: 50px;
height: 50px;
/* border: 1px solid rgb(194, 194, 194); */
background-color: white;
}
.wall {
background-color: black;
/* border: 1px solid black; */
}
#rat {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
#food {
position: absolute;
bottom: 0px;
right: 0px;
}
select {
width: 150px;
border-radius: 20px;
border: none;
margin-bottom: 20px;
font-size: 20px;
font-family: 'Courier New', Courier, monospace;
padding: 10px 20px;
}
</style>
<body onload="createMaze()">
<h1>Rat in a Maze</h1>
<select id="levelselect">
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
</select>
<div id="maze-container">
<img src="rat.png" id="rat" width="50px" height="50px" alt="rat">
<img src="food.png" id="food" alt="food" width="50px" height="50px">
</div>
</body>
<script>
let level1 = [
[1, 0, 1, 0],
[0, 1, 1, 1],
[1, 0, 1, 0],
[1, 0, 1, 1]
]
let level2 = [
[1, 1, 1, 0, 1, 0],
[1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 0, 0],
[1, 0, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1]
]
let level3 = [
[1, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 0, 0, 1]
]
let mazearray = level1;
let Level = document.getElementById("levelselect");
Level.addEventListener("change", function () {
let level = Level.value;
// console.log(level);
if (level == 1) {
mazearray = level1;
}
if (level == 2) {
mazearray = level2;
}
if (level == 3) {
mazearray = level3;
}
maze.innerHTML =
`<img src="rat.png" id ="rat" width="50px" height="50px" alt="rat" >
<img src="food.png" alt="rat" width="50px" height="50px" id="food">`
createMaze();
})
let maze = document.getElementById("maze-container");
let rat = document.getElementById("rat");
let food = document.getElementById("food");
function setratposition(x, y) {
rat.style.top = x + "px";
rat.style.left = y + "px";
}
function setfoodposition(x, y) {
food.style.bottom = x + "px";
food.style.right = y + "px";
}
function createMaze() {
for (let i = 0; i < mazearray.length; i++) {
let row = document.createElement("div");
row.classList.add("row");
for (let j = 0; j < mazearray[i].length; j++) {
let cell = document.createElement("div");
cell.classList.add("cell");
if (mazearray[i][j] == 0) {
cell.classList.add("wall");
}
row.appendChild(cell);
// rat = 2 , replace 2 with 0,0 of mazearray ---------------------------------
if (i == 0 && j == 0) {
mazearray[i][j] = 2;
}
}
maze.appendChild(row);
}
setratposition(0, 0)
setfoodposition(0, 0)
// console.log(mazearray);
}
function getratposition() {
// find 2 in mazearray and return its position
let position = [-1, -1];
for (let i = 0; i < mazearray.length; i++) {
for (let j = 0; j < mazearray[i].length; j++) {
if (mazearray[i][j] == 2) {
position[0] = i;
position[1] = j;
}
}
}
console.log(position);
return position;
}
document.addEventListener("keydown", function (e) {
let rat = document.getElementById("rat");
let food = document.getElementById("food");
let ratleft = rat.offsetLeft;
let rattop = rat.offsetTop;
let foodleft = food.offsetLeft;
let foodtop = food.offsetTop;
let ratposition = getratposition();
// console.log(ratleft, rattop);
if (e.key == "ArrowRight" && ratleft < (mazearray.length - 1) * 50 && mazearray[ratposition[0]][ratposition[1] + 1] == 1) {
ratleft += 50;
rat.style.left = ratleft + "px";
mazearray[ratposition[0]][ratposition[1]] = 1;
mazearray[ratposition[0]][ratposition[1] + 1] = 2;
}
if (e.key == "ArrowLeft" && ratleft > 0 && mazearray[ratposition[0]][ratposition[1] - 1] == 1) {
ratleft -= 50;
rat.style.left = ratleft + "px";
mazearray[ratposition[0]][ratposition[1]] = 1;
mazearray[ratposition[0]][ratposition[1] - 1] = 2;
}
if (e.key == "ArrowUp" && rattop > 0 && mazearray[ratposition[0] - 1][ratposition[1]] == 1) {
rattop -= 50;
rat.style.top = rattop + "px";
mazearray[ratposition[0]][ratposition[1]] = 1;
mazearray[ratposition[0] - 1][ratposition[1]] = 2;
}
if (e.key == "ArrowDown" && rattop < (mazearray.length - 1) * 50 && mazearray[ratposition[0] + 1][ratposition[1]] == 1) {
rattop += 50;
rat.style.top = rattop + "px";
mazearray[ratposition[0]][ratposition[1]] = 1;
mazearray[ratposition[0] + 1][ratposition[1]] = 2;
}
if (ratleft == foodleft && rattop == foodtop) {
alert("You Won");
}
})
</script>
</html>