Need help comparing Array data

M

Matt Beckley

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,
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sun, 18 Oct 2009 20:21:02 +0900
Von: Matt Beckley <[email protected]>
An: (e-mail address removed)
Betreff: 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,

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
 
A

Axel Etzold

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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,
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
 
M

Matt Beckley

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

Greg Barozzi

Matt said:
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}
 
B

Brian Candler

# 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top