-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_game.py
105 lines (86 loc) · 3.46 KB
/
run_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gym_2048
import gym
import keyboard
import json
from datetime import datetime
from time import time
import os
import numpy as np
DATA_FOLDER = 'data'
ACTION_LIST = ['left', 'up', 'right', 'down']
def clear_console():
os.system('cls||clear')
def save_data(
start_time, player_name,
observations, times, key_presses, highest_number, rewards):
if player_name:
data = dict(
player_name=player_name,
times=times,
observations=observations,
key_presses=key_presses,
highest_number=highest_number,
rewards=rewards)
with open(os.path.join(DATA_FOLDER, f"{start_time}.json"), 'w') as f:
json.dump(data, f)
def run_game(topscore=0):
clear_console()
start_time = datetime.today().strftime('%Y-%m-%d-%H-%M-%S')
player_name = input(
"What is your name (or unique identifier)?\nLeave blank to not save data.\n")
env = gym.make('2048-v0')
highest_number = 2
times = []
observations = []
rewards = []
key_presses = []
done = False
clear_console()
score = 0
print(f"Current score: {score}; Top score: {topscore}")
obs = env.reset()
env.render()
display_time = time()
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
press_time = time()
times.append(press_time - display_time)
observations.append(obs)
x = keyboard.read_key()
if x in ACTION_LIST:
key_presses.append(x)
action = ACTION_LIST.index(x)
clear_console()
obs, reward, done, info = env.step(action)
rewards.append(reward)
score += reward
if reward > highest_number:
highest_number = reward
print(f"Current score: {score}; Top score: {topscore}; You pressed {x}")
env.render()
display_time = time()
elif x == 'x':
done = True
if done:
final_message = f"Game Over! Well played {player_name}!"\
if player_name else "Game Over!"
print(final_message)
break
observations = [obs.tolist() for obs in observations]
rewards = [int(rew) for rew in rewards]
print("Times taken:")
print(f"mean: {np.mean(times):.4f}, std: {np.std(times):.4f}")
return start_time, player_name, observations, times, key_presses, int(highest_number), rewards, score
if __name__ == '__main__':
start_time, player_name, observations, times, key_presses, highest_number, rewards, score = run_game()
save_data(start_time, player_name, observations, times, key_presses, highest_number, rewards)
while True:
print("\nEnter 'y' to play again.")
play_again_event = keyboard.read_event()
if play_again_event.event_type == keyboard.KEY_DOWN and keyboard.read_key() == 'y':
start_time, player_name, observations, times, key_presses, highest_number, rewards, score = run_game(score)
save_data(start_time, player_name, observations, times, key_presses, highest_number, rewards)
elif play_again_event.event_type == keyboard.KEY_DOWN:
print("Done. Thank you.")
break