On Fri, Sep 21, 2012 at 12:02 AM, Henry Maddocks <[email protected]> wrote:
>
> To start with I would haveā¦
>
> One Room class that takes a name and description
Ack.
> and a visited attribute.
Well, it depends what kind of attribute you are thinking of: if there
are more players then the attribute would at least have to be a
collection type. Conceptually visit is a relationship between room
and user - with potentially more properties (e.g. time of visit,
order).
> Not a class for each room as you have done.
Ack.
> Create a Map structure that defines the room layout. Something like
>
> [
> {:room => :kitchen, :next_rooms => {:north => :living_room,
> :south => :basement}},
> {:room => : living_room, :next_rooms => {:north => :bathroom, :south
> => :kitchen}},
> ...
> ]
I'd rather make the rooms reference their neighbors, maybe like this:
class Room
attr_accessor :name, :description
def neighbors
@neighbors || = {}
end
def connect(direction, other_room)
neighbors[direction] = other_room
other_room.neighbors[reverse_direction(direction)] = self
end
private
def reverse_direction(d)
case d
when :north then :south
when :south then :north
#...
else
raise ArgumentError, "Illegal direction: %p" % [d]
end
end
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en