def go(self): # Reduce timer if dead if self.dead > 0: self.dead -= 1 # If dead timer ended, respawn if self.dead == 0: self.start () return None head = self.gethead() head = board.progress(head, self.dir) self.body.append (head) if self.growable > 0 : self.growable -= 1 else : game.blocked.remove(self.body.pop(0)) self.useddir = self.dir return head
def aimove(self): if self.dead > 0: return start = self.gethead() end = game.digplace # If old path is ok, continue! last = None if self.path == [] or self.path[-1] != end or set(self.path) & game.blocked != set(): # Use BFS moves = self.getneighbors(start) parrent = {start:None} for move in list(moves): parrent[move] = start mark = set([start]) | moves while moves : next = set() for move in list(moves) : for point in self.getneighbors(move) : if point not in mark: next.add(point) mark.add(point) parrent[point] = move last = point moves = next # Find the path if end in mark: node = end elif last == None: return #Soon to be dead! elif board.progress(start, self.dir) in game.blocked: # Try to survie as long as possible node = last else: node = None if node != None: self.path = [] while parrent[node]: self.path.append(node) node = parrent[node] self.path.reverse() if len(self.path) > 0: self.dir = tuple(map(operator.sub, self.path.pop(0), start))
def getneighbors (self, point): return set([ (board.progress (point, dir)) for dir in board.dirs]) - game.blocked