Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.