[ create a new paste ] login | about

Link: http://codepad.org/Q9iW5lBw    [ raw code | output | fork ]

joshua_cheek - Ruby, pasted on Oct 18:
puts "VERSION = #{VERSION}"


class RPS
  
  Hierarchy = {
    :rock      =>  { :rock => :tie  , :paper => :lose , :scissors => :win  } ,
    :paper     =>  { :rock => :win  , :paper => :tie  , :scissors => :lose } ,
    :scissors  =>  { :rock => :lose , :paper => :win  , :scissors => :tie  } ,
  }
  
  Choices = Hierarchy.keys
  
  attr_reader :guess
  def initialize( guess )
    guess = guess.to_sym
    raise "invalid guess" unless Choices.include?(guess)
    @guess = guess
  end
  
  def fight     ( opponent )   Hierarchy[guess][opponent.guess]   end
  def beats?    ( opponent )   fight(opponent) == :win            end
  def beat_by?  ( opponent )   fight(opponent) == :lose           end
  def ties?     ( opponent )   fight(opponent) == :tie            end
  def to_s      (          )   guess.to_s                         end
  
end



RPS::Choices.each do |userinput1|  RPS::Choices.each do |userinput2|
  
  player1,player2 = RPS.new(userinput1),RPS.new(userinput2)
  
  outcome = player1.fight(player2)
  
  puts "player1 chooses #{player1.guess} and #{outcome}s " \
       "the fight with player2 who chooses #{player2.guess}"
       
end end


Output:
1
2
3
4
5
6
7
8
9
10
VERSION = 1.8.6
player1 chooses paper and ties the fight with player2 who chooses paper
player1 chooses paper and loses the fight with player2 who chooses scissors
player1 chooses paper and wins the fight with player2 who chooses rock
player1 chooses scissors and wins the fight with player2 who chooses paper
player1 chooses scissors and ties the fight with player2 who chooses scissors
player1 chooses scissors and loses the fight with player2 who chooses rock
player1 chooses rock and loses the fight with player2 who chooses paper
player1 chooses rock and wins the fight with player2 who chooses scissors
player1 chooses rock and ties the fight with player2 who chooses rock


Create a new paste based on this one


Comments: