Commits

Zach Kelling committed 53dd691

Create GameMap class, refactor to make functions methods of a map.

  • Participants
  • Parent commits 459cc2f

Comments (0)

Files changed (1)

 }
 NAME_TO_CHAR = dict(map(reversed, CHAR_TO_NAME.items()))
 
+class GameMap(list):
+    def update(self, coord, char_name):
+        """
+        Updates coordinates with new character.
+        """
+        x, y = coord
+        self[y][x] = NAME_TO_CHAR[char_name]
+
+    def get_char(self, coord):
+        """
+        Returns character at given coordinates.
+        """
+        x, y = coord
+        try:
+            return CHAR_TO_NAME[self[y][x]]
+        except IndexError:
+            return None
+
+    @property
+    def player_pos(self):
+        """
+        Returns player's current position on game map.
+        """
+        for y, row in enumerate(self):
+            for x, char in enumerate(row):
+                print char
+                if char == NAME_TO_CHAR['player']:
+                    return (x, y)
+        else:
+            raise Exception, "Error finding player position. Make sure there is an @ in your map!"
+
+
 def import_map(map_f):
     """
     Returns map as 2D-array.
     """
     try:
         with open(map_f) as f:
-            return [[x for x in y.strip()] for y in f if not y.startswith(';')]
+            return GameMap([x for x in y.strip()] for y in f if not y.startswith(';'))
     except:
         raise Exception, 'Could not import map'
 
             scr.addstr((y + 2), (x + 2), char)
     scr.refresh()
 
-def get_player_pos(game_map):
-    """
-    Returns player's current position on game map.
-    """
-    for y, row in enumerate(game_map):
-        for x, char in enumerate(row):
-            print char
-            if char == NAME_TO_CHAR['player']:
-                return (x, y)
-    else:
-        raise Exception, "Error finding player position. Make sure there is an @ in your map!"
-
-def get_next_pos(player_pos, key):
+def get_next_pos(coord, key):
     """
     Returns updated coordinates based on key press.
     """
         'a': left,
         's': down,
         'd': right,
-    }.get(key)(*player_pos)
-
-def get_event(next_pos, game_map):
-    """
-    Returns event type.
-    """
-    x, y = next_pos
-    try:
-        return CHAR_TO_NAME[game_map[y][x]]
-    except IndexError:
-        return None
-
-def update_map(game_map, coord, char):
-    """
-    Updates map
-    """
-    x, y = coord
-    game_map[y][x] = char
+    }.get(key)(*coord)
 
 def move_player(game_map, player_pos, prev_char, key):
     """
-    Moves player. Returns updated game map, new player position,
-    and prev character under under player pos.
+    Moves player. Returns new player position,
+    and character previously under new player pos.
     """
     next_pos = get_next_pos(player_pos, key)
-    event = get_event(next_pos, game_map)
+    event = game_map.get_char(next_pos)
 
     def push_boulder():
         boulder_pos = get_next_pos(next_pos, key)
-        replaced_char = get_event(boulder_pos, game_map)
+        replaced_char = game_map.get_char(boulder_pos)
         if replaced_char == 'hole':
-            update_map(game_map, boulder_pos, NAME_TO_CHAR['boulder_in_hole'])
+            game_map.update(boulder_pos, 'boulder_in_hole')
         elif replaced_char == 'space':
-            update_map(game_map, boulder_pos, NAME_TO_CHAR['boulder'])
+            game_map.update(boulder_pos, 'boulder')
         else:
             return do_nothing()
-        update_map(game_map, next_pos, NAME_TO_CHAR['player'])
-        update_map(game_map, player_pos, NAME_TO_CHAR['space'])
+        game_map.update(next_pos, 'player')
+        game_map.update(player_pos, 'space')
         return next_pos, 'space'
 
     def move():
-        update_map(game_map, next_pos, NAME_TO_CHAR['player'])
-        update_map(game_map, player_pos, NAME_TO_CHAR[prev_char])
+        game_map.update(next_pos, 'player')
+        game_map.update(player_pos, prev_char)
         return next_pos, event
 
     def do_nothing():
     return scr
 
 if __name__ == '__main__':
-    logging.basicConfig(filename='log.soku', filemode='w', level=logging.DEBUG)
+    logging.basicConfig(filename='log.soku', filemode='a', level=logging.DEBUG)
     log = logging.getLogger(__file__)
     log.info("Sokuban has started.")
 
     game_map = import_map('test_map')
-    player_pos = get_player_pos(game_map)
+    player_pos = game_map.player_pos
     prev_char = 'space'
     scr = init()
     draw_map(scr, game_map)