Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local MAZE_SIZE = 100 -- Size of the maze (10x10)
- local WALL_HEIGHT = 10
- local TILE_SIZE = 4
- -- Create a function to generate the maze
- local function generateMaze()
- -- Create a maze grid
- local maze = {}
- for i = 1, MAZE_SIZE do
- maze[i] = {}
- for j = 1, MAZE_SIZE do
- maze[i][j] = true -- true represents a wall
- end
- end
- -- Create a helper function for recursive backtracking
- local function recursiveBacktracking(row, col)
- -- Set the current cell as visited (false represents an open path)
- maze[row][col] = false
- -- Create a random list of directions
- local directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}}
- for i = 1, 4 do
- local randomIndex = math.random(i, 4)
- directions[i], directions[randomIndex] = directions[randomIndex], directions[i]
- end
- -- Loop through the random directions
- for i = 1, 4 do
- local newRow = row + directions[i][1]
- local newCol = col + directions[i][2]
- -- Check if the new cell is within the maze boundaries
- if newRow >= 1 and newRow < MAZE_SIZE and newCol >= 1 and newCol < MAZE_SIZE then
- -- Check if the new cell is a wall
- if maze[newRow][newCol] then
- -- Create a path between the current cell and the new cell
- local pathRow = row + (directions[i][1] / 2)
- local pathCol = col + (directions[i][2] / 2)
- maze[pathRow][pathCol] = false
- -- Recursively call the function on the new cell
- recursiveBacktracking(newRow, newCol)
- end
- end
- end
- end
- -- Start the recursive backtracking algorithm at a random cell
- local startRow = math.random(1, MAZE_SIZE)
- local startCol = math.random(1, MAZE_SIZE)
- recursiveBacktracking(startRow, startCol)
- -- Return the generated maze
- return maze
- end
- -- Function to build the maze in Roblox (same as before)
- local function buildMaze()
- -- Generate the maze
- local maze = generateMaze()
- -- Create a base part to represent a wall
- local wallPart = Instance.new("Part")
- wallPart.Size = Vector3.new(TILE_SIZE, WALL_HEIGHT, TILE_SIZE)
- wallPart.Anchored = true
- wallPart.BrickColor = BrickColor.new("Medium stone grey")
- wallPart.Material = Enum.Material.Concrete
- -- Loop through the maze and create walls where needed
- for i = 1, MAZE_SIZE do
- for j = 1, MAZE_SIZE do
- if maze[i][j] then
- local wall = wallPart:Clone()
- wall.Position = Vector3.new((i - 1) * TILE_SIZE, WALL_HEIGHT / 2, (j - 1) * TILE_SIZE)
- wall.Parent = script
- end
- end
- end
- end
- -- Call the function to build the maze
- buildMaze()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement