Created
May 27, 2014 09:00
-
-
Save Liooo/c62045b38b15c26a094b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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