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 8573807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:20:04+00:00 2026-06-11T19:20:04+00:00

I am making a short, text-based game as an extra credit exercise based on

  • 0

I am making a short, text-based game as an extra credit exercise based on the Ruby I have learned so far and I’m having trouble getting classes to read and write variables between each other. I have read extensively and searched for clarification on how to do this but I haven’t had much luck. I have tried using @ instance variables and attr_accessible but I can’t figure it out. Here is my code so far:

class Game
  attr_accessor :room_count

  def initialize
    @room_count = 0
  end

  def play
    while true
      puts "\n--------------------------------------------------"

      if @room_count == 0
        go_to = Entrance.new()
        go_to.start
      elsif @room_count == 1
        go_to = FirstRoom.new()
        go_to.start
      elsif @room_count == 2
        go_to = SecondRoom.new()
        go_to.start
      elsif @room_count == 3
        go_to = ThirdRoom.new()
        go_to.start
      end
    end
  end

end

class Entrance

  def start
    puts "You are at the entrance."
    @room_count += 1
  end

end

class FirstRoom

  def start
    puts "You are at the first room."
    @room_count += 1
  end

end

class SecondRoom

  def start
    puts "You are at the second room."
    @room_count += 1
  end

end

class ThirdRoom

  def start
    puts "You are at the third room. You have reached the end of the game."
    Process.exit()
  end

end

game = Game.new()
game.play

I want to have the different Room classes change the @room_count variable so that Game class knows which room to go to next. I am also trying to do this without implementing class inheritance. 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-06-11T19:20:06+00:00Added an answer on June 11, 2026 at 7:20 pm
    class Room
      def initialize(game)
        @game = game
        @game.room_count += 1
      end
    
      def close
        @game.room_count -= 1
      end
    end
    
    class Game
      attr_accessor :room_count
    
      def initialize
        @room_count = 0
      end
    
      def new_room
        Room.new self
      end
    end
    
    game = Game.new
    game.room_count # => 0
    room = game.new_room
    game.room_count # => 1
    room.close
    game.room_count # => 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started making web pages and I'm having lots of trouble with
Kind of an odd question, but I have a game I am making for
So I am making a Text-Based RPG Applet in Java. I am using SWING
Long story short I'm making a greasemonkey script. The pages I work on have
Long story short, I'm making a custom Swing component that's basically a JTable with
I am making some sort of turn-based battle system in a JFrame where the
Making a word document of our network set-up. We have about 7 servers and
Let's say I have the following short-tag regex: \[BANNER{([0-9]*)}{(site1|site2)}{([A-Z]*)}\] basically, I have a CMS
I have a TextView that I am trying to make display a 2d character-based
i have a following feed from twitter and im making all links clickable and

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.