-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomStrat.py
56 lines (49 loc) · 2.21 KB
/
RandomStrat.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
from AntStrategy import AntStrategy
import random
class RandomStrat(AntStrategy):
'''
Chooses a random, valid move.
Left to its own devices, it will *eventually* score.
'''
def __init__(self, max_x, max_y, anthill):
super().__init__(max_x, max_y, anthill)
self.direction = "WEST"
def receive_info(self, messages):
'''This ant doesn't use message passing.'''
pass
def send_info(self):
'''Send messages. Returns empty list since ant never sends messages.'''
return []
def one_step(self, x, y, vision, food):
'''Calculate and return a randomly chosen, but valid, next move.'''
# Mapping of cardinal words to (x, y) indices in vision
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) }
# Keep choosing a move until a valid one is chosen
while True:
move = random.choice(["NORTH", "NORTHEAST", "EAST", "SOUTHEAST", "SOUTH", "SOUTHWEST", "WEST", "NORTHWEST", "GET", "DROP"])
if move == "GET":
if not food:
direction = random.choice(["NORTH", "NORTHEAST", "EAST", "SOUTHEAST", "SOUTH", "SOUTHWEST", "WEST", "NORTHWEST", "HERE"])
coords = cardinals[direction]
for agent in vision[coords[0]][coords[1]]:
if agent.isdigit():
print(type(agent))
return move + " " + direction
elif move == "DROP":
if food:
direction = random.choice(["NORTH", "NORTHEAST", "EAST", "SOUTHEAST", "SOUTH", "SOUTHWEST", "WEST", "NORTHWEST", "HERE"])
coords = cardinals[direction]
if self.anthill in vision[coords[0]][coords[1]]:
return move + " " + direction
else:
coords = cardinals[move]
if "#" not in vision[coords[0]][coords[1]]:
return move