Skip to content

Instantly share code, notes, and snippets.

@buyoh
Created July 16, 2016 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buyoh/5a4eaa1d1dc0ee1d6cd3a2550565de3a to your computer and use it in GitHub Desktop.
Save buyoh/5a4eaa1d1dc0ee1d6cd3a2550565de3a to your computer and use it in GitHub Desktop.
木構造の迷路生成 (gistに残す必要は無いけれども)
@n=59
range=(1..(@n-1)).to_a.reject{|e|e.even?}
@maze=Array.new(@n){Array.new(@n){'#'}}
@n.times{|y|
@n.times{|x|
@maze[x][y]="$" if x==0||y==0||x==@n-1||y==@n-1
}
}
def dig(x,y)
@maze[x][y]="."
a=[[-1,0],[1,0],[0,-1],[0,1]].shuffle
a.each{|e|
if x+e[0]<0 || y+e[1]<0 || x+e[0]>@n-1||y+e[1]>@n-1;p [x,y];exit;end
if (@maze[x+e[0]][y+e[1]]=="#" && @maze[x+e[0]*2][y+e[1]*2]=="#")
@maze[x+e[0]][y+e[1]]="."
dig(x+e[0]*2,y+e[1]*2)
end
}
end
dig(range.sample,range.sample)
@maze.each{|e| puts (e*"").gsub(/\$/){'#'}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment