18/11/24 14:02:53.32 S/Hnk7Kr.net
>>687 Ruby
def input(message, cond); begin; puts message; end while cond.call; end
def janken
hand_type = %w[g v w]
input('Input the number of opponents.', proc{gets.to_i < 1})
opp_number, hand, hands = $_.to_i
loop do
input('Input your hand.', proc{gets; false})
hand = hand_type.index($_.chomp!)
hands = opp_number.times.map{rand(3)}.unshift(hand)
p hands.map.with_index{|h, i| i == 0 ? ['You', $_] : ["PC#{i}", hand_type[h]]}
hand && hands.uniq.size == 2 ? break : puts('Draw')
end
puts %w[You\ Win! You\ Lose.][3 * hand / hands.uniq.sum / 2]
end
janken
# => Input the number of opponents.
1
Input your hand.
choki
[["You", "choki"], ["PC1", "g"]]
Draw
Input your hand.
w
[["You", "w"], ["PC1", "v"]]
You Lose.