【问题标题】:Ending while loops in one method from another method in Ruby?从Ruby中的另一种方法结束while循环?
【发布时间】:2011-10-10 00:46:33
【问题描述】:

我正在使用 Ruby 开发一个简单的基于文本的地下城游戏,但遇到了障碍。基本上,我有一个带锁门的房间。钥匙在另一个房间里,我希望在找到钥匙后打开门。

这是我目前所得到的:

def chest_room
  puts "You are in a small, round room."
  puts "On the floor in front of you is a small wooden chest."
  puts "It does not appear to be locked."
  puts "What do you do?"
  chest_open = false

  while true
  prompt; next_move = gets.chomp
  if next_move == "open chest"
    chest_open = true
    puts "Inside the chest, you see a small, brass key."
    puts "What do you do?"
    prompt; next_move = gets.chomp
  elsif next_move == "take key" and chest_open
    key_present = true      
  puts "You take the key and slip it into a pocket."
    puts "What do you do?"
    prompt; next_move = gets.chomp
  elsif next_move == "go back"
    start()
  else puts "I don't understand you."
    puts "What do you do?"
    prompt; next_move = gets.chomp
    end
  end
end  

def start
  puts "You find yourself in a dank room lit by torches."
  puts "There are three doors leading out of here."
puts "What do you do?"
door_open = false
key_present = false
while true
  prompt; next_move = gets.chomp
  if next_move == "door 1"
    chest_room()
  elsif next_move == "door 2"
    dais()
  elsif next_move == "door 3" and not door_open
    puts "This door is securely locked."
    puts "You'll need to find some way of opening it before you can enter."
    puts "What do you do?"
    prompt; next_move = gets.chomp
  elsif next_move == "door 3" and key_present
    door_open = true
    puts "The key you found fits easily into the lock."
    puts "With a click, you unlock the door!"
    orb_room()
  else 
    puts "I don't understand you."
    puts "What do you do?"
    prompt; next_move = gets.chomp
    end
  end
end

有什么意见或建议吗?本质上,我想在找到密钥后结束 door_open = false 循环,但我不知道如何在 chest_room 方法中设置 door_open = true 然后从 start 方法调用它。谢谢!

【问题讨论】:

    标签: ruby loops if-statement while-loop


    【解决方案1】:

    您真正需要的是备份并重新考虑设计。你有一个房间,它有一个“打开”或“关闭”的属性。您有一个密钥,并且您需要该密钥来更改该打开/关闭属性的状态。

    写下你想做什么,想想如何将它建模为一个房间和一个钥匙,你就会走上更好的轨道。

    (不,我不想给你答案。弄清楚更重要。)

    【讨论】:

    • +1 如果您不遵循此建议,您的代码可能会变得非常复杂,类似于 bit.ly/o37l86
    • 我一定要记住那个链接。
    • @CharlieMartin 感谢您的意见!我一直在跟随 Learn Ruby the Hard Way 来建立我的 Ruby 技能,并且可能对这个问题的思考过于短视了。我会根据你的建议重新考虑我在做什么,然后再试一次。
    【解决方案2】:

    您遇到了范围问题。通过在它们前面放置一个 @ 来创建 door_open 或 key_present 实例变量。然后任何方法都可以访问它们。 case/when 也比 if elsif 更干净

    while !@key_present # @key_present can be modified in another method
      prompt; next_move = gets.chomp
      case
        when "door 1" == next_move then chest_room() # put constants first when comparing ==
        when "door 2" == next_move then dais()
        # more conditions
      end
    end
    

    【讨论】:

    • 感谢您的回答!我更喜欢你的解决方案,而不是到目前为止我一直在做的,所以我会试试这个。
    猜你喜欢
    • 2019-03-15
    • 2015-07-06
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    相关资源
    最近更新 更多