-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_ant.py
More file actions
113 lines (101 loc) · 3.47 KB
/
Copy pathtutorial_ant.py
File metadata and controls
113 lines (101 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
from ants import *
# 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:
def __init__(self):
# define class level variables, will be remembered between turns
pass
# do_setup is run once at the start of the game
# after the bot has received the game settings
# the ants class is created and setup by the Ants.run method
def do_setup(self, ants):
self.hills = []
self.unseen = []
for row in range(ants.rows):
for col in range(ants.cols):
self.unseen.append((row, col))
# do turn is run once per turn
# 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):
# 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 True
else:
return False
targets = {}
def do_move_location(loc, dest):
directions = ants.direction(loc, dest)
for direction in directions:
if do_move_direction(loc, direction):
targets[dest] = loc
return True
break
else:
return False
for hill_loc in ants.my_hills():
orders[hill_loc] = None
# find close food
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_dist.sort()
for dist, ant_loc, food_loc in ant_dist:
if food_loc not in targets and ant_loc not in targets.values():
do_move_location(ant_loc, food_loc)
# kill 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 ants.my_ants():
if ant_loc not in orders.values():
dist = ants.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 map
for loc in self.unseen[:]:
if ants.visible(loc):
self.unseen.remove(loc)
for ant_loc in ants.my_ants():
if ant_loc not in orders.values():
unseen_dist = []
for unseen_loc in self.unseen:
dist = ants.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):
break
# unblock our hills
for hill_loc in ants.my_hills():
if hill_loc in ants.my_ants() and hill_loc not in orders.values():
for direction in ('n','e','s','w'):
if do_move_direction(hill_loc, direction):
break
if __name__ == '__main__':
# psyco will speed up python a little, but is not needed
try:
import psyco
psyco.full()
except ImportError:
pass
try:
# if run is passed a class with a do_turn method, it will do the work
# this is not needed, in which case you will need to write your own
# parsing function and your own game state class
Ants.run(MyBot())
except KeyboardInterrupt:
print('ctrl-c, leaving ...')