Question: Ruby Colossal Cave: https://en.wikipedia.org/wiki/Colossal_Cave_Adventure For this exercise complete the simple Dungeon game in the Inital Dungeon code. This is a fun way to learn about
Ruby
Colossal Cave: https://en.wikipedia.org/wiki/Colossal_Cave_Adventure
For this exercise complete the simple Dungeon game in the Inital Dungeon code. This is a fun way to learn about using classes and objects. Youll be learning about a new type of Ruby data structure called the Struct class(p. 139) its a simple was to create a data structure that is sometime preferable to using a class.
Youll notice that the Dungeon game is one large class with several nested classes within it: Player and Room. All of the action takes place inside of the Dungeon class.
There may be several points in putting the dungeon code together that you will have to re-factor the code. Be sure to follow instructions carefully.
Assemble the Dungeon from the code given the Inital Dungeon code into a file named dungeon.rb.
Add at least two more rooms to your dungeon.
Run your code and capture the output into a file named dungeon.txt.
Inital Dungeon code:
| class Dungeon | |
| attr_accessor :player | |
| def initialize(player_name) | |
| @player = Player.new(player_name) | |
| @rooms = [] | |
| end | |
| class Player | |
| attr_accessor :name, :location | |
| def initialize(player_name) | |
| @name = player_name | |
| end | |
| end | |
| class Room | |
| attr_accessor :reference, :name, :description, :connections | |
| def initialize(reference, name, description, connections) | |
| @reference = reference | |
| @name = name | |
| @description = description | |
| @connections = connections | |
| end | |
| end | |
| end
Each player can add rooms to the dungeon by using the add_room method. In Beginning Ruby you see these lines of code that show that rooms are added in pairs, with the rooms linked to each other to allow players to go back and forth. # Add rooms to the dungeon my_dungeon.add_room(:largecave, "Large Cave", "a large cavernous cave", { :west => :smallcave }) my_dungeon.add_room(:smallcave, "Small Cave", "a small, claustrophobic cave", { :east => :largecave }) The large room named :largecave is linked to a small room named :smallcave. The small room named :smallcave is linked to a large room named:largecave. Now lets add another room to the game that goes from :largecave to another large cave. Remember that each room must have its own name. In addition, we have to be careful not to overwrite an existing object. my_dungeon.add_room(:largecave, "Large Cave", "a large cavernous cave", { :west => :smallcave, :north =>:smallcave2 }) Since weve added :smallcave2 to the large caves connections, we have to create :smallcave2 and link it back to :largecave. # :largecave will now have two links my_dungeon.add_room(:largecave, "Large Cave", "a large cavernous cave", { :west => :smallcave, :north =>:smallcave2 }) my_dungeon.add_room(:largecave2, "Another large cave (largecave2), { :south => :largecave }) The rule is, if you add a room you have to add its complement!
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
