forked from hunkim/ReinforcementZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_1_play_frozenlake_det.py
60 lines (49 loc) · 1.25 KB
/
01_1_play_frozenlake_det.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
import gym
from gym.envs.registration import register
import sys
import tty
import termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
inkey = _Getch()
# MACROS
LEFT = 0
DOWN = 1
RIGHT = 2
UP = 3
# Key mapping
arrow_keys = {
'\x1b[A': UP,
'\x1b[B': DOWN,
'\x1b[C': RIGHT,
'\x1b[D': LEFT}
# Register FrozenLake with is_slippery False
register(
id='FrozenLake-v3',
entry_point='gym.envs.toy_text:FrozenLakeEnv',
kwargs={'map_name': '4x4', 'is_slippery': False}
)
env = gym.make('FrozenLake-v3')
env.render() # Show the initial board
while True:
# Choose an action from keyboard
key = inkey()
if key not in arrow_keys.keys():
print("Game aborted!")
break
action = arrow_keys[key]
state, reward, done, info = env.step(action)
env.render() # Show the board after action
print("State: ", state, "Action: ", action,
"Reward: ", reward, "Info: ", info)
if done:
print("Finished with reward", reward)
break