Created
June 2, 2012 04:39
-
-
Save sorah/2856619 to your computer and use it in GitHub Desktop.
Usage: amida.rb [members [lines [fillness]]]
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
module Amida | |
EMPTY = " " | |
LINE = "---" | |
def self.amida(member, line_wo_member = 2, fillness=5) | |
people = ('A'..'Z').take(member) | |
hit = people.sample | |
line = member + line_wo_member | |
atari = rand(people.size) | |
result = Result.new | |
result.add people.join(EMPTY) | |
result.add Line.new(member) | |
grid = Grid.new(member, line, fillness).fill | |
grid.to_a.each do |l| | |
result.add l | |
end | |
result.add Line.new(member) | |
result.add "#{" " * atari}!!!", false | |
result.add "", false | |
result.add "Atari: #{people[grid.trace(atari, true)]}", false | |
result.to_s | |
end | |
class Grid | |
def initialize(member, line, fillness) | |
@lines = [] | |
@member = member | |
@filled = (@member-1).times.each_with_index.map { |m, i| | |
Line.new(member).fill(i) | |
} | |
@line = line | |
@fillness = fillness | |
end | |
def fill | |
@lines = @filled.dup | |
(@line-@lines.size).times do | |
l = Line.new(@member) | |
l.fill if rand(@fillness) == 0 | |
@lines << l | |
end | |
@lines.sort_by! { rand } | |
self | |
end | |
def trace(i, reverse=false) | |
lines = @lines.dup | |
lines.reverse! if reverse | |
x = i | |
lines.each do |line| | |
left = line[x-1] | |
right = line[x] | |
if left == LINE | |
x -= 1 | |
elsif right == LINE | |
x += 1 | |
end | |
end | |
x | |
end | |
def to_a | |
@lines | |
end | |
end | |
class Line | |
def initialize(member) | |
@columns = Array.new(member-1, EMPTY) | |
end | |
def fill(i=nil) | |
i = rand(@columns.size) unless i | |
@columns[i] = LINE | |
self | |
end | |
def [](x) | |
x < 0 ? nil : @columns[x] | |
end | |
def to_a | |
@columns | |
end | |
def to_s | |
"|#{@columns.join("|")}|" | |
end | |
end | |
class Result | |
def initialize | |
@str = "" | |
end | |
def add(other, head=true) | |
@str << " " if head | |
@str << other.to_s << "\n" | |
self | |
end | |
def to_s; @str; end | |
end | |
end | |
# amida.rb [members [lines [fillness]]] | |
puts Amida.amida *ARGV.map(&:to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment