# Need help comparing Array data

**URL:** <https://www.ruby-forum.com/t/need-help-comparing-array-data/177470>\
**Category:** Ruby\
**Created:** [October 18, 2009, 1:21pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470 "2009-10-18T13:21:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 18, 2009, 1:21pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/1 "2009-10-18T13:21:00Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 18, 2009, 1:40pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/2 "2009-10-18T13:40:36Z")

</div>

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

> Datum: Sun, 18 Oct 2009 20:21:02 +0900  
> Von: Matt B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> 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/](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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 18, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/3 "2009-10-18T13:47:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 18, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/4 "2009-10-18T22:12:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 18, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/5 "2009-10-18T21:54:39Z")

</div>

On Sun, Oct 18, 2009 at 6:21 AM, Matt B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](http://www.ruby-forum.com/).

How about something like this  
[http://codepad.org/Q9iW5lBw](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

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 19, 2009, 12:38am UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/6 "2009-10-19T00:38:44Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 19, 2009, 10:45am UTC](https://www.ruby-forum.com/t/need-help-comparing-array-data/177470/7 "2009-10-19T10:45:26Z")

</div>

# 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
