Skip to content

Commit acaeef2

Browse files
Stephen Gruppettaclaude
andcommitted
Iteration: Add vulture, turret, and hit_and_run bots
Three more strategies for people to learn from and beat: vulture hunts the weakest opponent, turret never moves and only attacks neighbours, and hit_and_run chases while healthy but flees at 1 HP. The demo seed moves to 2, the first seed where all seven shipped bots play to a winner. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 7430830 commit acaeef2

5 files changed

Lines changed: 122 additions & 3 deletions

File tree

‎bots/hit_and_run.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Fights while healthy, runs when hurt: chases at 2 HP or more, flees at 1 HP."""
2+
3+
from botbattle.view import ATTACKS, DIRECTIONS, Action, BotView
4+
5+
6+
def distance(x1: int, y1: int, x2: int, y2: int) -> int:
7+
return abs(x1 - x2) + abs(y1 - y2)
8+
9+
10+
def act(view: BotView) -> Action:
11+
if not view.others:
12+
return Action.WAIT
13+
14+
me = view.me
15+
nearest = min(view.others, key=lambda other: distance(me.x, me.y, other.x, other.y))
16+
17+
# Try every move that stays on the grid, and note how far it leaves us.
18+
moves = {}
19+
for direction, (dx, dy) in DIRECTIONS.items():
20+
new_x, new_y = me.x + dx, me.y + dy
21+
if 0 <= new_x < view.width and 0 <= new_y < view.height:
22+
moves[direction] = distance(new_x, new_y, nearest.x, nearest.y)
23+
24+
if me.hp == 1:
25+
# Hurt: take the move that gets farthest away.
26+
return max(moves, key=moves.get)
27+
28+
# Healthy: attack if the nearest opponent is next to us, else close in.
29+
for direction, step in DIRECTIONS.items():
30+
if step == (nearest.x - me.x, nearest.y - me.y):
31+
return ATTACKS[direction]
32+
return min(moves, key=moves.get)

‎bots/turret.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Never moves: attacks any opponent that comes next to it, and otherwise waits."""
2+
3+
from botbattle.view import ATTACKS, DIRECTIONS, Action, BotView
4+
5+
6+
def act(view: BotView) -> Action:
7+
me = view.me
8+
for direction, (dx, dy) in DIRECTIONS.items():
9+
for other in view.others:
10+
if (other.x, other.y) == (me.x + dx, me.y + dy):
11+
return ATTACKS[direction]
12+
return Action.WAIT

‎bots/vulture.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Picks on the weakest: hunts the opponent with the least HP and attacks it."""
2+
3+
from botbattle.view import ATTACKS, DIRECTIONS, Action, BotView
4+
5+
6+
def act(view: BotView) -> Action:
7+
if not view.others:
8+
return Action.WAIT
9+
10+
me = view.me
11+
# Weakest first. If two are equally weak, go for the closer one.
12+
target = min(
13+
view.others,
14+
key=lambda other: (other.hp, abs(other.x - me.x) + abs(other.y - me.y)),
15+
)
16+
dx = target.x - me.x
17+
dy = target.y - me.y
18+
19+
# If the target is right next to us, attack it.
20+
for direction, step in DIRECTIONS.items():
21+
if step == (dx, dy):
22+
return ATTACKS[direction]
23+
24+
# Otherwise close the bigger gap first.
25+
if abs(dx) >= abs(dy):
26+
return Action.RIGHT if dx > 0 else Action.LEFT
27+
return Action.DOWN if dy > 0 else Action.UP

‎tests/test_cli.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
from botbattle.cli import CLEAR_SCREEN, main
77

88
REAL_BOTS_FOLDER = Path(__file__).parent.parent / "bots"
9-
SHIPPED_BOTS = ["chaser.py", "coward.py", "example.py", "random_walker.py"]
10-
DEMO_SEED = "0" # The shipped bots play to a winner with this seed
9+
SHIPPED_BOTS = [
10+
"chaser.py",
11+
"coward.py",
12+
"example.py",
13+
"hit_and_run.py",
14+
"random_walker.py",
15+
"turret.py",
16+
"vulture.py",
17+
]
18+
DEMO_SEED = "2" # The shipped bots play to a winner with this seed
1119

1220

1321
@pytest.fixture

‎tests/test_loader.py‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def test_skips_file_starting_with_underscore(tmp_path):
5454
def test_real_bots_load_and_return_valid_actions():
5555
bots = load_bots(REAL_BOTS_FOLDER)
5656
names = [name for name, _ in bots]
57-
assert {"chaser", "coward", "example", "random_walker"} <= set(names)
57+
shipped = {"chaser", "coward", "example", "hit_and_run", "random_walker"}
58+
assert shipped | {"turret", "vulture"} <= set(names)
5859

5960
view = make_view((4, 4), (7, 2))
6061
for _, act in bots:
@@ -94,3 +95,42 @@ def test_coward_fights_back_when_cornered():
9495
neighbour = BotInfo("neighbour", 1, 0, hp=3)
9596
view = BotView(me=me, others=(neighbour,), width=2, height=1, round=1)
9697
assert coward(view) == Action.ATTACK_RIGHT
98+
99+
100+
def test_vulture_goes_for_the_weakest_opponent():
101+
vulture = load_real_bots()["vulture"]
102+
me = BotInfo("me", 4, 4, hp=3)
103+
strong_and_close = BotInfo("strong", 5, 4, hp=3)
104+
weak_and_far = BotInfo("weak", 4, 8, hp=1)
105+
view = BotView(
106+
me=me, others=(strong_and_close, weak_and_far), width=10, height=10, round=1
107+
)
108+
assert vulture(view) == Action.DOWN
109+
110+
111+
def test_vulture_attacks_the_weakest_when_it_is_next_to_it():
112+
vulture = load_real_bots()["vulture"]
113+
me = BotInfo("me", 4, 4, hp=3)
114+
weak = BotInfo("weak", 4, 3, hp=1)
115+
view = BotView(me=me, others=(weak,), width=10, height=10, round=1)
116+
assert vulture(view) == Action.ATTACK_UP
117+
118+
119+
def test_turret_never_moves():
120+
turret = load_real_bots()["turret"]
121+
assert turret(make_view((4, 4), (8, 8))) == Action.WAIT
122+
assert turret(make_view((4, 4), (4, 5))) == Action.ATTACK_DOWN
123+
124+
125+
def test_hit_and_run_chases_when_healthy():
126+
hit_and_run = load_real_bots()["hit_and_run"]
127+
assert hit_and_run(make_view((4, 4), (5, 4))) == Action.ATTACK_RIGHT
128+
assert hit_and_run(make_view((4, 4), (4, 8))) == Action.DOWN
129+
130+
131+
def test_hit_and_run_flees_when_hurt():
132+
hit_and_run = load_real_bots()["hit_and_run"]
133+
me = BotInfo("me", 4, 4, hp=1)
134+
attacker = BotInfo("attacker", 5, 4, hp=3)
135+
view = BotView(me=me, others=(attacker,), width=10, height=10, round=1)
136+
assert hit_and_run(view) in {Action.LEFT, Action.UP, Action.DOWN}

0 commit comments

Comments
 (0)