Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ランダムに2チームへ分けるスクリプト ジャンケン編 - モジュールに機能を分離
#!/usr/bin/env ruby
class Member
attr_reader :name
def initialize(name)
@name = name
end
end
class Symbol
def win? rhs
if self == rhs
:even
else
case self
when :gu
if rhs == :choki
:win
else # rhs == :pa
:lose
end
when :choki
if rhs == :gu
:lose
else # rhs == :pa
:win
end
when :pa
if rhs == :gu
:win
else # rhs == :choki
:lose
end
end
end
end
end
module Janken
def win?(opponent)
begin
result = self.hand.win? opponent.hand
end while result == :even
result == :win
end
def hand
[:gu, :choki, :pa].at(rand(3))
end
end
class JankenMember < Member
include Janken
end
if $0 == __FILE__
srand(Time.local(2012, 8, 16).to_i)
members = ARGF.map do |name|
JankenMember.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
You can’t perform that action at this time.