create better future planning into BestBattleSnake

This commit is contained in:
2026-04-04 12:00:02 +02:00
parent c6ebb5834b
commit bbdc8b288a
2 changed files with 443 additions and 39 deletions
+192
View File
@@ -829,5 +829,197 @@ class TestBestBattleSnake(unittest.TestCase):
move = make_board(game_state).snake_neat_make_a_move()
self.assertEqual(move, "right")
def test_enemy_attack_map_marks_own_tail_contest(self):
snake = BestBattleSnake()
my_snake = {
"id": "me",
"name": "me",
"health": 90,
"length": 4,
"head": {"x": 3, "y": 3},
"body": [
{"x": 3, "y": 3},
{"x": 3, "y": 2},
{"x": 2, "y": 2},
{"x": 2, "y": 3},
],
}
enemy = {
"id": "enemy",
"name": "enemy",
"health": 90,
"length": 6,
"head": {"x": 1, "y": 3},
"body": [
{"x": 1, "y": 3},
{"x": 1, "y": 2},
{"x": 1, "y": 1},
{"x": 0, "y": 1},
{"x": 0, "y": 2},
{"x": 0, "y": 3},
],
}
attack_map = snake._build_enemy_attack_map(
my_snake=my_snake,
other_snakes=[enemy],
food_set=set(),
is_constrictor=False,
width=7,
height=7,
enemy_can_grow_cache={"enemy": False},
)
self.assertEqual(attack_map.get((2, 3)), 6)
def test_simulation_frees_enemy_tail_even_if_enemy_can_grow(self):
snake = BestBattleSnake()
future_body = [
{"x": 4, "y": 4},
{"x": 4, "y": 3},
{"x": 3, "y": 3},
{"x": 3, "y": 4},
]
enemy = {
"id": "enemy",
"name": "enemy",
"health": 90,
"length": 4,
"head": {"x": 1, "y": 1},
"body": [
{"x": 1, "y": 1},
{"x": 1, "y": 0},
{"x": 0, "y": 0},
{"x": 0, "y": 1},
],
}
blocked = snake._simulation_blocked(
future_body=future_body,
other_snakes=[enemy],
food_set={(2, 1)},
is_constrictor=False,
enemy_can_grow_cache={"enemy": True},
)
self.assertNotIn((0, 1), blocked)
def test_enemy_attack_map_allows_enemy_tail_move_when_enemy_can_grow(self):
snake = BestBattleSnake()
my_snake = {
"id": "me",
"name": "me",
"health": 90,
"length": 4,
"head": {"x": 6, "y": 6},
"body": [
{"x": 6, "y": 6},
{"x": 6, "y": 5},
{"x": 5, "y": 5},
{"x": 5, "y": 6},
],
}
enemy = {
"id": "enemy",
"name": "enemy",
"health": 90,
"length": 4,
"head": {"x": 3, "y": 3},
"body": [
{"x": 3, "y": 3},
{"x": 3, "y": 2},
{"x": 2, "y": 2},
{"x": 2, "y": 3},
],
}
attack_map = snake._build_enemy_attack_map(
my_snake=my_snake,
other_snakes=[enemy],
food_set={(4, 3)},
is_constrictor=False,
width=11,
height=11,
enemy_can_grow_cache={"enemy": True},
)
self.assertEqual(attack_map.get((2, 3)), 4)
def test_future_planning_prefers_non_trap_path(self):
snake = BestBattleSnake()
my_body = [
{"x": 3, "y": 3},
{"x": 3, "y": 2},
{"x": 2, "y": 2},
{"x": 2, "y": 3},
]
enemy = {
"id": "enemy",
"name": "enemy",
"health": 90,
"length": 8,
"head": {"x": 0, "y": 0},
"body": [
{"x": 0, "y": 0},
{"x": 4, "y": 4},
{"x": 4, "y": 2},
{"x": 5, "y": 4},
{"x": 5, "y": 2},
{"x": 6, "y": 4},
{"x": 6, "y": 3},
{"x": 6, "y": 2},
],
}
safe_moves = snake._legal_moves(
my_head=my_body[0],
my_body=my_body,
other_snakes=[enemy],
food_set=set(),
is_constrictor=False,
width=7,
height=7,
)
self.assertIn("left", safe_moves)
self.assertIn("right", safe_moves)
right_bonus = snake._future_rollout_bonus_for_move(
move="right",
safe_moves=safe_moves,
my_body=my_body,
other_snakes=[enemy],
food_set=set(),
is_constrictor=False,
width=7,
height=7,
enemy_can_grow_cache={"enemy": False},
depth=3,
branch_limit=2,
deadline=None,
)
left_bonus = snake._future_rollout_bonus_for_move(
move="left",
safe_moves=safe_moves,
my_body=my_body,
other_snakes=[enemy],
food_set=set(),
is_constrictor=False,
width=7,
height=7,
enemy_can_grow_cache={"enemy": False},
depth=3,
branch_limit=2,
deadline=None,
)
self.assertGreater(left_bonus, right_bonus)
if __name__ == "__main__":
unittest.main()