--000e0cd211da76f13e04763afec4
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Oct 18, 2009 at 6:21 AM, Matt Beckley <greenlizards / yahoo.com>wrote:

> Trying to write code for rock paper scissors game and read in two values
> from people playing. Take those values and determine the outcome. As I
> understand an array you are able to compare values with the <and I
> think this is the big jump I need to making things work. I would have
> something like this:
>
>  a  rock,paper,scissors] #Comparison array
>  b  userinput1 , userinput2] #User store .a values
>
>
>  if userinput1 < userinput2   #loop for showing who wins
>  elsif userinput1 > userinput2
>  else userinput1 userinput2
>
> So my question is how do I go about comparing my two arrays to determine
> a outcome? I think a hash is the way to go, not sure how to make the
> jump. please include code. Thanks,
> --
> Posted via http://www.ruby-forum.com/.
>
>
How about something like this
http://codepad.org/Q9iW5lBw


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  ierarchy.keys

  attr_reader :guess
  def initialize( guess )
    guess  uess.to_sym
    raise "invalid guess" unless Choices.include?(guess)
    @guess  uess
  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  PS.new(userinput1),RPS.new(userinput2)

  outcome  layer1.fight(player2)

  puts "player1 chooses #{player1.guess} and #{outcome}s " \
       "the fight with player2 who chooses #{player2.guess}"

end end

--000e0cd211da76f13e04763afec4--