Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:20:44+00:00 2026-05-26T02:20:44+00:00

I’m working on a simple text-based dungeon game in Ruby, and I’ve run into

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:20:45+00:00Added an answer on May 26, 2026 at 2:20 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.