Disclosure: I'm the developer of Bear Wars, the game this post is about.

Bear Wars: aiming against the computer

Bear Wars is a turn-based artillery game in the spirit of Worms and Hedgewars that runs in the browser. You can play it against the computer at three difficulty levels. The bot doesn't solve ballistics equations. It does something simpler: it tries shots on copies of the match and keeps the best one.

1. Deterministic core = free "what if"

The game simulation is deterministic: the same state and the same inputs always give the same result. So a copy of the match, stepped with the same inputs, ends exactly like the real match would. The bot plans a turn by forking the current state many times and asking "what happens if I fire like this?"

2. Make the copies cheap

A copy has to be fast to create. The terrain is a solidity mask stored in 64x64 tiles with copy-on-write, so a copy shares all pixels with the original and only duplicates the tiles an explosion actually carves.

For screening there is an even cheaper copy: a "ghost" terrain where explosions don't carve at all. The bot scans a coarse grid of aim angles, power and fuse times on ghost copies, keeps the best candidates, tries their neighbours, and checks only the finalists on exact copies.

3. Score the outcome, not the shot

Each trial is scored by what happened: damage to enemies, kills, bears drowned in the water, where the enemy ends up (in a pit, near an edge), minus any harm to its own team. Weapons other than the bazooka and grenade get their own candidate generators: walking up to an enemy and boring under it with the drill, flights for the helicopter, and so on.

4. The plan is a list of key presses

The result of planning is a plain list of inputs, one per tick: the same keys a human would press. The turn then plays out on screen like a player's turn, with a short "thinking" pause before the first key. Nothing special happens in the simulation because the opponent is a bot.

5. Budget in trials, not milliseconds

The search budget is counted in trial runs, not in time. With a time budget, a slow phone would play worse than a desktop and the same position could give different moves. With a trial budget, the same state and seed always give the same plan, which keeps replays and tests reproducible.

Planning runs in a Web Worker, so the main thread keeps rendering while the bot thinks.

6. Difficulty is a set of handicaps

The three levels use the same planner with different settings:

  • Easy ignores the wind, adds aim error (about 6 degrees) and power error, sometimes picks a careless move from the better half, uses fewer weapons, and gets about 900 trials.
  • Medium plans with half the wind, aims with about 2.5 degrees of error, also tries walking before the shot, and gets about 2,600 trials.
  • Hard uses the full wind, makes no execution error, tries more positions and weapons, and gets about 6,000 trials.

Tuning a level means changing numbers, not writing a new bot.

Bear Wars is free at bearwars.org. Try the hard bot and tell me in the comments how fast it beat you.