Matt Beckley 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,

Here's a way that seems to work ...

# Rock Paper Sciscors

def rock_paper_scissors
rps = %w{ rock paper scissors }

player1 = rand(3)

player2 = rand(3)

# If they picks the same things it is a tie
case player1
     # If they choose the same thing it is a tie
     when player2 then puts "#{rps[player1]} and #{rps[player2]} is a 
tie."
     # If player1 is one higher than player2, player1 wins
     when player2 + 1 then puts "Player one wins #{rps[player1]} beats 
#{rps[player2]}."
     # If player one chose rock(0) and player2 chose sciscors(2) player1 
wins
     when player2 - 2 then puts "Player one wins #{rps[player1]} beats 
#{rps[player2]}."
     else
     #player2 wins
          puts "Player two wins #{rps[player2]} beats #{rps[player1]}."
end

end

20.times {rock_paper_scissors}

-- 
Posted via http://www.ruby-forum.com/.