Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- maze = function(width, height, complexity, density) {
- shape = c((height / 2) * 2 + 1, (width / 2) * 2 + 1)
- complexity = (complexity * (5 * (shape[1] + shape[2])))
- density = (density * ((shape[1] / 2) * (shape[2] / 2)))
- Z = array(c(rep(0,shape[1]), rep(0,shape[2])), dim=c(shape[1], shape[2]))
- Z[1, ] = Z[shape[1], ] = 1
- Z[, 1] = Z[, shape[2]] = 1
- lappend <- function (lst, ...){
- lst <- c(lst, list(...))
- return(lst)
- }
- set.seed(1)
- for (i in 1:density){
- x = (round(runif(1, 1, shape[2]/2)))*2
- y = (round(runif(1, 1, shape[1]/2)))*2
- Z[y,x] = 1
- for (j in 1:complexity){
- neighbours = list()
- if (x > 2) { neighbours = lappend(neighbours, c(y, x-2)) }
- if (x < shape[2] - 2) { neighbours = lappend(neighbours, c(y, x+2)) }
- if (y > 2) { neighbours = lappend(neighbours, c(y-2, x)) }
- if (y < shape[1] - 2) { neighbours = lappend(neighbours, c(y+2, x)) }
- if (length(neighbours) > 1) {
- sample1 = round(runif(1, 1, length(neighbours)))
- y1 = neighbours[[sample1]][1]
- x1 = neighbours[[sample1]][2]
- if (Z[y1,x1] == 0) {
- Z[y1,x1] = 1
- Z[y1 + (y - y1)/2, x1 + (x - x1) / 2] = 1
- x = x1
- y = x1
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement