-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_play_game.py
71 lines (58 loc) · 1.73 KB
/
cmd_play_game.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
from __future__ import print_function
__author__ = 'frankhe'
import time
from Parameters import Test
import curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
stdscr.nodelay(1)
"""
Press s to start console, and press q to end the console. Press numbers to take actions
"""
game_name = Test.game_name
print_states = False
import gym
env = gym.make(game_name)
# env.monitor.start('MsPacman-exp')
env.reset()
game_episodes = [{'reward':0}]
while True:
if stdscr.getch() == ord('s'):
break
print("Playing ", game_name, end=' ')
print("action_space=", env.action_space, end=' ')
print("observation_space=", env.observation_space, end=' ')
action_dict = [ord(str(x)) for x in range(9)]
action = 0
while True:
env.render()
# action = env.action_space.sample()
c = stdscr.getch()
if c == ord('q'):
break
for action_i in range(len(action_dict)):
if action_dict[action_i] == c:
action = action_i
if c != curses.ERR:
print('action=', action, end='')
observation, reward, done, info = env.step(action) # take a action
time.sleep(0.1)
episode = len(game_episodes)-1
game_episodes[episode]['reward'] += reward
if done:
game_episodes.append({'reward':0})
print('game ', episode, ' is done! Reward=', game_episodes[episode]['reward'], end='')
env.reset()
if print_states:
print(_, end='')
print("action=", action, end='')
print("observation=", observation.shape, end='')
print("reward=", reward, end='')
print("done=", done, end='')
print("info=", info, end='')
curses.endwin()
for episode in game_episodes:
print('reward=', episode['reward'])
# env.monitor.close()