[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner help - txt dungeon

Jonathon Hartoon

2/9/2008 5:05:00 AM

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-...attachment/1409/...

--
Posted via http://www.ruby-....

18 Answers

Huw Collingbourne

2/9/2008 2:47:00 PM

0

I'm not familiar with the example you are following. However, you may
find my adventure game tutorial useful...?

http://www.bitwisemag.com/2/Adventur...
http://www.bitwisemag.com/2/Adventur...-Part-2
http://www.bitwisemag.com/2/Adventur...-Part-3

best wishes
Huw

SapphireSteel Software
Ruby and Rails In Visual Studio
http://www.sapphir...
--
Posted via http://www.ruby-....

Jonathon Hartoon

2/9/2008 2:54:00 PM

0

Huw Collingbourne wrote:
> I'm not familiar with the example you are following. However, you may
> find my adventure game tutorial useful...?
>
> http://www.bitwisemag.com/2/Adventur...
> http://www.bitwisemag.com/2/Adventur...-Part-2
> http://www.bitwisemag.com/2/Adventur...-Part-3
>
> best wishes
> Huw
>
> SapphireSteel Software
> Ruby and Rails In Visual Studio
> http://www.sapphir...

Your tutorial looks interesting and easy to follow. I am going to work
my way through it over the next days. My program is attached to my
original post. Thanks for the help.
--
Posted via http://www.ruby-....

Jonathon Hartoon

2/12/2008 1:17:00 AM

0

I read your comments and some questions did in fact come up. You
commented on my file opening system. I agree with your reasoning, as it
is very hard to work with. The reason I made it that way is because it
was simple to code. I do not know how I would seperate it the way you
suggested.

The next comment was on my find_room method.
"You are using an Array as a Hash. The following line could just be:"
"@rooms[reference] # using a Hash"

I am confused by this. Maybe some more explaination. The next comments
are on my command system. I figured there had to be a much better way
to do this, but it was the first thing I had written without help or
examples. I however do not know what "case" means. A link to some
explanation might help. Your suggestions on the inventory definitely
helped keep my train of thought on the tracks that lead to resultopia.
--
Posted via http://www.ruby-....

James Gray

2/12/2008 3:51:00 AM

0

On Feb 11, 2008, at 7:17 PM, Jonathon Hartoon wrote:

> I read your comments and some questions did in fact come up. You
> commented on my file opening system. I agree with your reasoning,
> as it
> is very hard to work with. The reason I made it that way is because
> it
> was simple to code. I do not know how I would seperate it the way you
> suggested.

It is a bit harder. Save that for when you have learned a bit more.

> The next comment was on my find_room method.
> "You are using an Array as a Hash. The following line could just be:"
> "@rooms[reference] # using a Hash"
>
> I am confused by this. Maybe some more explaination.

>> class Room
>> # ...
?> end
=> nil
>> rooms = {:some_room => Room.new, :another_room => Room.new}
=> {:another_room=>#<Room:0x58c0dc>, :some_room=>#<Room:0x58c0f0>}
>> rooms[:some_room]
=> #<Room:0x58c0f0>

> I however do not know what "case" means. A link to some
> explanation might help.

>> def case_example(input)
>> case input
>> when "one"
>> 1
>> when "two"
>> 2
>> else
?> "unknown"
>> end
>> end
=> nil
>> case_example "one"
=> 1
>> case_example "two"
=> 2
>> case_example "three"
=> "unknown"

Hope that helps.

James Edward Gray II

Jonathon Hartoon

2/12/2008 2:21:00 PM

0


> >> def case_example(input)
> >> case input
> >> when "one"
> >> 1
> >> when "two"
> >> 2
> >> else
> ?> "unknown"
> >> end
> >> end
> => nil
> >> case_example "one"
> => 1
> >> case_example "two"
> => 2
> >> case_example "three"
> => "unknown"
>
> Hope that helps.
>
> James Edward Gray II

That is rather simple. Maybe I will leave the array being used as a
hash for another time. At the moments the funtionality remain intact.
Thx again for the help.
--
Posted via http://www.ruby-....

Thufir Hawat

2/15/2008 10:15:00 PM

0

On Sat, 09 Feb 2008 14:05:25 +0900, Jonathon Hartoon wrote:

> I am now trying to build in an
> inventory system, and smashing my face into the rocks.

Well, one approach would be to use a database. By using a db from the
get go you get the advantage of, well, organizing your data. Then, you
just connect to the db.

If you're using text files that's more fragile because one small change
will cause other things to break.

I kinda, sorta, had thoughts along your line for a game, but was using
_why's poignant ruby for inspiration :)

It's a bit rough (I don't do anything with the creatures once they're
created), but the db part, for me, looks like:

require 'rubygems'
require 'fileutils'
require 'active_record'
require 'creature'
require 'dragon'
require 'instantiate'
include Instantiate


system("rm dwemthys.db")

ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = true

ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => "dwemthys.db"
)

ActiveRecord::Schema.define do
create_table :creatures do |table|
table.column :type, :string
table.column :life, :integer
table.column :strength, :integer
table.column :charisma, :integer
table.column :weapon, :integer
end
end




99.times do |index|
creature = Instantiate.randomCreature
p creature
creature.save
end


http://dwemthys.googlecode.com/svn/trunk/db_crud...


HTH,

Thufir


Christopher Dicely

2/16/2008 5:23:00 PM

0

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-...attachment/1409/...
>
> --
> Posted via http://www.ruby-....
>
>

Thufir Hawat

2/17/2008 2:33:00 AM

0

On Feb 16, 9:22 am, Christopher Dicely <cmdic...@gmail.com> wrote:
> 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.
[...]

Actually, an array makes sense to me. Can you explain the advantages
of a Hash? With an array, the array can later be made to a Singleton
which could be iterated through.


-Thufir

Christopher Dicely

2/17/2008 6:03:00 AM

0

Since the main use is lookup by reference, and hash is designed for
keyed access, hash would seem to be the natural container to use. I'm
not sure I quite get what you are saying about iterating through a
singleton instead, could you explain a little more?

On Feb 16, 2008 6:35 PM, Thufir <hawat.thufir@gmail.com> wrote:
> On Feb 16, 9:22 am, Christopher Dicely <cmdic...@gmail.com> wrote:
> > 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.
> [...]
>
> Actually, an array makes sense to me. Can you explain the advantages
> of a Hash? With an array, the array can later be made to a Singleton
> which could be iterated through.
>
>
> -Thufir
>
>

Thufir Hawat

2/17/2008 11:48:00 PM

0

On Feb 16, 10:03=A0pm, "Christopher Dicely" <cmdic...@gmail.com> wrote:
> Since the main use is lookup by reference, and hash is designed for
> keyed access, hash would seem to be the natural container to use. I'm
> not sure I quite get what you are saying about iterating through a
> singleton instead, could you explain a little more?
[...]

There's one set of rooms, which may have sub-sets, of course. But,
for the set of rooms you may want to iterate through each room for
whatever reason. Putting the rooms into a Singleton allows such
iteration in an inherently thread-safe way.

Admittedly, I don't really understand Hash that well.


-Thufir