def startDigCorridor(self, start, dir): step = (coord.sum(start, dir), coord.sum(start, dir, dir)) if any([self._map.isOutBound(c) for c in step]): return if any([self._map.at(c).isStopDig for c in step]): return if any([c in self._diged for c in step]): return for c in step: self._diged.add(c) if self._map.at(c) == Tile.NULL: self._map.put(c, Tile.CORRIDOR) self.digAround(step[1])
def updateExitCanditate(self): for w in self._walls(): if len( [d for d in direction.CROSS if coord.sum(w, d) in self._space ]) != 1: continue self._exitCanditate.append(w)
def update(self): self.render() key = self._console.getKey() if key in self.KeyMap: self._cursor = coord.sum(self._cursor, self.KeyMap.get(key)) else: self.putTerrain(key)
def _walls(self): walls = set() for p in self._space: walls |= set([ w for w in [coord.sum(p, d) for d in direction.CROSS] if w not in self._space ]) return walls
def flood(self, start, flooded): for d in direction.CIRCLE: c = coord.sum(start, d) if self._isOutBound(c): continue if c in self._alives: continue if c in flooded: continue flooded.add(c) self.flood(c, flooded) return flooded
def toOctant(self, center, octant): dx, dy = coord.sub((self.x, self.y), center) if octant == 0: return coord.sum(center, (dx, dy)) if octant == 1: return coord.sum(center, (dy, dx)) if octant == 2: return coord.sum(center, (-dx, dy)) if octant == 3: return coord.sum(center, (-dy, dx)) if octant == 4: return coord.sum(center, (dx, -dy)) if octant == 5: return coord.sum(center, (dy, -dx)) if octant == 6: return coord.sum(center, (-dx, -dy)) if octant == 7: return coord.sum(center, (-dy, -dx)) raise ValueError("invalid octant")
def fillDeadEnd(self, point): if not self.isDeadEnd(point): return self._map.put(point, Tile.NULL) for d in direction.CROSS: step = coord.sum(point, d) if self._map.at(step).isWall: continue self.fillDeadEnd(step) break for de in self._deadends: self._map.put(de, Tile.DEADEND)
def moveHero(self, dir): nextCoord = coord.sum(self._hero, dir) if self._map.isWall(nextCoord): return self._hero = nextCoord
def isDeadEnd(self, point): wall = 0 for d in direction.CROSS: if self._map.at(coord.sum(point, d)).isWall: wall += 1 return wall == 3
def around(self): return [coord.sum(self._coord, d) for d in direction.CIRCLE]
def countLiveNeighbours(self, c): alives = 0 for d in direction.CIRCLE: if coord.sum(c, d) in self._alives: alives += 1 return alives
def move(self, point): self._space = set([coord.sum(point, s) for s in self._space]) return self
def update(self): key = self._console.getKey() coord.sum(self._coord, Walk.KeyMap.get(key, (0, 0))) self.render()
def moveTarget(self, dir): self._target = coord.sum(self._target, dir)
def testMultiSumAndSub(self): self.assertEqual(coord.sum((1, 2), (3, 4), (5, 6)), (9, 12)) self.assertEqual(coord.sub((5, 6), (3, 4), (1, 2)), (1, 0))
def testSumAndSub(self): self.assertEqual(coord.sum((1, 2), (3, 4)), (4, 6)) self.assertEqual(coord.sub((3, 4), (1, 2)), (2, 2))