Looking through that, there are a few things that jump out:
1. You've got a bunch of rooms that you reference by symbols, and you
are storing them in an Array. You might want to look into changing
this to a Hash keyed by the reference. This would change
Dungeon#find_room from:
def find_room(reference)
@rooms.detect { |room| room.reference == reference }
end
to:
def find_room(reference)
@rooms[reference]
end
(Really, since find_room is only called within Dungeon, you can
probably do without the method in that case, and just replace
find_room(reference) calls with @rooms[reference])
2. You are doing your own file handling, which may be a good way to
learn Ruby's generic file handling facilities. On the other hand, you
may also want to try using the standard YAML library (discussed in
Chapter 9 of Begging Ruby: From Novice to Professional) to store your
rooms. It stores and loads arbitrary Ruby objects very simply in a
format that is easy to create/edit by hand, and if you keep expanding
what your program does (and what it loads and stores), it'll be easier
to expand than updating a custom format with each change.
3. The Dungeon#command method uses a series of independent if
statements, where only one of them can be true. There are several
improvements possible with this. The smallest change would be to use
an if [...] elsif [...] elsif [...] end idiom instead of if [...] end
if [...] end etc. The next would be to use a case statement, like
this:
case input
when "west"
...
when "east"
...
when "north"
...
when "south"
...
when "look"
...
when "inventory"
...
end
You could also consider, though this is more advanced, having a
@commands instance variable that is a Hash keyed by the name of
commands with values that are procs that are called for each command.
Then to process a command, you could just do @commands[command].call.
4. Your initialize method for Dungeon calls create and run at the end.
You might want think about to pulling those out of initialize, and
letting "create" take a parameter identifying the file name to use.
Then you would use Dungeon.new.create("file.txt").run to load and run
a dungeon. Only do this if you might ever want to create a blank
dungeon without loading structure from a file, or to load the
structure without running the dungeon.
···
On Feb 8, 2008 9:05 PM, Jonathon Hartoon <ezrickknight@gmail.com> wrote:
Hi. I have tried to learn other programming languages before and ruby
was the first one that really stuck. Anyways, I own both Prgramming
Ruby & Beginning Ruby. The later has a tutorial for making a text
dungeon. I expanded the example in the book to accept commands from a
user and load new maps from a txt file. I am now trying to build in an
inventory system, and smashing my face into the rocks. I can't even
come up with an acceptable way to do it. I also want my items to load
from a text file. My end goal is to have a text dungeon maker. Where
you have 2 or 3 text files (or even one I guess) that my engine will
create an entire dungeon from. Any help on an inventory system would be
great as I don't even know where to start.
The rooms.txt is pretty simple syntax (if thats what I would call it).
On one line you put a room.
reference~ Name #The player sees this~description~ #The next 8 places
seperated by "~" are connections, they are split up into pairs and
stored in a hash. The first part of the pair is input from the user and
the second is returned.
Working example:
cavecell~ Cave cell~a small cell in the side of a cave wall. The gate
on the north side is open.~north~cellblocka1~south~n~east~n~west~n~
cellblocka1~ Cell Block A~a long corridor full of jail cells. To the
south is an open cell. The corridor runs east and west, and seems to go
on for quite
sometime.~north~n~south~n~east~cellblocka2~west~cellblocka0~
cellblocka2~ Cell Block A~a long corridor full of jail cells. The
corridor runs east and west, and seems to go on for quite
sometime.~north~n~south~n~east~cellblocka0~west~cellblocka1~
cellblocka0~ Cell Block A~a long corridor full of jail cells. The
corridor runs east and west, and seems to go on for quite
sometime.~north~n~south~n~east~cellblocka1~west~cellblocka2~
This just loops back on itself. An "n" returned is impassable.
Attachments:
http://www.ruby-forum.com/attachment/1409/Dungeon.rb
--
Posted via http://www.ruby-forum.com/\.