-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
190 lines (149 loc) · 4.34 KB
/
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
from random import randint
import numpy as np
class Engine:
"""
Snake game engine
Used for manageing game environment
"""
def __init__(self, board_size, random_start=False):
self.board_size = board_size
self.alive = None
self.direction = None
self.fruit = None
self.points = None
self.round = None
self.snake = None
self.random_start = random_start
self.food_reward = 1
self.death_penalty = 1
self.round_penalty = .05
self.reset()
def reset(self):
"""
Reset environment to initial state
"""
self.points = 0
self.round = 1
self._reset_snake()
self._reset_fruit()
def _reset_snake(self):
"""
Reset snake to initial state
"""
self.alive = True
self.snake = []
if self.random_start:
pos_x = randint(0, self.board_size - 1)
pos_y = randint(0, self.board_size - 1)
init_pos = [pos_x, pos_y]
else:
pos = self.board_size // 2
init_pos = [pos, pos]
self.snake.append(init_pos)
self.direction = randint(0, 3)
def _reset_fruit(self):
"""
Respawn fruit
"""
pos = self.snake[0]
while pos in self.snake:
pos = [randint(0, self.board_size - 1),
randint(0, self.board_size - 1)]
self.fruit = pos
def _is_pos_outside(self, pos):
"""
Check if give position is outside the board
"""
x, y = pos
is_outside = False
if x < 0 or x >= self.board_size:
is_outside = True
elif y < 0 or y >= self.board_size:
is_outside = True
return is_outside
def _has_died(self):
"""
Check if snake collided with wall or itself
"""
*body, head = self.snake
return head in body or self._is_pos_outside(head)
def _has_eaten(self):
"""
Check if snake found fruit
"""
head = self.snake[-1]
return head == self.fruit
def _move_pos(self, pos, direction):
"""
Get new position after move in given direction
"""
new_pos = pos.copy()
if direction == 0:
new_pos[1] += 1
elif direction == 1:
new_pos[0] += 1
elif direction == 2:
new_pos[1] -= 1
else:
new_pos[0] -= 1
return new_pos
def _move(self):
"""
Move snake
"""
head = self.snake[-1]
new_head = self._move_pos(head, self.direction)
self.snake.append(new_head)
def next_round(self, direction):
"""
Transfer game into next round
"""
self.round += 1
self.direction = direction
self._move()
food_reward = 0
death_penalty = 0
if self._has_eaten():
food_reward = self.food_reward
self._reset_fruit()
else:
self.snake.pop(0)
if self._has_died():
self.alive = False
death_penalty = self.death_penalty
reward = food_reward - death_penalty - self.round_penalty
self.points += 1 if food_reward != 0 else 0
terminal = not self.alive
return reward, terminal
def get_game_state(self):
"""
Get binary vector describing current game state
"""
if len(self.snake) > 1:
_, *body, head = self.snake
else:
*body, head = self.snake
# Direction
dir_state = np.zeros(4)
dir_state[self.direction] = 1
# Food
food_state = np.zeros(4)
fx, fy = self.fruit
hx, hy = head
if fx < hx:
food_state[0] = 1
elif fx > hx:
food_state[1] = 1
if fy < hy:
food_state[2] = 1
elif fy > hy:
food_state[3] = 1
# Danger
danger_state = np.zeros(3)
dirs = [-1, 0, 1]
for i, d in enumerate(dirs):
tmp_dir = (self.direction + d) % 4
tmp_head = self._move_pos(head, tmp_dir)
if tmp_head in body or self._is_pos_outside(tmp_head):
danger_state[i] = 1
return np.concatenate([dir_state, food_state, danger_state])