-
Notifications
You must be signed in to change notification settings - Fork 1
/
snek-game.py
278 lines (211 loc) · 8.12 KB
/
snek-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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import curses
import yaml
import math
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint
from itertools import cycle
from time import time
import sys
sys.path.append('../')
from core.network import load_network
board = {
'height': 20,
'width': 40
}
mooves = [
KEY_RIGHT,
KEY_RIGHT,
KEY_RIGHT,
KEY_RIGHT
]
moove = cycle(mooves)
def init_gui():
curses.initscr()
win = curses.newwin(board['height'], board['width'], 0, 0)
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)
return win
def violated_boundary_conditions(snake):
return (snake[0][0] == 0 or
snake[0][1] == 0 or
snake[0][0] == board['height'] - 1 or
snake[0][1] == board['width'] - 1 or
snake[0] in snake[1:])
def calculate_head(snake, key):
snake.insert(0, (
snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1),
snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)
))
def get_input(player, event, key):
dir = {
KEY_UP: {KEY_LEFT: -1, KEY_UP: 0, KEY_RIGHT: 1},
KEY_LEFT: {KEY_DOWN: -1, KEY_LEFT: 0,KEY_UP: 1},
KEY_RIGHT: {KEY_UP: -1, KEY_RIGHT: 0, KEY_DOWN: 1},
KEY_DOWN: {KEY_RIGHT: -1, KEY_DOWN: 0, KEY_LEFT: 1}
}
opposite = {
KEY_DOWN: KEY_UP,
KEY_UP: KEY_DOWN,
KEY_LEFT: KEY_RIGHT,
KEY_RIGHT: KEY_LEFT
}
if player:
if event != -1 and event != opposite[key]:
return event, dir[key][event]
else:
return key, dir[key][key]
else:
return next(moove)
def init_state(player, gui, snek):
if player:
delay = 100
elif gui and not player:
delay = 100
else:
delay = 0
return (
KEY_RIGHT, # init direction
0, # score
delay,
snek if snek is not None else [(10, 10), (10, 9), (10, 8), (10, 7), (10, 6)], # snek init
(randint(1,board['height']-1), randint(1,board['width']-1)), # food
)
def new_food(snake):
food = (randint(1, board['height'] - 2), randint(1, board['width'] - 2)) # Calculating next food's coordinates
return food if food not in snake else new_food(snake)
def eat_food(snake, food):
return snake[0] == food
def extract_features(snake, board, score, food, key, action):
directions = {
KEY_DOWN: {'front': (1, 0), 'left': (0, 1), 'right': (0, -1)},
KEY_UP: {'front': (-1, 0), 'left': (0, -1), 'right': (0, 1)},
KEY_LEFT: {'front': (0, -1), 'left': (1, 0), 'right': (-1, 0)},
KEY_RIGHT: {'front': (0, 1), 'left': (-1, 0), 'right': (1, 0)}
}
head = snake[0]
head_front = (head[0] + directions[key]['front'][0], head[1] + directions[key]['front'][1])
head_left = (head[0] + directions[key]['left'][0], head[1] + directions[key]['left'][1])
head_right = (head[0] + directions[key]['right'][0], head[1] + directions[key]['right'][1])
obstacle_front = int(violated_boundary_conditions([head_front] + snake[1:]))
obstacle_left = int(violated_boundary_conditions([head_left] + snake[1:]))
obstacle_right = int(violated_boundary_conditions([head_right] + snake[1:]))
food_angle = math.atan2(abs(head[0] - food[0]), abs(head[1] - food[1]))
#distance = food_distance(snake, food) / (math.sqrt(board['width'] ** 2 + board['height'] ** 2) / 2)
return obstacle_front, obstacle_left, obstacle_right, action, food_angle
def draw(win, snake, food, delay, score, last=None):
win.addch(food[0], food[1], '*')
if last is not None:
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '#')
win.border(0)
win.addstr(0, 2, 'Score : ' + str(score) + ' ') # Printing 'Score' and
win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings
win.timeout(delay) # Increases the speed of Snake as its length increases
def draw_features(win, features, board):
win.addstr(board['height']-1, 2, str({'f': features[0], 'l': features[1], 'r': features[2], 'a': features[3]}))
def random_action(prev_action, features):
action = randint(0, 2) - 1
dir = {
KEY_UP: {-1: KEY_LEFT, 0: KEY_UP, 1: KEY_RIGHT},
KEY_LEFT: {-1: KEY_DOWN, 0: KEY_LEFT, 1: KEY_UP},
KEY_RIGHT: {-1: KEY_UP, 0: KEY_RIGHT, 1: KEY_DOWN},
KEY_DOWN: {-1: KEY_RIGHT, 0: KEY_DOWN, 1: KEY_LEFT}
}
return dir[prev_action][action], action
def neural_action(prev_action, features):
dir = {
KEY_UP: {-1: KEY_LEFT, 0: KEY_UP, 1: KEY_RIGHT},
KEY_LEFT: {-1: KEY_DOWN, 0: KEY_LEFT, 1: KEY_UP},
KEY_RIGHT: {-1: KEY_UP, 0: KEY_RIGHT, 1: KEY_DOWN},
KEY_DOWN: {-1: KEY_RIGHT, 0: KEY_DOWN, 1: KEY_LEFT}
}
left = network.test(features[:-1] + (-1,))
front = network.test(features[:-1] + (0,))
right = network.test(features[:-1] + (1,))
if left == max(left, front, right):
return dir[prev_action][-1], -1
elif front == max(left, front, right):
return dir[prev_action][0], 0
else:
return dir[prev_action][1], 1
def neural_action2(prev_action, features):
dir = {
KEY_UP: {-1: KEY_LEFT, 0: KEY_UP, 1: KEY_RIGHT},
KEY_LEFT: {-1: KEY_DOWN, 0: KEY_LEFT, 1: KEY_UP},
KEY_RIGHT: {-1: KEY_UP, 0: KEY_RIGHT, 1: KEY_DOWN},
KEY_DOWN: {-1: KEY_RIGHT, 0: KEY_DOWN, 1: KEY_LEFT}
}
left = network.test(features[:-2] + (features[-1], -1))
front = network.test(features[:-2] + (features[-1], 0))
right = network.test(features[:-2] + (features[-1], 1))
if left == max(left, front, right):
return dir[prev_action][-1], -1
elif front == max(left, front, right):
return dir[prev_action][0], 0
else:
return dir[prev_action][1], 1
def food_distance(snek, food):
head = snek[0]
return math.sqrt((head[0] - food[0]) ** 2 + (head[1] - food[1]) ** 2)
def snek(player=False, gui=False, snek=None, action=random_action):
win = init_gui()
key, score, delay, snake, food = init_state(player, gui, snek)
win.addch(food[0], food[1], '*') # Prints the food
while key != 27: # While Esc key is not pressed
prevKey = key # Previous key pressed
prev_dist = food_distance(snake, food)
prev_score = score
features = extract_features(snake, board, score, food, key, key)
event = win.getch()
if player:
key, choosen_action = get_input(player, event, key)
else:
key, choosen_action = action(prevKey, features)
if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed
key = prevKey
features = extract_features(snake, board, score, food, key, choosen_action)
calculate_head(snake, key)
last = None
if eat_food(snake, food):
food = new_food(snake)
score += 1
else:
last = snake.pop() # [1] If it does not eat the food, length stays same
new_dist = food_distance(snake, food)
if violated_boundary_conditions(snake):
result = -1
elif violated_boundary_conditions(snake) and (new_dist < prev_dist or score > prev_score):
result = 0.5
elif not violated_boundary_conditions(snake) and (new_dist < prev_dist) or score > prev_score:
result = 1
else:
result = -0.5
features = extract_features(snake, board, score, food, key, choosen_action)
learning_data.append((*features, result))
prev_score = score
if violated_boundary_conditions(snake):
break
if gui:
draw(win, snake, food, delay, score, last)
draw_features(win, features, board)
curses.endwin()
return score
learning_data = []
def learn():
start_time = time()
while len(learning_data) < 1000:
score = snek(player=True, gui=True)
end_time = time()
print(end_time - start_time)
with open('snek-learning1.yaml', 'w') as file:
yaml.dump(learning_data, file)
#network = load_network('snek-survive.yaml')
network = load_network('snek-play-awesome.yaml')
def test():
for _ in range(5):
score = snek(player=False, gui=True, action=neural_action2)
#learn()
test()