[Tutor] my newbie program

david din22@cox.net
Wed Nov 20 23:49:02 2002


could you please explain the shared object?
i am not clear on how Room is using one
of Maps methods. 
thanks,
david
 
> I think you need a shared map object
> though, that keeps track of how rooms
> are connected, rather than letting
> each room know where you get if you
> go in a certain direction.

> class Map:
>      ...
> 
> class Room:
>      map = Map() # Shared between instances.
> 
> I don't remember exactly what your code so far
> looks like, but I guess the interface to Room
> can look the same, but instead of looking in an
> internal list a room makes a call to:
>          self.map.hasOpening(self, direction).
> 
> In Room.__init__ you have something like
> self.map.placeRoom(self, x, y) so that the map
> can keep track of where each room is.
> 
> So, in class Map you will have something like:
> 
> class Map:
>      '''Keep track of rooms and connections'''
>      # DirectionSymbol: (deltaX, deltaY) Assumes 0,0 lower left corner
>      directions = {'n': (1, 0), 's': (-1, 0), 'w': (-1, 0), 'e': (1, 9)}
> 
>      def __init__(self):
>          self.grid = {}
>          self.connections = [] # or {}???
> 
>      def placeRoom(self, room, x, y):
>          if self.grid.has_key((x, y)):
>              raise KeyError, "Location taken!"
>          self.grid[(x, y)] = room
> 
>      def _getXY(self, theRoom):
>          '''Where is a room'''
>          for aKoord, aRoom in self.grid.items():
>              if aRoom == theRoom:
>                  return aKoord
>          # Didn't find it
>          raise KeyError, "Room not found"
> 
>      def _relativeCoord(self, room, direction):
>          thisX, thisY = self._getXY(room)
>          deltaX, deltaY = self.directions[direction]
>          return (thisX + deltaX, thisY + deltaY)
> 
>      def dig(self, room, direction):
>          # A lot of checks needs to be added in this method
>          newX, newY = self._relativeCoord(room, direction)
>          try:
>              otherRoom = self.grid[(newX, newY)]
>          except KeyError: #No room where we dug
>              otherRoom = Room(newX, newY)
>          self._makeConnection(room, otherRoom)
> 
>      def move(self, room, direction):
>          '''Return the room we get to when we move in a
>             certain direction'''
>          newX, newY = self._relativeCoord(room, direction)
>          try:
>              otherRoom = self.grid[(newX, newY)]
>              if self.hasConnection(room, otherRoom):
>                  return otherRoom
>              else: # Stay where we are
>                  reurn room
>          except: #Stay where we are
>              return room
>          # Another approach is to cast an exception if we can't move.
> 
>      def _makeConnection(self, room1, room2):
>          ...
> 
>      def _hasConnection(self, room1, room2):
>          ...
>          return xxx # 1 (yes) or 0 (no)
> 
> 
> I'm not sure about how to store connections (i.e. openings / doors).
> A connection could be stored like a pair of coordinates as above.
> "((thisX, thisY), (newX, newY))". But how do we see in a simple
> way that the above is equivalent with
> "((newX, newY), (thisX, thisY))"? Or isn't it? Can there be one
> way connections? Should we store both directions when we dig?
> Should we sort before we store?
> 
> 
> 
> -- 
> Magnus Lycka, Thinkware AB
> Alvans vag 99, SE-907 50 UMEA, SWEDEN
> phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
> http://www.thinkware.se/  mailto:magnus@thinkware.se
>