Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.26 KB | None | 0 0
  1. maze = function(width, height, complexity, density) {
  2.   shape = c((height / 2) * 2 + 1, (width / 2) * 2 + 1)
  3.   complexity = (complexity * (5 * (shape[1] + shape[2])))
  4.   density = (density * ((shape[1] / 2) * (shape[2] / 2)))
  5.   Z = array(c(rep(0,shape[1]), rep(0,shape[2])), dim=c(shape[1], shape[2]))
  6.   Z[1, ] = Z[shape[1], ] = 1
  7.   Z[, 1] = Z[, shape[2]] = 1
  8.   lappend <- function (lst, ...){
  9.     lst <- c(lst, list(...))
  10.     return(lst)
  11.   }
  12.   set.seed(1)
  13.   for (i in 1:density){
  14.     x = (round(runif(1, 1, shape[2]/2)))*2
  15.     y = (round(runif(1, 1, shape[1]/2)))*2
  16.     Z[y,x] = 1
  17.     for (j in 1:complexity){
  18.       neighbours = list()
  19.       if (x > 2) { neighbours = lappend(neighbours, c(y, x-2)) }
  20.       if (x < shape[2] - 2) { neighbours = lappend(neighbours, c(y, x+2)) }
  21.       if (y > 2) { neighbours = lappend(neighbours, c(y-2, x)) }
  22.       if (y < shape[1] - 2) { neighbours = lappend(neighbours, c(y+2, x)) }
  23.       if (length(neighbours) > 1) {
  24.         sample1 = round(runif(1, 1, length(neighbours)))
  25.         y1 = neighbours[[sample1]][1]
  26.         x1 = neighbours[[sample1]][2]
  27.         if (Z[y1,x1] == 0) {
  28.           Z[y1,x1] = 1
  29.           Z[y1 + (y - y1)/2, x1 + (x - x1) / 2] = 1
  30.           x = x1
  31.           y = x1
  32.         }
  33.       }
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement