Hey guys I'm pretty new to Ruby and programming in general. I'm reading the Ruby book "Beginning Ruby - From Novice To Professional" and I'm doing one of the included projects - building a basic text adventure.
I'm done with the text adventure but now I want to expand the features with some of my own methods but I'm running into problems.
What I want to do is to have a method that prints out in which directions (south, north, east, west), from any given room, there are exists to other rooms.
To do this I want to iterate through a hash with the each method and store every key (not value) in an array so that I then can see how many exits there are to the room where the player is at and print out a message of those exits. The keys are symbols.
So I've come this far:Code Ruby:def find_exits find_room_in_dungeon(@player.location).connections.each {|direction, location| } end
This partfinds the current room object where the player is at.Code Ruby:find_room_in_dungeon(@player.location)
This is the Room class:
Code Ruby:class Room #Creates the objects variables that store infromation about the Room. attr_accessor :reference, :name, :description, :connections #Sets the information that is set when the object is created to the object variables for storage. def initialize(reference, name, description, connections) @reference = reference @name = name @description = description @connections = connections end #Defines a method that returns the full description of the Room object. def full_description @name + "\n\nYou are in " + @description end end
@connections is a object variable that stores the hash that contains directions and locations like so for example: {:east => :large_room}
So I managed to store the directions of any given Room object with this line:but how do I get what's in direction into an array?Code Ruby:find_room_in_dungeon(@player.location).connections.each {|direction, location| }
I have no idea how to do this. Any help is appreciated.






Bookmarks