Last active
February 17, 2021 10:50
-
-
Save ikbendewilliam/fa60c8d0b90f41f33176aa2e1f822fe9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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