Repository navigation
Expand file tree
/
Copy pathgoodies.py
More file actions
120 lines (79 loc) · 3.7 KB
/
Copy pathgoodies.py
File metadata and controls
120 lines (79 loc) · 3.7 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
'''
goodies.py
Definitions for some example goodies
'''
import random
from maze import Goody, Baddy, Position, UP, DOWN, LEFT, RIGHT, STAY, STEP, PING
class StaticGoody(Goody):
''' A static goody - does not move from its initial position '''
def take_turn(self, _obstruction, _ping_response):
''' Stay where we are '''
return STAY
class RandomGoody(Goody):
''' A random-walking goody '''
def take_turn(self, obstruction, _ping_response):
''' Ignore any ping information, just choose a random direction to walk in, or ping '''
possibilities = [PING]
for direction in [UP, DOWN, LEFT, RIGHT]: # STEP.keys()
if not obstruction[direction]:
possibilities.append(direction)
return random.choice(possibilities)
class GreedyGoody(Goody):
''' A goddy that pings once and then walks towards the other goody. '''
last_ping_response = None
def vector_len_2(self, vector):
return (vector.x * vector.x) + (vector.y * vector.y)
def take_turn(self, obstruction, ping_response):
''' Ignore any ping information, just choose a random direction to walk in, or ping '''
if ping_response is not None:
self.last_ping_response = ping_response
if self.last_ping_response is None:
# If we don't know where the other goody is, then send a ping so that we can find out.
print("Pinging to find our friend and foe")
return PING
friend, = [player for player in self.last_ping_response.keys()
if isinstance(player, Goody) and player is not self]
foe, = [player for player in self.last_ping_response.keys() if isinstance(player, Baddy)]
# For the four possible moves, find the resulting distance to our friend after each one:
last_known_friend_position = self.last_ping_response[friend]
len_and_dirs = []
for direction in [UP, DOWN, LEFT, RIGHT]: # STEP.keys()
# Choose the one that takes us closest to the our friend
if not obstruction[direction]:
# STEP[direction] turns the direction label into a vector (dx, dy) which we can add
# to a Position (another vector):
new_vector = last_known_friend_position - STEP[direction]
entry = [direction, new_vector, self.vector_len_2(new_vector)]
len_and_dirs.append(entry)
len_and_dirs.sort(key=lambda len_and_dir: len_and_dir[2])
print(len_and_dirs)
return len_and_dirs[0][0]
class TopRightGoody(Goody):
''' A goddy that tries to the top right corner. '''
pos = None
def take_turn(self, obstruction, _ping_response):
''' Ignore any ping information, just choose a random direction to walk in, or ping '''
if self.pos == UP:
self.pos=DOWN
elif self.pos == DOWN:
self.pos=UP
elif self.pos == LEFT:
self.pos=RIGHT
elif self.pos == RIGHT:
self.pos=LEFT
possibilities = []
for direction in [UP, RIGHT]: # STEP.keys()
if not obstruction[direction] and direction != self.pos:
possibilities.append(direction)
if len(possibilities) != 0:
self.pos = random.choice(possibilities)
return self.pos
else:
for direction in [DOWN, LEFT]: # STEP.keys()
if not obstruction[direction] and direction != self.pos:
possibilities.append(direction)
if len(possibilities) !=0:
self.pos = random.choice(possibilities)
return self.pos
else:
return STAY