-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
68 lines (51 loc) · 1.87 KB
/
play.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
from argparse import ArgumentParser
import sys
from itertools import count
import pygame
from src.agent import QAgent, Random_Agent, Human_Agent
from src.constants import *
from src.game import Game
def run(agent_type, model_path, verbose):
game = Game(randomize_start_pos=False)
if agent_type == "q":
my_agent = QAgent()
my_agent.load_model(model_path)
if agent_type == "random":
my_agent = Random_Agent(STATE_SIZE, ACTION_SIZE)
if agent_type == "human":
my_agent = Human_Agent()
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
state = game.reset()
move = HALT
for i in count():
# get move from keyboard
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if agent_type == "human":
move = move if my_agent.act(events) is None else my_agent.act(events)
else:
move = my_agent.act(state)
state, reward, done, _ = game.step(move)
if verbose:
print(state, reward, done, move)
if done:
break
screen.fill(BLACK)
screen.blit(game.player.image, game.player.pos)
screen.blit(game.minion.image, game.minion.pos)
pygame.display.update()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--agent", choices=["human", "q", "random"], default="human")
parser.add_argument("--agent_model_path", default=None)
parser.add_argument("--verbose", "-v", action="store_true")
args = parser.parse_args()
agent_type = args.agent
agent_model_path = args.agent_model_path
verbose = args.verbose
if agent_model_path is None and args == "q":
raise ValueError("Must specify the path to model if playing in agent mode")
run(agent_type, agent_model_path, verbose)