如何从数组中随机选择红宝石

时间:2018-07-17 17:09:15

标签: ruby

我对编程还很陌生,我正在尝试制作一个基本的剪刀石头布程序。

我已经使用1 =岩石,2 =纸张,3 =剪刀制作了该程序。但是现在我想尝试使用实际单词而不是数字来实现。我如何让红宝石从我的阵列中随机选择?

谢谢!

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

1 个答案:

答案 0 :(得分:0)

您可以使用sample

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

Documentation