save direction to kill the snake use the correct kill direction in overwrite_eat_the_other_snake can end in a draw when both heads are by the food

This commit is contained in:
2024-04-17 12:22:54 +02:00
parent a57536b7cb
commit 034b0e361a
2 changed files with 8 additions and 41 deletions
+5 -39
View File
@@ -28,7 +28,7 @@ class BetterMasterSnake(TemplateSnake):
self.find_safe_positions()
if self.eat_the_snake_overwrite:
return self.overwrite_eat_the_other_snake(move, game_data["turn"])
return self.overwrite_eat_the_other_snake(game_data["turn"])
if self.game_type == "constrictor":
move = self.selected_move_constrictor()
@@ -38,16 +38,10 @@ class BetterMasterSnake(TemplateSnake):
self.add_to_history({"turn": game_data["turn"], "data": self.calculations})
return move if move else "up"
def overwrite_eat_the_other_snake(self, move:str, turn:int):
if len(self.safe_positions) > 1:
first_key = list(self.safe_positions.keys())[0]
self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions, "selected_move": self.safe_positions[first_key]})
self.add_to_history({"turn": turn, "data": self.calculations})
return self.safe_positions[first_key]
self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions})
def overwrite_eat_the_other_snake(self, turn:int):
self.add_calculations({"function": "eat_the_snake_overwrite", "my_head": self.my_head, "move": self.kill_the_snake, "safe_positions": self.safe_positions})
self.add_to_history({"turn": turn, "data": self.calculations})
return self.safe_positions
return self.kill_the_snake
#TODO: How to Fill the Gameboard best?
def selected_move_constrictor(self):
@@ -141,7 +135,7 @@ class BetterMasterSnake(TemplateSnake):
def would_eating_the_food_kill_the_snake(self, move:str):
return False
def ensure_escape_route(self, move):
def ensure_escape_route(self, move:str):
try:
future_position = self.safe_positions[move]
except KeyError:
@@ -160,36 +154,8 @@ class BetterMasterSnake(TemplateSnake):
#return move
# TODO: Fix - Snake Neat to find the best way - Close to the Tail and maybe fill most free cells as posible
#if self.will_end_in_dead_end(self.safe_positions[move], depth=20):
# return move
return move
def will_end_in_dead_end(self, start, depth=10):
start = (start['x'], start['y'])
return self.dfs_dead_end(self.board, start, depth, set([start]))
def dfs_dead_end(self, board, position, depth, path):
# Abbruchbedingung der Rekursion: Wenn Tiefe erreicht ist
if depth == 0:
return False # Nicht genügend Tiefe, um eine Sackgasse zu bestätigen
# Bewege nach oben
next_position = position
if next_position[0] < 0 or next_position in board['snakes'] or next_position in path:
return True # Sackgasse gefunden
# Füge aktuelle Position zum Pfad hinzu
path.add(next_position)
# Rekursive Überprüfung der nächsten Position
result = self.dfs_dead_end(board, next_position, depth - 1, path)
# Entferne aktuelle Position vom Pfad
path.remove(next_position)
return result
def is_position_safe(self, position):
return 0 <= position['x'] < self.board_width and 0 <= position['y'] < self.board_height and (position['x'], position['y']) not in self.my_body
def is_near_tail(self, position, tail):
return abs(position["x"] - tail[0]) + abs(position["y"] - tail[1]) <= 2
+3 -2
View File
@@ -122,10 +122,11 @@ class TemplateSnake:
for snake in self.other_snakes:
for direction, location in self.safe_positions.items():
if len(self.safe_positions) > 1:
#TODO: Avoid Draw when heads are by the food
if snake["length"] < self.my_snake["length"] and location in [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]:
self.eat_the_snake_overwrite = True
return direction
#TODO: Check if snake on the way to the bood here and only remove this pos
self.kill_the_snake = direction
#TODO: Check if snake on the way to the food here and only remove this pos
elif location in [{"x": v["x"], "y": v["y"]} for k, v in self.get_possible_moves(snake["head"]).items()]:
remove.append(direction)