-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGame.py
136 lines (116 loc) · 4.1 KB
/
SnakeGame.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
import numpy as np
import matplotlib.pyplot as plt
class SnakeGame:
def __init__(self, size=6, test=False):
self.test = test
self.grid = self.make_grid(size)
self.grid_size = size
self.head_val = 3
self.move_snake_body = self.v_move_snake_body()
self.plot_snake = self.v_plot_snake()
self.dead = False
if not self.test:
self.spawn_reward()
def plot_grid(self):
if not self.test:
plt.matshow(self.plot_snake(self.grid, self.head_val))
plt.show()
def reset_snake(self):
self.grid = self.make_grid(self.grid_size)
self. head_val = 3
self.dead = False
self.spawn_reward()
@staticmethod
def v_plot_snake():
def plot_snake(pixel, head_val):
if pixel > 0 and not pixel == head_val:
return 1
elif pixel == head_val:
return 2
else:
return pixel
v_plot_snake = np.vectorize(plot_snake)
return v_plot_snake
@staticmethod
def v_move_snake_body():
def move_snake_body(segment):
if segment > 0:
return segment - 1
else:
return segment
v_move_snake_body = np.vectorize(move_snake_body)
return v_move_snake_body
def move_snake(self, direction):
if self.dead:
print(f'You died, your score was {self.head_val - 3}')
return
grid = self.grid
head_val = self.head_val
head_loc = self.find_head_loc(grid, head_val)
new_head_loc = self.new_end_loc(head_loc, direction)
self.dead = self.fatal_move(grid, new_head_loc)
if not self.dead:
self.grid = self.update_grid(grid, new_head_loc)
#self.plot_grid()
else:
#self.plot_grid()
print(f'You died, your score was {self.head_val - 3}')
return
def update_grid(self, grid, head_loc):
# reward?
if grid[head_loc[0], head_loc[1]] == -2:
self.head_val += 1
self.spawn_reward()
else:
grid = self.move_snake_body(grid)
grid[head_loc[0], head_loc[1]] = self.head_val
return grid
def spawn_reward(self):
reward_spawned = False
while not reward_spawned:
spawn_loc = self.random_index(self.grid_size)
if self.grid[spawn_loc[0], spawn_loc[1]] == 0 and not self.grid[spawn_loc[0], spawn_loc[1]] == -2:
self.grid[spawn_loc[0], spawn_loc[1]] = -2
reward_spawned = True
@staticmethod
def random_index(grid_size):
return [np.random.randint(1, grid_size - 1), np.random.randint(1, grid_size - 1)]
@staticmethod
def fatal_move(grid, new_head_loc):
return grid[new_head_loc[0], new_head_loc[1]] not in [0, -2]
@staticmethod
def new_end_loc(end_loc, direction):
if direction == 'up':
return end_loc + np.array([-1, 0])
elif direction == 'down':
return end_loc + np.array([1, 0])
elif direction == 'left':
return end_loc + np.array([0, -1])
elif direction == 'right':
return end_loc + np.array([0, 1])
else:
raise ValueError('Invalid direction')
@staticmethod
def find_head_loc(grid, head_val):
# find head
return np.array(np.where(grid == head_val)).flatten()
@staticmethod
def make_grid(size):
"""
Makes a size x size array that represents the initial snake grid.
"""
assert size > 4
assert type(size) == int
# initialise all to empty space
game_board = np.reshape(np.zeros(size * size), (size, size))
# fill in border
game_board[0] = np.full(size, -1)
game_board[size - 1] = np.full(size, -1)
for row_index in range(1, size - 1):
game_board[row_index][0] = -1
game_board[row_index][size - 1] = -1
# add snake
game_board[1][1] = 1
game_board[1][2] = 2
game_board[1][3] = 3
return game_board