Created
July 16, 2016 10:04
-
-
Save buyoh/5a4eaa1d1dc0ee1d6cd3a2550565de3a to your computer and use it in GitHub Desktop.
木構造の迷路生成 (gistに残す必要は無いけれども)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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