-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_no_visual.py
152 lines (116 loc) · 4.87 KB
/
snake_no_visual.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
import random
import numpy as np
import pickle
class LearnSnake:
def __init__(self):
self.screen_width = 600
self.screen_height = 400
self.snake_size = 10
self.snake_speed = 15
self.snake_coords = []
self.snake_length = 1
self.dir = "right"
self.board = np.zeros((self.screen_height // self.snake_size, self.screen_width // self.snake_size))
self.game_close = False
self.x1 = self.screen_width / 2
self.y1 = self.screen_height / 2
self.r1, self.c1 = self.coords_to_index(self.x1, self.y1)
self.board[self.r1][self.c1] = 1
self.c_change = 1
self.r_change = 0
self.food_r, self.food_c = self.generate_food()
self.board[self.food_r][self.food_c] = 2
self.survived = 0
self.step()
def get_state(self):
head_r, head_c = self.snake_coords[-1]
state = []
state.append(int(self.dir == "left"))
state.append(int(self.dir == "right"))
state.append(int(self.dir == "up"))
state.append(int(self.dir == "down"))
state.append(int(self.food_r < head_r))
state.append(int(self.food_r > head_r))
state.append(int(self.food_c < head_c))
state.append(int(self.food_c > head_c))
state.append(self.is_unsafe(head_r + 1, head_c))
state.append(self.is_unsafe(head_r - 1, head_c))
state.append(self.is_unsafe(head_r, head_c + 1))
state.append(self.is_unsafe(head_r, head_c - 1))
return tuple(state)
def is_unsafe(self, r, c):
if self.valid_index(r, c):
if self.board[r][c] == 1:
return 1
return 0
else:
return 1
def get_dist(self, r1, c1, r2, c2):
return ((r2 - r1) ** 2 + (c2 - c1) ** 2) ** 0.5
def valid_index(self, r, c):
return 0 <= r < len(self.board) and 0 <= c < len(self.board[0])
def coords_to_index(self, x, y):
r = int(y // 10)
c = int(x // 10)
return (r, c)
def generate_food(self):
food_c = int(round(random.randrange(0, self.screen_width - self.snake_size) / 10.0))
food_r = int(round(random.randrange(0, self.screen_height - self.snake_size) / 10.0))
if self.board[food_r][food_c] != 0:
food_r, food_c = self.generate_food()
return food_r, food_c
def game_over(self):
return self.game_close
def step(self, action="None"):
if action == "None":
action = random.choice(["left", "right", "up", "down"])
else:
action = ["left", "right", "up", "down"][action]
reward = 0
if action == "left" and (self.dir != "right" or self.snake_length == 1):
self.c_change = -1
self.r_change = 0
self.dir = "left"
elif action == "right" and (self.dir != "left" or self.snake_length == 1):
self.c_change = 1
self.r_change = 0
self.dir = "right"
elif action == "up" and (self.dir != "down" or self.snake_length == 1):
self.r_change = -1
self.c_change = 0
self.dir = "up"
elif action == "down" and (self.dir != "up" or self.snake_length == 1):
self.r_change = 1
self.c_change = 0
self.dir = "down"
if self.c1 >= self.screen_width // self.snake_size or self.c1 < 0 or self.r1 >= self.screen_height // self.snake_size or self.r1 < 0:
self.game_close = True
self.c1 += self.c_change
self.r1 += self.r_change
self.snake_coords.append((self.r1, self.c1))
if self.valid_index(self.r1, self.c1):
self.board[self.r1][self.c1] = 1
if len(self.snake_coords) > self.snake_length:
rd, cd = self.snake_coords[0]
del self.snake_coords[0]
if self.valid_index(rd, cd):
self.board[rd][cd] = 0
for r, c in self.snake_coords[:-1]:
if r == self.r1 and c == self.c1:
self.game_close = True
if self.c1 == self.food_c and self.r1 == self.food_r:
self.food_r, self.food_c = self.generate_food()
self.board[self.food_r][self.food_c] = 2
self.snake_length += 1
reward = 1 # food eaten, so +1 reward
else:
rh1, ch1 = self.snake_coords[-1]
if len(self.snake_coords) == 1:
rh2, ch2 = rh1, ch1
else:
rh2, ch2 = self.snake_coords[-1]
# death = -10 reward
if self.game_close:
reward = -10
self.survived += 1
return self.get_state(), reward, self.game_close