-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrozenLake.py
More file actions
53 lines (44 loc) · 1.45 KB
/
FrozenLake.py
File metadata and controls
53 lines (44 loc) · 1.45 KB
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
import gym
from gym.envs.registration import register
# Register FrozenLake with is_slippery False
register(
id='FrozenLake-v3',
entry_point='gym.envs.toy_text:FrozenLakeEnv',
kwargs={'map_name': '4x4', 'is_slippery': False}
)
# 방향 키 매핑
arrow_keys = {
'w': 0, # 위쪽 이동
'a': 1, # 왼쪽 이동
's': 2, # 아래쪽 이동
'd': 3 # 오른쪽 이동
}
def get_user_input():
"""사용자 입력을 받아 방향 키로 변환"""
while True:
key = input("Enter move (w/a/s/d): ").strip().lower()
if key in arrow_keys:
return key
print("Invalid input! Use w/a/s/d for moves.")
# FrozenLake 환경 초기화
env = gym.make('FrozenLake-v3', render_mode="human")
observation = env.reset()
env.render()
# 게임 루프
while True:
# 사용자 입력
key = get_user_input()
action = arrow_keys[key]
# Gym의 반환값 처리 (5개 값 처리)
observation, reward, terminated, truncated, info = env.step(action)
env.render()
# 상태 출력
print(f"State: {observation}, Reward: {reward}, Terminated: {terminated}, Truncated: {truncated}, Info: {info}")
# 에피소드 종료 조건
if terminated or truncated:
if reward > 0:
print("Congratulations! You reached the goal!")
else:
print("You fell into a hole! Better luck next time!")
break
env.close()