if got Key Error in move function create a new snake and calculate next best move

This commit is contained in:
2024-04-17 15:55:37 +02:00
parent 5ce12d70c1
commit 8c57e48f60
+5 -1
View File
@@ -96,7 +96,11 @@ class Server:
# move is called when your Battlesnake game is running game
def _move(self, game_state:dict) -> dict:
next_move = self.running_snake[game_state["game"]["id"]].choose_move(game_state)
try:
next_move = self.running_snake[game_state["game"]["id"]].choose_move(game_state)
except KeyError:
self.running_snake[game_state["game"]["id"]] = SnakeBuilder.build(self.snake_type)
next_move = self.running_snake[game_state["game"]["id"]].choose_move(game_state)
if self.store_game_state:
self.running_games[game_state["game"]["id"]].add_moves(game_state["turn"], game_state["board"], next_move)