简单的摇滚,纸,剪刀游戏

时间:2015-03-01 00:08:45

标签: ruby if-statement control-structure

我写了一篇“摇滚,纸,剪刀”游戏:

puts "Hello, this is a rock, papers, scissors game. Let's play."
puts "Player 1, plase enter your choice: \n"
puts "r for rock. \np for paper. \ns for scissors."
p1 = gets.chomp.downcase

puts "Player 2, please enter your choice: \n"
puts "r for rock. \np for paper. \ns for scissors."
p2 = gets.chomp.downcase

if p1 == 'r' && p2 == 's'
  puts "Player 1 wins."
elsif p1 == 'r' && p2 == 'p'
  puts "Player 2 wins."
elsif p1 == 'r' && p2 == 'r'
  puts "Tie."
elsif p1 == 'p' && p2 == 'r'
  puts "Player 1 wins."
elsif p1 == 'p' && p2 == 's'
  puts "Player 2 wins."
elsif p1 == 'p' && p2 == 'p'
  puts "Tie."
elsif p1 == 's' && p2 == 'r'
  puts "Player 2 wins."
elsif p1 == 's' && p2 == 'p'
  puts "Player 1 wins."
elsif p1 == 's' && p2 == 's'
  puts "Tie."
end

然而,它有很多elsif s,我知道这可以用case...when语句来实现,但问题是我无法弄清楚如何。

我试图根据输入使用return语句:“返回0表示摇滚,1表示纸张,2表示剪刀”,然后使用条件来说出“嘿,如果玩家1返回1,玩家2也返回1,然后puts'平局'“,其他可能的结果也一样。

我试图将一个数字与结果相关联:{1}}当玩家1获胜时,return - 1获得平局,return 0获得玩家2获胜。

我是这样做的,但它有点相同,我觉得它太糟糕了:

return 2

我会感激任何帮助。

5 个答案:

答案 0 :(得分:1)

阵列可以用作戒指,每个项目都有 右侧较弱的物品,左侧较强的物品。

weapons = ['paper', 'rock', 'scissors']

用你最喜欢的方式选择武器

w1 = weapons[rand(weapons.length)]
w2 = weapons[rand(weapons.length)]

旋转数组,直到w1位于中心

while weapons[1] != w1
    weapons.rotate! 1
end

现在结果由武器阵列中的w2索引表示,方便。

verbs = ['is beat by', 'ties', 'beats']
puts "#{w1} #{verbs[weapons.index(w2)]} #{w2}"

几次运行的示例输出:

paper beats rock
paper ties paper
rock beats scissors
scissors beats paper
rock is beat by paper

你可以发挥创意并添加一个动词阵列的哈希值,每个武器一个,使用w1作为键,以便输出(例如) paper cover rock 等。

答案 1 :(得分:0)

您可以尝试这样的事情:

if (p1 == p2)
  puts "Tie"
elsif (p1 == 'r' && p2 == 'p') ||
      (p1 == 'p' && p2 == 's') ||
      (p1 == 's' && p2 == 'r')
  puts "P2 wins"
else
  puts "P1 wins"
end

答案 2 :(得分:0)

你也可以做相当于说

的红宝石
  If p1 = 'r' then

转到R.      万一       如果p1 =' p'然后 转到S.      ENDIF

等。

然后在你的转到位置

R:

如果p2 =' r'然后 put" tie"

等。

然而,p1 = p2然后......的想法很好。

答案 3 :(得分:0)

这是另一种方式:

WINNERS = {s: :p, p: :r, r: :s}
  # => {:s=>:p, :p=>:r, :r=>:s} 

def win_lose_or_tie?(me, you)
  return "I win! :-)"   if WINNERS[me] == you 
  return "you win! :-(" if WINNERS[you] == me
  "it's a tie :-|"
end

keys = WINNERS.keys
keys.product(keys).each { |me, you|
  puts "If I play #{me} and you play #{you}, then #{win_lose_or_tie?(me, you)}" }
  # If I play s and you play s, then it's a tie :-|
  # If I play s and you play p, then I win! :-)
  # If I play s and you play r, then you win! :-(
  # If I play p and you play s, then you win! :-(
  # If I play p and you play p, then it's a tie :-|
  # If I play p and you play r, then I win! :-)
  # If I play r and you play s, then I win! :-)
  # If I play r and you play p, then you win! :-(
  # If I play r and you play r, then it's a tie :-|

答案 4 :(得分:-1)

我使用case / when语句,如:

result = case [p1, p2]
         when %w[r r], %w[p p], %w[s s]
           0
         when %w[r p], %w[p s], %w[s r]
           1
         when %w[r s], %w[p r], %w[s p]
           -1
         end

puts case result
     when  0
      "Tie"
     when -1
      "P1 wins"
     when  1
      "P2 wins"
     end

但是,在写完后这更有意义:

puts case [p1, p2]
        when %w[r r], %w[p p], %w[s s]
          'Tie'
        when %w[r p], %w[p s], %w[s r]
          'P1 wins'
        when %w[r s], %w[p r], %w[s p]
          'P2 wins'
        end

这是一个测试:

[
  %w[r r], %w[p p], %w[s s],
  %w[r p], %w[p s], %w[s r],
  %w[r s], %w[p r], %w[s p]
  ].each do |p1, p2|

    puts case [p1, p2]
         when %w[r r], %w[p p], %w[s s]
           'Tie'
         when %w[r p], %w[p s], %w[s r]
           'P1 wins'
         when %w[r s], %w[p r], %w[s p]
           'P2 wins'
         end

  end
# >> Tie
# >> Tie
# >> Tie
# >> P1 wins
# >> P1 wins
# >> P1 wins
# >> P2 wins
# >> P2 wins
# >> P2 wins