forked from virajj014/JS_RatInAMaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (35 loc) · 1.27 KB
/
script.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
function createMaze() {
var maze = document.getElementById("maze");
var mazeArray = [];
var mazeRows = 15;
var mazeCols = 15;
// Create a 2D array with random values (0 for path, 1 for wall)
for (var i = 0; i < mazeRows; i++) {
mazeArray[i] = new Array(mazeCols);
for (var j = 0; j < mazeCols; j++) {
mazeArray[i][j] = Math.round(Math.random());
}
}
// Create the maze using the mazeArray
for (var i = 0; i < mazeArray.length; i++) {
var row = maze.insertRow();
for (var j = 0; j < mazeArray[i].length; j++) {
var cell = row.insertCell();
if (mazeArray[i][j] == 1) {
cell.classList.add("wall");
} else {
cell.classList.add("path");
}
}
}
// Add the rat to the maze
var rat = document.getElementById("rat");
var startingRow = Math.floor(Math.random() * (mazeRows - 2)) + 1;
var startingCol = Math.floor(Math.random() * (mazeCols - 2)) + 1;
while (mazeArray[startingRow][startingCol] === 1) {
startingRow = Math.floor(Math.random() * (mazeRows - 2)) + 1;
startingCol = Math.floor(Math.random() * (mazeCols - 2)) + 1;
}
rat.style.top = startingRow * 50 + "px";
rat.style.left = startingCol * 50 + "px";
}