Created
August 23, 2012 03:39
-
-
Save bigwheel/3431969 to your computer and use it in GitHub Desktop.
ランダムに2チームへ分けるスクリプト ハイアンドロー編
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 | |
class Member | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def win?(opponent) | |
raise 'should be overrided' | |
end | |
end | |
class HighAndLowMember < Member | |
def win?(opponent) | |
begin | |
now_number = rand(13) | |
next_number = rand(13) | |
end while now_number == next_number | |
if opponent.sayHigh?(now_number) | |
now_number > next_number | |
else | |
now_number < next_number | |
end | |
end | |
def sayHigh?(now_number) | |
[ true, false ].sample | |
end | |
end | |
if $0 == __FILE__ | |
srand(Time.local(2012, 8, 23).to_i) | |
members = ARGF.map do |name| | |
HighAndLowMember.new(name.chomp) | |
end | |
def members.choose! | |
delete_at(rand(count)) | |
end | |
a_team = [] | |
b_team = [] | |
teams = [a_team, b_team] | |
while members.count != 0 | |
if members.count == 1 | |
teams[rand(2)] << members.choose! | |
break | |
end | |
red = members.choose! | |
blue = members.choose! | |
if red.win?(blue) | |
a_team << red | |
b_team << blue | |
else | |
a_team << blue | |
b_team << red | |
end | |
end | |
puts "A: #{a_team.map {|member| member.name }.join(' ')}" | |
puts "B: #{b_team.map {|member| member.name }.join(' ')}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment