Commits

Lars Yencken committed 6186112

Food seeking, collision detection and exploration.

  • Participants
  • Parent commits 9ed46df

Comments (0)

Files changed (1)

 #!/usr/bin/env python
-from ants import *
+import random
+
+from ants import Ants
 
 # define a class with a do_turn method
 # the Ants.run method will parse and update bot input
 # it will also run the do_turn method for us
-class MyBot:
+class MyBot(object):
     def __init__(self):
-        # define class level variables, will be remembered between turns
-        pass
+        self.seen = set()
     
     # do_setup is run once at the start of the game
     # after the bot has received the game settings
     # the ants class has the game state and is updated by the Ants.run method
     # it also has several helper methods to use
     def do_turn(self, ants):
-        # loop through all my ants and try to give them orders
-        # the ant_loc is an ant location tuple in (row, col) form
-        for ant_loc in ants.my_ants():
-            # try all directions in given order
-            directions = ('n','e','s','w')
+        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 True
+            else:
+                return False
+
+        targets = {}
+        def do_move_location(loc, dest):
+            directions = ants.direction(loc, dest)
             for direction in directions:
-                # the destination method will wrap around the map properly
-                # and give us a new (row, col) tuple
-                new_loc = ants.destination(ant_loc, direction)
-                # passable returns true if the location is land
-                if (ants.passable(new_loc)):
-                    # an order is the location of a current ant and a direction
-                    ants.issue_order((ant_loc, direction))
-                    # stop now, don't give 1 ant multiple orders
-                    break
-            # check if we still have time left to calculate more orders
-            if ants.time_remaining() < 10:
-                break
+                if do_move_direction(loc, direction):
+                    targets[dest] = loc
+                    return True
+            return False
+
+        ant_locs = set()
+        ant_dist = []
+        for food_loc in ants.food():
+            for ant_loc in ants.my_ants():
+                dist = ants.distance(ant_loc, food_loc)
+                ant_dist.append((dist, ant_loc, food_loc))
+                ant_locs.add(ant_loc)
+        self.seen.update(ant_locs)
+
+        ant_dist.sort()
+        for dist, ant_loc, food_loc in ant_dist:
+            if food_loc not in targets and ant_loc not in targets.values():
+                if do_move_location(ant_loc, food_loc):
+                    ant_locs.remove(ant_loc)
+
+        for ant_loc in ant_locs:
+            directions = ['n', 's', 'e', 'w']
+            random.shuffle(directions)
+            for d in directions:
+                new_loc = ants.destination(ant_loc, d)
+                if new_loc not in self.seen:
+                    if do_move_direction(ant_loc, d):
+                        break
+            else:
+                for d in directions:
+                    if do_move_direction(ant_loc, d):
+                        break
+
+        # check if we still have time left to calculate more orders
+        #if ants.time_remaining() < 10:
+            #break
             
 if __name__ == '__main__':
     # psyco will speed up python a little, but is not needed