Skip to content

Instantly share code, notes, and snippets.

@bigwheel
Created August 9, 2012 03:13
Show Gist options
  • Save bigwheel/3300587 to your computer and use it in GitHub Desktop.
Save bigwheel/3300587 to your computer and use it in GitHub Desktop.
ランダムに2チームへ分けるスクリプト ジャンケン編
#!/usr/bin/env ruby
class Member
attr_reader :name
def initialize(name)
@name = name
end
def win?(opponent)
result_table = { gu: { gu: :even, choki: :win, pa: :lose },
choki: { gu: :lose, choki: :even, pa: :win },
pa: { gu: :win, choki: :lose, pa: :even } }
begin
result = result_table[self.hand][opponent.hand]
end while result == :even
result == :win
end
def hand
[:gu, :choki, :pa].at(rand(3))
end
end
if $0 == __FILE__
srand(Time.local(2012, 8, 9).to_i)
members = ARGF.map do |name|
Member.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