Need help comparing Array data

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,

-------- Original-Nachricht --------

Datum: Sun, 18 Oct 2009 20:21:02 +0900
Von: Matt B. [email protected]
An: [email protected]
Betreff: Need help comparing Array data

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/.

Dear Matt,

you can use the Array#index method and comparisons of these values:

a=[“rock”,“paper”,“scissors”]
p a.index(“rock”)
p a.index(“paper”)
p a.index(“scissors”)
p a.index(“rock”)>a.index(“paper”)
p a.index(“paper”)>a.index(“rock”)
user_input1=“rock”
user_input2=“scissors”
p a.index(user_input1)>a.index(user_input2)

If the user enters something that isn’t in your Array a, you’ll get an
error:

user_input2=“ciseaux”
p a.index(user_input1)>a.index(user_input2)

You can catch that using a begin - rescue clause …

Best regards,

Axel

I am just confused: what happens in this game if
rock and scissors are compared ?
You might have to check that beforehand if
rock wins against scissors …

Best regards,

Axel

class RPS

Hierarchy = {
:rock => { :rock => :tie , :paper => :lose , :scissors =>
:win } ,
:paper => { :rock => :win , :paper => :tie , :scissors =>
:lose } ,
:scissors => { :rock => :lose , :paper => :win , :scissors =>
:tie } ,
}

Like what you did with the hashes by making each possible scenario its
own hash/array. I thought I had to do the comparison with operators <=>,
Still working through the rest of it.

On Sun, Oct 18, 2009 at 6:21 AM, Matt B.
[email protected]wrote:

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 = 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

Matt B. 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}

How about a separate object to represent each choice:

RPS::Rock

RPS::Paper

RPS::Scissors

and RPS::All is an array of all three.

class RPS
attr_accessor :beats
def initialize(name, beats=nil)
@name = name
@beats = beats
end
def beats?(other)
@beats == other
end
def to_s
@name
end

Rock = new(“rock”)
Paper = new(“paper”, Rock)
Scissors = new(“scissors”, Paper)
Rock.beats = Scissors
All = [Rock, Paper, Scissors]

def self.play
player1 = All[rand All.size]
player2 = All[rand All.size]

puts "Player 1 chose #{player1}"
puts "Player 2 chose #{player2}"
if player1.beats?(player2)
  puts "Player 1 wins!"
elsif player2.beats?(player1)
  puts "Player 2 wins!"
else
  puts "Tie!"
end
puts

end
end

20.times do
RPS.play
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs