-3

Im pretty new to programming and I'm trying to make a basic rock paper scissors program.

I've already made this program using 1 = rock, 2 = paper, 3 = scissors. but now I want to try doing it with actual words instead of numbers. How can I make ruby choose randomly from my array, mine?

Thanks!

mine = ["Rock", "Paper", "Scissors"]
mine.sample(1 + rand(mine.count))
puts 'Write your choice, Rock, Paper, or Scissors'
yours = gets.chomp.to_s
if yours == "Rock"
    puts 'Your choice is Rock'


elsif yours == "Paper"
    puts 'Your choice is Paper'

else yours == "Scissors"
    puts 'Your choice is Scissors'

end
puts "--------------------------"
if mine == "Rock"
    puts 'Computer: My choice is Rock'


elsif mine == "Paper"
    puts 'Computer: My choice is Paper'

else mine == "Scissors"
    puts 'Computer: My choice is Scissors'

end


print 'Computer: My choice was: '
print mine
puts

if mine == "Rock" && yours == "Paper"
    puts "==========="
    puts "Paper Wins!"
    puts "==========="
elsif mine == "Paper" && yours == "Rock"
    puts "==========="
    puts "Paper Wins!"
    puts "==========="
elsif mine == "Paper" && yours == "Scissors"
    puts "=============="
    puts "Scissors Wins!"
    puts "=============="
elsif mine == "Scissors" && yours == "Paper"
    puts "=============="
    puts "Scissors Wins!"
    puts "=============="
elsif mine == "Scissors" && yours == "Rock"
    puts "=========="
    puts "Rock Wins!"
    puts "=========="
elsif  mine == "Rock" && yours == "Scissors"
    puts "=============="
    puts "Rock Wins!"
    puts "=============="
else
    puts "======================"
    puts "It's a tie! TRY AGAIN!"
    puts "======================"
end
  • Consider defining the hash `winners = { :rock=>:scissors, :scissors=>:paper, :paper=>:rock }`. Suppose players 1 and 2 one choose to play `c1` and `c2`, respectively. Then player 1 wins if `winners[c1] == c2`, player 2 wins if `winners[c2] == c1`; else it's a tie. – Cary Swoveland Jul 17 '18 at 18:32

1 Answers1

0

You can use sample:

[1, 2, 3].sample #=> 1
[1, 2, 3].sample(2) #=> [3, 1]

Documentation

coreyward
  • 77,547
  • 20
  • 137
  • 166