-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_agent.py
52 lines (44 loc) · 1.94 KB
/
keyboard_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
# keyboard_agent.py
# -----------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero ([email protected]) and Dan Klein ([email protected]).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
from game import Agent, Action
class KeyboardAgent(Agent):
"""
An agent controlled by the keyboard.
"""
LEFT_KEY = 'a'
RIGHT_KEY = 'd'
UP_KEY = 'w'
DOWN_KEY = 's'
def __init__(self, tk_window):
super().__init__()
self.keys = []
tk_window.subscribe_to_keyboard_pressed(self.listener)
self.tk_window = tk_window
self._should_stop = False
def get_action(self, state):
self._should_stop = False
move = self._get_move(state)
while move is None and not self._should_stop:
self.tk_window.mainloop_iteration()
move = self._get_move(state)
if self._should_stop:
return Action.STOP
return move
def stop_running(self):
self._should_stop = True
def _get_move(self, state):
move = None
legal_actions = state.get_agent_legal_actions()
if Action.LEFT in legal_actions and (self.LEFT_KEY in self.keys or 'Left' in self.keys): move = Action.LEFT
if Action.RIGHT in legal_actions and (self.RIGHT_KEY in self.keys or 'Right' in self.keys): move = Action.RIGHT
if Action.UP in legal_actions and (self.UP_KEY in self.keys or 'Up' in self.keys): move = Action.UP
if Action.DOWN in legal_actions and (self.DOWN_KEY in self.keys or 'Down' in self.keys): move = Action.DOWN
self.keys = []
return move
def listener(self, tk_event=None, *args, **kw):
self.keys.append(tk_event.keysym)