-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
40 lines (31 loc) · 1.04 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
import random
class Agent:
def get_action(self, available_actions):
pass
class RandomAgent(Agent):
'''
picks a valid move at action
'''
def get_action(self, available_actions):
random_action = random.choice(available_actions.keys())
return available_actions[random_action]
class BestScoreAgent(Agent):
'''
picks move that yields the highest possible score on that turn
'''
def get_action(self, available_actions):
best_move = sorted([(-score, move) for move, (board, score) in available_actions.iteritems()])[0][1]
return available_actions[best_move]
class Human(Agent):
'''
queries user to pick move
'''
def __init__(self):
self.move_d = {'l': 'left', 'r': 'right', 'u': 'up', 'd': 'down'}
def get_action(self, available_actions):
while True:
move = raw_input('Move: ')
if move in self.move_d:
move = self.move_d[move]
if move in available_actions:
return available_actions[move]