Skip to content

The bot heuristic

Every Arbitas game comes with a search bot that plays any rules file without being taught: it tries moves, plays the game on at random from each, and favors the moves that win most. That works well for short games. In longer ones, random play says little about a position, and the bot plays weakly. The heuristic section, new in DSL 1.1, gives it some of your game knowledge: how good a position is, and which moves are worth trying first.

The heuristic is never part of the rules. It doesn’t change what’s legal or who wins; a rules file without it plays exactly the same, and only the bots behave differently.

heuristic:
evaluate: "global.pits[store(s)] - global.pits[store(opponent(s))]"
scale: 4
prefer:
sow: "played(s, pit).last == store(s) ? 10 : played(s, pit).pits[store(s)] - global.pits[store(s)]"

That’s Kalah’s heuristic.

  • evaluate (required) scores an unfinished position for a seat, bound as s (and as self). It’s a whole number, and higher is better for s. Kalah’s is the lead in stones banked in the seat’s store.
  • scale is the lead in score that counts as a clear advantage: a lead of one scale is worth three quarters of a win. It defaults to 10. In Kalah, 4 stones is a clear lead.
  • prefer gives each action a weight, an expression with s and the move’s choices bound by their names. The bot tries the highest first. Kalah’s tries first a sowing that ends in the player’s own store, which earns another turn, then those that bank the most. played is one of Kalah’s own functions: the position after sowing a pit.

The bot doesn’t play each try out to the end. After a couple of moves it stops and scores the position with evaluate, which is much faster and, with a good heuristic, a better guide than random play. And when it looks at a position for the first time, it tries the moves prefer rates highest first.

The bot never sees what its seat can’t. It searches over samples of the hidden information that fit what it has seen, so evaluate scores one of those samples, never the real state, and a heuristic can’t make it cheat.

For tic-tac-toe, a position is good for a player when more lines are still open to them than to their opponent. A line is open to a player when the opponent has no mark on it. And the center square is part of four lines, so it’s worth trying first. Add this to the end of the tic-tac-toe rules:

heuristic:
# Lines still open to s, minus lines still open to the opponent: an int, higher is better for s.
evaluate: >
lines().count(l => l.all(sq => pieceAt(sq)?.owner != opponent(s)))
- lines().count(l => l.all(sq => pieceAt(sq)?.owner != s))
# A lead of two open lines counts as a clear advantage.
scale: 2
prefer:
# Try the center square first.
mark: "at == square(2, 2) ? 1 : 0"

at is the mark action’s choice step, so prefer can read the square a move would mark.

  • evaluate must give a whole number, and so must every preference.
  • Each key under prefer must name a declared action.
  • Nothing in the heuristic may use randomness: no die, random or shuffled.

You don’t have to write one yourself. On your game’s Bot page in the portal, choose Draft the bot, and optionally say in your own words what the bot should value. AI drafts a heuristic, for AI credits (the button shows the price). Arbitas checks the draft, then plays seeded matches between the bot guided by it and the bot without one, and proposes it to you only if the guided bot wins. Review the proposal shows the change to your rules, which you accept or reject.

  • Start simple. A score that tracks the game’s own scoring (points, material, stones banked) usually helps.
  • Check it helps. A heuristic is only worth keeping if the bot guided by it beats the bot without one. Play against it on the Playtest tab and see whether it plays better.
  • Keep it cheap. The bot scores thousands of positions a move, so a heuristic that walks every component in the game many times slows it down, and a slower bot looks at fewer moves.