-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmarterRandomStrat.py
59 lines (50 loc) · 2.08 KB
/
SmarterRandomStrat.py
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
from AntStrategy import AntStrategy
import random
class SmarterRandomStrat(AntStrategy):
'''If carrying food, limit directions chosen from to get closer to anthill.
If not, moves randomly and grabs first food.
'''
def __init__(self, max_x, max_y, anthill):
super().__init__(max_x, max_y, anthill)
self.inbox = []
self.outbox = []
self.direction = "WEST"
def receive_info(self, messages):
'''Receive messages sent by teammates in the last round.'''
self.inbox = messages
def send_info(self):
'''Send messages.'''
to_return = self.outbox
self.outbox = []
return to_return
def one_step(self, x, y, vision, food):
'''Calculate and return a randomly chosen, but valid, next move.'''
cardinals = { "NORTH": (1, 0),
"SOUTH": (1, 2),
"EAST": (2, 1),
"WEST": (0, 1),
"NORTHEAST": (2, 0),
"SOUTHEAST": (2, 2),
"SOUTHWEST": (0, 2),
"NORTHWEST": (0, 0),
"HERE": (1,1) }
self.outbox.append("message")
# DROP if carrying food near anthill, GET if find food while not carrying
for direct, coords in cardinals.items():
for agent in vision[coords[0]][coords[1]]:
if food and agent == self.anthill:
return "DROP " + direct
if not food and agent.isdigit():
return "GET " + direct
# Take ant towards hill if it's carrying food
if food:
if self.anthill == "@":
return random.choice(["NORTH", "NORTHEAST", "EAST", "WEST", "NORTHWEST"])
else:
return random.choice(["SOUTH", "SOUTHEAST", "EAST", "WEST", "SOUTHWEST"])
# Otherwise move randomly
while True:
move = random.choice(["NORTH", "NORTHEAST", "EAST", "SOUTHEAST", "SOUTH", "SOUTHWEST", "WEST", "NORTHWEST"])
coords = cardinals[move]
if "#" not in vision[coords[0]][coords[1]]:
return move