-
Notifications
You must be signed in to change notification settings - Fork 18
/
user_engine.py
138 lines (120 loc) · 3.52 KB
/
user_engine.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import curses
import numpy as np
import os
from engine import TetrisEngine
def play_game():
# Store play information
db = []
'''
states = []
rewards = []
done_flags = []
actions = []
'''
# Initial rendering
stdscr.addstr(str(env))
reward_sum = 0
done = False
# Global action
action = 6
while not done:
action = 6
key = stdscr.getch()
if key == -1: # No key pressed
action = 6
elif key == ord('a'): # Shift left
action = 0
elif key == ord('d'): # Shift right
action = 1
elif key == ord('w'): # Hard drop
action = 2
elif key == ord('s'): # Soft drop
action = 3
elif key == ord('q'): # Rotate left
action = 4
elif key == ord('e'): # Rotate right
action = 5
# Game step
state, reward, done = env.step(action)
reward_sum += reward
db.append(np.array((state,reward,done,action)))
'''
states.append(state)
rewards.append(reward)
done_flags.append(done)
actions.append(action)
'''
# Render
stdscr.clear()
stdscr.addstr(str(env))
stdscr.addstr('\ncumulative reward: ' + str(reward_sum))
stdscr.addstr('\nreward: ' + str(reward))
'''
db = {
'states' : np.array(states),
'rewards' : np.array(rewards),
'done_flags' : np.array(done_flags),
'actions' : np.array(actions),
}
'''
return db
def play_again():
print('Play Again? [y/n]')
print('> ', end='')
choice = input()
return True if choice.lower() == 'y' else False
def prompt_save_game():
#print('Accumulated reward: {0} | {1} moves'.format(sum(db['rewards']), db['actions'].shape[0]))
print('Accumulated reward: {0} | {1} moves'.format(sum([i[1] for i in db]), len(db)))
print('Would you like to store the game info as training data? [y/n]')
#stdscr.addstr('Would you like to store the game info as training data? [y/n]\n')
#stdscr.addstr('> ')
print('> ', end='')
choice = input()
return True if choice.lower() == 'y' else False
def save_game(path='training_data.npy'):
if os.path.exists(path):
x = np.load(path, allow_pickle=True)
x = np.concatenate((x,db))
np.save(path, x)
print('{0} data points in the training set'.format(len(x)))
else:
print('no training file exists. Creating one now...')
#fw = open('training_data.npy', 'wb')
print('Saving {0} moves...'.format(len(db)))
np.save(path, db)
def terminate():
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
os.system("stty sane")
def init():
# Don't display user input
curses.noecho()
# React to keys without pressing enter (700ms delay)
curses.halfdelay(7)
# Enumerate keys
stdscr.keypad(True)
#return stdscr
if __name__ == '__main__':
# Curses standard screen
stdscr = curses.initscr()
# Init environment
width, height = 10, 20 # standard tetris friends rules
env = TetrisEngine(width, height)
# Play games on repeat
while True:
init()
stdscr.clear()
env.clear()
db = play_game()
# Return to terminal
terminate()
# Should the game info be saved?
if prompt_save_game():
save_game()
# Prompt to play again
if not play_again():
print('Thanks for contributing!')
break