Skip to content

Instantly share code, notes, and snippets.

@Liooo
Created May 27, 2014 09:00
Show Gist options
  • Save Liooo/c62045b38b15c26a094b to your computer and use it in GitHub Desktop.
Save Liooo/c62045b38b15c26a094b to your computer and use it in GitHub Desktop.
class Hand
@@HANDS = ['G', 'T', 'P']
@@WIN = 2
@@LOSE = 1
@@DRAW = 0
attr_reader :int_hand, :str_hand
def initialize(str_hand)
@str_hand = str_hand
@int_hand = Hand.to_int(str_hand)
raise 'Unrecognized hand #{str_hand}' if @int_hand.nil?
end
def janken(urhand)
(@int_hand - urhand.int_hand + 3) % 3
end
def get_superior
int_sup_hand = (@int_hand + 2) % 3
Hand.new(Hand.to_string(int_sup_hand))
end
def to_s
@str_hand
end
def self.WIN; @@WIN; end
def self.LOSE; @@LOSE; end
def self.DRAW; @@DRAW; end
private
def self.to_int(str_hand)
@@HANDS.index(str_hand)
end
def self.to_string(int_hand)
@@HANDS[int_hand]
end
end
ur_hands = ARGV[0].split('').map{|v| Hand.new(v)}
first_hand = Hand.new('G')
my_hands = ur_hands.inject([first_hand]) do |mine, ur_hand|
case mine.last.janken(ur_hand)
when Hand.DRAW, Hand.LOSE
mine << mine.last.get_superior
when Hand.WIN
mine << mine.last
end
mine
end
my_hands[0..-2].each do |v|
puts v.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment