def recognize_stuck(self, loc):
        '''Recognize an ant which failed to move. Clear its plan.
        Return a 2-tuple of loc & plan.

        '''
        fail = [f for f in antmath.neighbors(loc) \
                if (self.wrap(f) in self.antplans and
                    self.antplans[f][0] == loc)]
        if fail:
            return fail[0], self.antplans.pop(fail[0])
        # unrecognized
        return None, 4 * (None,)
Exemple #2
0
def genmoves(logfn, game):
    moves = {}
    while True:
        env = yield moves
        moves = {}

        for aI, (aN, _) in env.myid.iteritems():
            neighbors = antmath.neighbors(aN)
            walls = set(n for n in neighbors if n in env.water)
            if walls:
                ways = list(set(neighbors) - walls)
                moves[aI] = antmath.loc_displacement(aN, random.choice(ways))

        moves = env.supplement(moves)
def genmoves(logfn, game):
    moves = {}
    arounds = {}
    while True:
        env = yield moves
        moves = {}

        if not arounds:
            for h in env.myhill:
                arounds[h] = set()
                for n in am.neighbors(h):
                    arounds[h] = arounds[h].union(am.eightsquare(n))
                arounds[h] = arounds[h].difference([h] + am.neighbors(h))

        for aI, (aN, _) in env.myid.iteritems():
            hills = env.digest('myhill', aN)
            if hills:
                d2, h = hills[0]
                if aN == h or aN in am.neighbors(h):
                    moves[aI] = choice('NESW')
                elif aN in arounds[h]:
                    moves[aI] = choice(am.naive_dir(h, aN))

        moves = env.supplement(moves)
 def tonari(loc):
     return am.neighbors(loc)