-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagents_human.py
58 lines (41 loc) · 1.7 KB
/
agents_human.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
from pacumen.mechanics.agent import Agent
from pacumen.mechanics.agent_direction import Direction
class HumanAgent(Agent):
"""
An agent controlled by a human using a keyboard.
"""
WEST_KEY = 'a'
EAST_KEY = 'd'
NORTH_KEY = 'w'
SOUTH_KEY = 's'
STOP_KEY = 'q'
def __init__(self, index=0):
self.index = index
self.keys = []
self.last_move = Direction.STOP
super().__init__(index)
def get_action(self, state):
from pacumen.displays.graphical_builder import keys_waiting
from pacumen.displays.graphical_builder import keys_pressed
keys = keys_waiting() + keys_pressed()
if keys:
self.keys = keys
legal_actions = state.get_legal_actions(self.index)
action = self.get_movement(legal_actions)
if action == Direction.STOP:
# Try to move in the same direction as the previous move.
if self.last_move in legal_actions:
action = self.last_move
self.last_move = action
return action
def get_movement(self, legal_actions):
movement = Direction.STOP
if (self.WEST_KEY in self.keys or 'Left' in self.keys) and Direction.WEST in legal_actions:
movement = Direction.WEST
if (self.EAST_KEY in self.keys or 'Right' in self.keys) and Direction.EAST in legal_actions:
movement = Direction.EAST
if (self.NORTH_KEY in self.keys or 'Up' in self.keys) and Direction.NORTH in legal_actions:
movement = Direction.NORTH
if (self.SOUTH_KEY in self.keys or 'Down' in self.keys) and Direction.SOUTH in legal_actions:
movement = Direction.SOUTH
return movement