-
Notifications
You must be signed in to change notification settings - Fork 2
/
snakegame.py
207 lines (177 loc) · 5.8 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
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
import numpy as np
from random import randint
from collections import deque
import copy
import warnings
FOOD = 10
SNAKE = 7
HEAD = 8
WALL = 1
GROUND =4
class SnakeEnv:
def __init__(self, x, y):
self.x = x
self.y = y
self.state = np.ones((x, y), dtype=int)*GROUND
self.reward_range = (0, 1)
self.score = 0
self.done = 0
self.screen = np.ones((x*4, y*4), dtype=int)*GROUND
self.timestep = 0
self.reset()
def project(self):
state = self.state
screen = self.screen
for i in range(10):
for j in range(10):
screen[i*4][j*4] = state[i][j]
screen[i*4+1][j*4] = state[i][j]
screen[i*4][j*4+1] = state[i][j]
screen[i*4+1][j*4+1] = state[i][j]
return screen
def food(self):
a = randint(0, self.x-1)
b = randint(0, self.y-1)
if self.state[a][b] == GROUND:
self.state[a][b] = FOOD
else:
self.food()
def reset(self):
x = self.x
y = self.y
self.timestep = 0
self.state = np.ones((x, y), dtype=int)*GROUND
small_snake = []
small_snake.append([randint(2, x - 3), randint(2, y - 3)])
choice = randint(1, 4)
if choice == 1:
small_snake.append([small_snake[0][0] + 1, small_snake[0][1]])
if choice == 2:
small_snake.append([small_snake[0][0], small_snake[0][1] + 1])
if choice == 3:
small_snake.append([small_snake[0][0] - 1, small_snake[0][1]])
if choice == 4:
small_snake.append([small_snake[0][0], small_snake[0][1] - 1])
choice = randint(0, 1)
self.snake = deque([small_snake[choice], small_snake[abs(choice - 1)]])
for i in self.snake:
self.state[i[0]][i[1]] = SNAKE
head = self.snake[0]
self.state[head[0]][head[1]] = HEAD
self.state[0] = WALL
self.state[self.x - 1] = WALL
for i in range(self.x):
self.state[i][0] = WALL
self.state[i][self.y - 1] = WALL
# self.food()
self.food()
return self.state
def render(self):
for i in self.snake:
self.state[i[0]][i[1]] = SNAKE
head = self.snake[0]
self.state[head[0]][head[1]] = HEAD
def step_absolute(self, direction):
head = self.snake[0]
last_pos = self.snake[1]
if direction == 0:
# screen up
return [head[0], head[1] + 1]
elif direction == 1:
# screen right
return [head[0] + 1, head[1]]
elif direction == 2:
# screen down
return [head[0], head[1] - 1]
elif direction == 3:
#screen left
return [head[0] - 1, head[1]]
def step(self, action):
#relative
state = self.state
snake = self.snake
head = snake[0]
self.done = 0
skore = len(snake)
next_move = [0,0]
neck_pos = snake[1]
self.timestep += 1
if action == 0:
# forward
if neck_pos == self.step_absolute(0):
next_move = self.step_absolute(2)
else:
next_move = self.step_absolute(0)
if action == 1:
# right
if neck_pos == self.step_absolute(1):
next_move = self.step_absolute(3)
else:
next_move = self.step_absolute(1)
if action == 2:
# down
if neck_pos == self.step_absolute(2):
next_move = self.step_absolute(0)
else:
next_move = self.step_absolute(2)
if action == 3:
# down
if neck_pos == self.step_absolute(3):
next_move = self.step_absolute(1)
else:
next_move = self.step_absolute(3)
'''
# Left
if action == 0:
if neck_pos == 0:
next_move = self.step_absolute(3)
elif neck_pos == 1:
next_move = self.step_absolute(0)
elif neck_pos == 2:
next_move = self.step_absolute(1)
else:
next_move = self.step_absolute(2)
# Up
if action == 1:
if neck_pos == 0:
next_move = self.step_absolute(0)
elif neck_pos == 1:
next_move = self.step_absolute(1)
elif neck_pos == 2:
next_move = self.step_absolute(2)
else:
next_move = self.step_absolute(3)
# Right
if action == 2:
if neck_pos == 0:
next_move = self.step_absolute(1)
elif neck_pos == 1:
next_move = self.step_absolute(2)
elif neck_pos == 2:
next_move = self.step_absolute(3)
else:
next_move = self.step_absolute(0)
'''
# collision with wall
if state[next_move[0]][next_move[1]] == WALL:
self.done = 1
# food
if state[next_move[0]][next_move[1]] == FOOD:
snake.appendleft([next_move[0], next_move[1]])
self.food()
# collision with self
if state[next_move[0]][next_move[1]] == SNAKE:
self.done = 1
# no obstruction
if state[next_move[0]][next_move[1]] == GROUND:
snake.appendleft([next_move[0], next_move[1]])
state[snake[-1][0]][snake[-1][1]] = GROUND
snake.pop()
self.render()
self.score = ((len(snake) - skore) * 1)
if self.done:
self.score = 0
return self.state, self.score, self.done
def getCurrentState(self):
state = copy.deepcopy(self.state)
return state