optimise game type constrictor to fill up most of the game board

This commit is contained in:
2024-04-17 11:15:06 +02:00
parent b9a0bca4c6
commit b364c6454e
+20 -7
View File
@@ -150,8 +150,10 @@ class BetterMasterSnake(TemplateSnake):
#TODO: How to Fill the Gameboard best?
def selected_move_constrictor(self):
move = self.ensure_escape_route(self.find_direction())
self.add_calculations({"function": "find_direction", "my_head": self.my_head, "move": move})
move = self.move_close_to_body()
self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move})
move = self.ensure_escape_route(move)
self.add_calculations({"function": "ensure_escape_route", "my_head": self.my_head, "move": move, "safe_positions": self.safe_positions})
return move
def selected_move_standard(self, move=None):
@@ -162,7 +164,7 @@ class BetterMasterSnake(TemplateSnake):
self.add_calculations({"function": "move_towards", "my_head": self.my_head, "path_to_food": path_to_food, "move": move})
if not move:
move = self.move_close_to_body()
move = self.move_close_to_body(move_close_to_tail=True)
self.add_calculations({"function": "move_close_to_body", "my_head": self.my_head, "move": move})
# Überprfe, ob der Zug einen Ausweg lässt
@@ -209,21 +211,32 @@ class BetterMasterSnake(TemplateSnake):
return best_direction if best_direction else "up"
def move_close_to_body(self):
def move_close_to_body(self, move_close_to_tail=False):
# Heuristik, um Positionen nahe dem eigenen Körper zu bevorzugen
body_positions = set((part['x'], part['y']) for part in self.my_body)
tail_position = (self.my_body[-1]['x'], self.my_body[-1]['y'])
best_move = None
best_score = float('inf')
max_distance = -1 # Initialize maximum distance
for direction, pos in self.safe_positions.items():
next_position = (pos['x'], pos['y'])
if next_position in self.safe_positions:
# Berechne die Distanz zum eigenen Körper
distance_to_body = min(abs(next_position[0] - part[0]) + abs(next_position[1] - part[1]) for part in body_positions)
if distance_to_body < best_score:
best_score = distance_to_body
# Berechne die Distanz zum eigenen Schwanz
distance_to_tail = abs(next_position[0] - tail_position[0]) + abs(next_position[1] - tail_position[1])
# Wähle die maximale Distanz (Körper oder Schwanz)
if move_close_to_tail:
distance = min(next_position, distance_to_tail)
else:
distance = max(next_position, distance_to_body)
# Update max_distance if a larger distance is found
if distance > max_distance:
max_distance = distance
best_move = direction
return best_move if best_move else "up" # Standardbewegung, falls keine bessere gefunden wird
def ensure_escape_route(self, move):
try:
future_position = self.safe_positions[move]