Exemplo n.º 1
0
def pprintdg(grid, ants):
	hillrow, hillcol = ants.my_hills()[0]
	lbound = max(0, hillrow - 10)
	rbound = min(ants.rows - 1, hillrow + 10)
	bbound = max(0, hillcol - 10)
	tbound = min(ants.cols - 1, hillcol + 10)
	for rownum, row in enumerate(grid):
		if rownum < lbound or rownum > rbound:
			continue
		new_row = []
		for elnum, elem in enumerate(row):
			if elnum < bbound or elnum > tbound:
				continue
			new_elem = '%.3f' % (elem)
			new_row.append(new_elem)
		sys.stderr.write('df ' + ' '.join(new_row) + '\n')
Exemplo n.º 2
0
def pprintlm(ants):
	hillrow, hillcol = ants.my_hills()[0]
	lbound = max(0, hillrow - 10)
	rbound = min(ants.rows - 1, hillrow + 10)
	bbound = max(0, hillcol - 10)
	tbound = min(ants.cols - 1, hillcol + 10)
	for rownum, row in enumerate(xrange(ants.rows)):
		if rownum < lbound or rownum > rbound:
			continue
		new_row = []
		for colnum, col in enumerate(xrange(ants.cols)):
			if colnum < bbound or colnum > tbound:
				continue
			if not ants.passable((row, col)):
				new_elem = '  x  '
			elif (row, col) in ants.my_ants():
				new_elem = '  a  '
			else:
				new_elem = '  -  ' 
			new_row.append(new_elem)			
		sys.stderr.write('m  ' + ' '.join(new_row) + '\n')
Exemplo n.º 3
0
    def do_turn(self, ants):
        logging.info('MAP: \n' + ants.render_text_map())
        # track all moves, prevent collisions
        orders = {}
        def do_move_direction(loc, direction):
            new_loc = ants.destination(loc, direction)
            if (ants.unoccupied(new_loc) and new_loc not in orders):
                ants.issue_order((loc, direction))
                orders[new_loc] = loc
                return new_loc
            else:
                return False

        target_dests = {}
        def do_move_location(loc, dest, path=None):
            # if path given use it
            if path:
                direction = path.popleft()
                # ant who has an aim will be in new_loc next turn
                new_loc = do_move_direction(loc, direction)
                if new_loc and ants.passable(new_loc):
                    self.aims[new_loc] = dest, path
                    target_dests[dest] = loc
                    return True
            # or use direction from manhattan distance
            else:
                directions = ants.manhattan_direction(loc, dest)
                for direction in directions:
                    if do_move_direction(loc, direction):
                        target_dests[dest] = loc
                        return True
            return False

        # prevent stepping on own hill
        for hill_loc in ants.my_hills():
            orders[hill_loc] = None

        # ants who have an aim shall continue first
        for ant_loc in self.aims.keys():
            # be sure that ant is not dead nor assigned
            if (ant_loc not in ants.my_ants()) or (ant_loc in orders.values()):
                del(self.aims[ant_loc])
                continue
            dest, path = self.aims[ant_loc]
            # are we at the end of the road?
            if path:
                # got stuck somewhere? repath
                if not do_move_location(ant_loc, dest, path):
                    new_path = ants.bfs_shortest_path(ant_loc, dest)
                    if do_move_location(ant_loc, dest, new_path):
                        logging.info('REPATH: ' + str(ant_loc) + ' ' + str(dest) + ' ' + str(new_path))
                else:
                    logging.info('CONTINUE PATH: ' + str(ant_loc) + ' ' + str(dest) + ' ' + str(path))
            # in all cases delete current loc from aims
            del(self.aims[ant_loc])

        for food_loc in ants.food():
            ant_loc, path = ants.find_closest_ant(food_loc)
            if ant_loc and (food_loc not in target_dests) and (ant_loc not in target_dests.values()):
                path = ants.reverse_path(path)
                if do_move_location(ant_loc, food_loc, path):
                    logging.info('FEEDER (2): ' + str(ant_loc) + ' ' + str(food_loc) + ' ' + str(path))

        # attack hills
        for hill_loc, hill_owner in ants.enemy_hills():
            if hill_loc not in self.hills:
                self.hills.append(hill_loc)
        ant_dist = []
        for hill_loc in self.hills:
            for ant_loc in iter(ants.my_ants()):
                if ant_loc not in orders.values():
                    dist = ants.manhattan_distance(ant_loc, hill_loc)
                    ant_dist.append((dist, ant_loc))
        ant_dist.sort()
        for dist, ant_loc in ant_dist:
            do_move_location(ant_loc, hill_loc)

        # explore unseen areas
        for loc in self.unseen[:]:
            if ants.visible(loc):
                self.unseen.remove(loc)
        for ant_loc in iter(ants.my_ants()):
            if ant_loc not in orders.values():
                unseen_dist = []
                for unseen_loc in self.unseen:
                    dist = ants.manhattan_distance(ant_loc, unseen_loc)
                    unseen_dist.append((dist, unseen_loc))
                unseen_dist.sort()
                for dist, unseen_loc in unseen_dist:
                    if do_move_location(ant_loc, unseen_loc):
                        logging.info('SCOUT: ' + str(ant_loc) + ' ' + str(unseen_loc))
                        break

        # unblock own hill
        for hill_loc in ants.my_hills():
            if hill_loc in ants.my_ants() and hill_loc not in orders.values():
                for direction in ('s','e','w','n'):
                    if do_move_direction(hill_loc, direction):
                        break

        # track idle ants
        for ant in iter(ants.my_ants()):
            if ant not in orders.values():
                logging.info('IDLE: ' + str(ant))