X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=erdslangetjie%2Flevel.py;h=e4bc0e09a58072ea9c28dccdd2e4f8a790d9a6fb;hb=14a47bbc124c45098e35dbbc4cb09e55bebb8358;hp=8efc539e13a08d6b6ab7cafab4bd7fc1715f5197;hpb=4b54ba10f3412990f36ae5608acf892235b77335;p=erdslangetjie.git diff --git a/erdslangetjie/level.py b/erdslangetjie/level.py index 8efc539..e4bc0e0 100644 --- a/erdslangetjie/level.py +++ b/erdslangetjie/level.py @@ -73,9 +73,9 @@ class Level(object): exit_points = 0 for line in self._data: if ENTRY in line: - entry_points += 1 + entry_points += line.count(ENTRY) if EXIT in line: - exit_points += 1 + exit_points += line.count(EXIT) if entry_points == 0: raise RuntimeError('No entry point') if entry_points > 1: @@ -94,16 +94,18 @@ class Level(object): def set_tile_type(self, pos, new_type): self._data[pos[1]][pos[0]] = new_type - new_tile = self._get_tile_image(new_type, pos) + new_tile = self._get_tile_image(pos, new_type) self._tiles[pos[1]][pos[0]] = new_tile self._changed.append((pos, new_tile)) # Also update neighbourhood for wall types, etc. for new_pos in [(pos[0] - 1, pos[1]), (pos[0] + 1, pos[1]), - (pos[0], pos[1] - 1), (pos[0], pos[1] + 1)]: + (pos[0] - 1, pos[1] - 1), (pos[0] + 1, pos[1] + 1), + (pos[0], pos[1] - 1), (pos[0], pos[1] + 1), + (pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]: if not self._in_limits(new_pos): continue tile = self._data[new_pos[1]][new_pos[0]] - new_tile = self._get_tile_image(tile, pos) + new_tile = self._get_tile_image(new_pos, tile) self._tiles[new_pos[1]][new_pos[0]] = new_tile self._changed.append((new_pos, new_tile)) @@ -113,6 +115,9 @@ class Level(object): def at_exit(self, pos): return pos in self.exit_pos + def get_level_data(self): + return '\n'.join(reversed([''.join(x) for x in self._data])) + def _get_wall_tile(self, pos): # Is the neighbour in this direction also a wall? x, y = pos @@ -236,10 +241,16 @@ class LevelList(object): line = line.strip() if os.path.exists(filepath(line)): level_file = load(line) - self.levels.append(Level(level_file)) + level = Level(level_file) level_file.close() + try: + level.validate() + except RuntimeError as err: + raise RuntimeError( + 'Invalid level %s in level_list: %s' % (line, err)) + self.levels.append(level) else: - print 'Level list includes non-existant level %s' % line + raise RuntimeError('Level list includes non-existant level %s' % line) level_list.close() self._cur_level = 0