-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
144 lines (121 loc) · 3.9 KB
/
agent.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
from utils import Directions
class BaseAgent(object):
def __init__(self, height, width, initial_strength, name='base_agent'):
"""
Base class for a game agent
Parameters
----------
height: int
Height of the game map
width: int
Width of the game map
initial_strength: int
Initial strength of the agent
name: str
Name of the agent
"""
self.height = height
self.width = width
self.initial_strength = initial_strength
self.name = name
def step(self, location, strength, game_map, map_objects):
"""
Parameters
----------
location: tuple of int
Current location of the agent in the map
strength: int
Current strength of the agent
game_map: numpy.ndarray
Map of the game as observed by the agent so far
map_objects: dict
Objects discovered by the agent so far
Returns
-------
direction: Directions
Which direction to move
"""
pass
class RandomAgent(BaseAgent):
"""
A random agent that moves in each direction randomly
Parameters
----------
height: int
Height of the game map
width: int
Width of the game map
initial_strength: int
Initial strength of the agent
name: str
Name of the agent
"""
def __init__(self, height, width, initial_strength, name='random_agent'):
super().__init__(height=height, width=width,
initial_strength=initial_strength, name=name)
def step(self, location, strength, game_map, map_objects):
"""
Implementation of a random agent that at each step randomly moves in
one of the four directions
Parameters
----------
location: tuple of int
Current location of the agent in the map
strength: int
Current strength of the agent
game_map: numpy.ndarray
Map of the game as observed by the agent so far
map_objects: dict
Objects discovered by the agent so far
Returns
-------
direction: Directions
Which direction to move
"""
return np.random.choice(list(Directions))
class HumanAgent(BaseAgent):
"""
A human agent that that can be controlled by the user. At each time step
the agent will prompt for an input from the user.
Parameters
----------
height: int
Height of the game map
width: int
Width of the game map
initial_strength: int
Initial strength of the agent
name: str
Name of the agent
"""
def __init__(self, height, width, initial_strength, name='human_agent'):
super().__init__(height=height, width=width,
initial_strength=initial_strength, name=name)
def step(self, location, strength, game_map, map_objects):
"""
Implementation of an agent that at each step asks the user
what to do
Parameters
----------
location: tuple of int
Current location of the agent in the map
strength: int
Current strength of the agent
game_map: numpy.ndarray
Map of the game as observed by the agent so far
map_objects: dict
Objects discovered by the agent so far
Returns
-------
direction: Directions
Which direction to move
"""
dir_dict = {'N': Directions.NORTH,
'S': Directions.SOUTH,
'W': Directions.WEST,
'E': Directions.EAST}
dirchar = ''
while not dirchar in ['N', 'S', 'W', 'E']:
dirchar = input("Please enter a direction (N/S/E/W): ").upper()
return dir_dict[dirchar]