Created
June 2, 2012 17:12
-
-
Save abe4tawa8/2859203 to your computer and use it in GitHub Desktop.
Amidakuji
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
#!/usr/bin/env ruby | |
# Based on what N. Ishitobi and Y. Isozaki wrote at minatorubykaigi01. | |
# Modified by Y. Isozaki. | |
class Amidakuji | |
def initialize(people, height=10) | |
@people, @height = people, height | |
end | |
def draw | |
draw_players | |
draw_lines | |
draw_winner | |
end | |
private | |
def draw_players | |
puts ' ' + players.join(' ') + ' ' | |
end | |
def players | |
%w[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z][0..@people-1] | |
end | |
def draw_lines | |
@height.times { draw_line } | |
end | |
def draw_line | |
a = [] | |
while a.length < @people do | |
(a << '| ') while rand(2) == 0 | |
a << '|---' | |
a << '| ' | |
end | |
a[@people-1] = '|' | |
puts ' ' + a[0..@people-1].join | |
end | |
def draw_winner | |
a = Array.new(@people, ' ') | |
a[rand(@people)] = '!!!' | |
puts a.join(' ') | |
end | |
end | |
begin | |
people = ARGV.shift.to_i | |
raise '<number-of-people> must be between 1 and 26' unless people.between?(1, 26) | |
height = ARGV.shift.to_i | |
height = 10 unless height > 0 | |
rescue | |
abort("Usage: #{$0} <number-of-people> [<height>]") | |
end | |
amidakuji = Amidakuji.new(people, height) | |
amidakuji.draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment