Skip to content

Instantly share code, notes, and snippets.

@supercleanse
Created October 14, 2013 20:55
Show Gist options
  • Select an option

  • Save supercleanse/6982046 to your computer and use it in GitHub Desktop.

Select an option

Save supercleanse/6982046 to your computer and use it in GitHub Desktop.
Maze Generator
# Maze Generating Tweet? Gah
#|--| Top=even/Left=odd
#|-----------| Row %(w+1)
#1,1,1,1,1,1,1,1
#w=6;h=3;m=(w+1)*3;n=(h+1)*3;g=[];s=m*n;s.times{|i|g<<(((i%m==(m-2))or(i%3==2)or(i>(s-m)))? 0: 1)}
#c=(s-3);(w*h).times({ s[(c+2)]=1; x=[(c>m)? (c-m): nil,(c%m==(m-3))? c+3: nil,c<(s-m)? c+m : nil,(c%m>0)? c-3: nil].compact.map{|j|j if(j[2]==0)}.sample;(x>(c+3))? g[x]=0: (x>c)? g[x+1]=0: (x<(c-3))? g[c]=0: g[c+1]=0;c=x})
#(w*h*2).times(|i|((i%2)==0)? print '+'+((g[(i/2)*3]==0)? " ": "-"): print ((g[((i/2)*3)+1]==0)? " ": "|")+' ')
w=80;h=40;g=[];v=[]
(h+1).times{|y|g[y]=[];(w+1).times{|x|g[y]<<[x==w ? 0 : 1,y==h ? 0 : 1]}}
g[h][w-1][0]=g[0][0][0]=0;c=[w-1,h-1]
def t(c,v,w,h,g)
v<<c;x=c[0];y=c[1];n=[[x,y-1],[x+1,y],[x,y+1],[x-1,y]].map{|j|j if(j[0]>=0&&j[0]<w&&j[1]>=0&&j[1]<h)}.compact
n.shuffle.each{|d|
if(!v.include?(d))
o=(d[0]>x)? d[0]: x
p=(d[1]>y)? d[1]: y
(d[0]!=x)? g[p][o][1]=0 : g[p][o][0]=0
t(d,v,w,h,g)
end
}
end
t(c,v,w,h,g)
g.each{|r| r.each{|c| print "+"+((c[0]==1)? "-": " ")};print "\n";r.each{|c|print ((c[1]==1)? "|": " ") + " "};print "\n"}
#0,0 -> 1,1
#1,0 -> 1,1
#
#+-+-+-+-+-+
#| | | | | |
#+-+-+-+-+-+
#| | | | | |
#+-+-+-+-+-+
# Maze Generating Tweet
#w=30;h=10;g=[]
#w.times{|x|g[x]=[];l=[1,1,1,1];h.times{g[x]<<l}}
#c=[w-1,h-1];v=[]
#def t(c,v,w,h,g)
# v<<c
# x=c[0];y=c[1];
# n=[[x,y-1,0,2],[x+1,y,1,3],[x,y+1,2,0],[x-1,y,3,1]].map{|j|j if(j[0]>=0&&j[0]<w&&j[1]>=0&&j[1]<h)}.compact
# f=0
# n.shuffle.each{|d|
# r=[d[0],d[1]]
# if(!v.include?(r))
# if(f==0)
# g[x][y][d[2]]=g[d[0]][d[1]][d[3]]=0
# f=1
# end
# t(r,v,w,h,g)
# end
# }
#end
#t(c,v,w,h,g)
#puts g.inspect
#
#g.each do |l|
# l.each do |r|
# print "+"+(r[0]==1? "-": " ")
# end
# print "+\n"
# l.each do |r|
# print (r[3]==1? "|": " ") + " "
# end
# print (l.last[1]==1? "|": " ")+"\n"
#end
#j[0].between?(0,(w-1))&&j[1].between?(0,(h-1))&&!v.keys.include?(j)
#puts c.inspect
#x=c[0];y=c[1]
#[[x,y-1],[x+1,y],[x,y+1],[x-1,y]].map{|j|j if(j[0].between?(0,(w-1))&&j[1].between?(0,(h-1))&&!v.keys.include?(j))}.compact)
#walk(v,w,h,ca)
#g.each_with_index do |e,i|
# r=[]
# w.times{|j|r<<[j,i]}
# #top
# r.each do |n|
# print "+"+(v[n][0] ? "-" : " ")
# end
# print "\n"
#
# r.each do |n|
# print (v[n][3] ? "|" : " ")+" "
# end
#
# print "\n"
#end
#
#require 'pp'
#
## Maze Generating Tweet
#w=30;h=30;g=[];
#w.times{n=[1,1,1,1];h.times{g<<n}}
#v={};ca=[[(w-1),(h-1)]]
#
#def walk(v,w,h,ca)
# ca.shuffle.each do |c|
# #puts c.inspect
# o = rand(5)
# v[c] = [1,1,1,1]
# v[c][o] = 0
# x=c[0];y=c[1]
# walk(v,w,h,[[x,y-1],[x+1,y],[x,y+1],[x-1,y]].map{|j|j if(j[0].between?(0,(w-1))&&j[1].between?(0,(h-1))&&!v.keys.include?(j))}.compact)
# end
#end
#
#walk(v,w,h,ca)
#
#g.each_with_index do |e,i|
# r=[]
# w.times{|j|r<<[j,i]}
# #top
# r.each do |n|
# print "+"+(v[n][0] ? "-" : " ")
# end
# print "\n"
#
# r.each do |n|
# print (v[n][3] ? "|" : " ")+" "
# end
#
# print "\n"
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment