Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
...
def distance_to_apple(self, pos):
return ((pos[0] - self.apple[0]) ** 2 + (pos[1] - self.apple[1]) ** 2) ** (1 / 2)
def get_reward(self):
distance = self.distance_to_apple(self.snake[0])
last_distance = self.distance_to_apple(self.snake[1])
if last_distance > distance:
return 0
return -1
...
# Change
if self.snake_in_apple(self.apple):
self.apple = self.random_apple_position()
if self.apple == -1:
won = True
else:
self.snake = self.snake[:-1]
# Into
if self.snake_in_apple(self.apple):
self.apple = self.random_apple_position()
if self.apple == -1:
won = True
reward = 10
else:
self.snake = self.snake[:-1]
reward = self.get_reward()
# And change
if done or self.snake[0][0] < 0 or self.snake[0][0] >= self.board_size or self.snake[0][1] < 0 or self.snake[0][1] >= self.board_size:
done = True
elif not won:
self.board = self.get_board()
return self.board, 0, done
# Into
if done or self.snake[0][0] < 0 or self.snake[0][0] >= self.board_size or self.snake[0][1] < 0 or self.snake[0][1] >= self.board_size:
done = True
reward = -50
elif not won:
self.board = self.get_board()
if won:
return self.board, 100, True
return self.board, reward, done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment