开发者

Ending while loops in one method from another method in Ruby?

开发者 https://www.devze.com 2023-04-12 01:10 出处:网络
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

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开发者_如何学Python "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!


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.)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号