Skip to content

Instantly share code, notes, and snippets.

@june29
Created June 2, 2012 06:25
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 june29/2856957 to your computer and use it in GitHub Desktop.
Save june29/2856957 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Amida
def initialize(number)
@number = number
@depth = number * 2
@goal = rand(number)
@data = generate
end
def render
puts @number.times.to_a.map { |n| "%c" % (n + 65) }.join(" ")
@data.each do |line|
puts [ nil, *line.map { |e| e == 1 ? "---" : " " }, nil ].join("|")
end
puts " " * (@goal * 4) + "*"
end
private
def generate
loop do
prev = 0
amida = @depth.times.map { |i|
(@number - 1).times.map { |j|
if prev == 1
prev = 0
else
prev = rand(100) < 22 ? 1 : 0
end
}
}.to_a
return amida if amida.transpose.all? { |line| line.inject(&:+) > 0 }
end
end
end
n = (ARGV[0] || 5).to_i
amida = Amida.new(n)
amida.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment