Skip to content

Instantly share code, notes, and snippets.

@fukajun
Created June 2, 2012 05:12
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 fukajun/2856697 to your computer and use it in GitHub Desktop.
Save fukajun/2856697 to your computer and use it in GitHub Desktop.
amidakuji
class Kuji
attr_accessor :bars
def initialize(num, length = nil)
@num = num
length = 4 if length.nil?
@bars = Array.new(length) { |index| Array.new(num - 1, false) }
end
def generate
@bars.each do |bar|
bar.each_with_index do |b, idx|
if can_bar?(bar,idx)
bar[idx] = (rand(2) == 0)
else
bar[idx] = false
end
end
end
end
def can_bar?(b, idx)
if idx == 0
true
elsif b[idx-1]
false
else
true
end
end
private :can_bar?
def output
result = Array.new
result.push (0...@num).to_a.map{|m| m.to_s}.join(' ')
@bars.each do |bar|
result.push '|' + bar.map {|b| b ? "-" : " " }.join('|') + '|'
end
result.push '!'
result.join("\n")
end
end
kuji = Kuji.new(5,5)
kuji.generate
puts kuji.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment