在 Ruby 中从一个方法中结束另一种方法中的 while 循环?

发布于 2024-12-09 02:58:47 字数 1869 浏览 0 评论 0原文

我正在用 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方法调用它。谢谢!

I'm working on a simple text-based dungeon game in Ruby, and I've run into a snag. Basically, I have one room with a locked door. The key is found in another room and I want the door to be unlocked once the key has been found.

Here's what I've got so far:

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

Any input or advice? Essentially, I want to end the door_open = false loop once the key is found, but I can't figure out how to set door_open = true in the chest_room method then call it from the start method. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-12-16 02:58:47

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

写下你想做什么,并考虑如何将其建模为房间和钥匙,这样你就会走上更好的轨道。

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

What you really need is to back up and think out the design again. You have a Room, and it has a property "open" or "closed". You have a Key, and you need the key to change the state of that open/closed property.

Write out what you want to do, and think about how to model it as a Room and a Key, and you'll be on a better track.

(No, I don't want to give you the answer. Figuring it out is more important.)

指尖凝香 2024-12-16 02:58:47

您遇到了范围问题。通过在它们前面放置@来创建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

You're having scope issues. make the door_open or key_present instance variables by putting an @ in front of them. Then any method can access them. also case/when is cleaner than 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文