Game Strategy7 min read

How Our Bots Think: Minimax, Pattern Spotting and Fair Opponents

Why the hard Tic Tac Toe bot can never lose, how the Connect Four bot looks ahead, and why the Rock Paper Scissors bot watches your habits.

Doozles TeamThe people who build and play Doozles ยท

Every board game on Doozles now has a vs Bot mode. Building a bot that is fun is a different problem from building a bot that is strong. A perfect opponent is boring for most people, and a random one is pointless. Here is how each of our bots works, and how the difficulty levels change it.

Tic Tac Toe: the game a computer can solve completely

Tic Tac Toe has fewer than 6,000 distinct positions, so a computer can look at every possible future of the game from any position. Our hard bot uses an algorithm called minimax:

  1. For each empty square, imagine playing there.
  2. Then imagine the opponent playing their best reply, then the bot's best reply to that, and so on until the game ends.
  3. Score each ending: a win is good, a loss is bad and a draw is neutral.
  4. Pick the move whose worst case is the best.

Because it explores everything, the hard bot never loses. The best you can do is draw. The medium bot wins when it can and blocks your immediate threats, but does not plan forks. The easy bot plays a random square most of the time, which leaves plenty of chances to practise your own forks.

Connect Four: looking ahead without looking at everything

Connect Four has around 4.5 trillion positions, far too many to search completely in a browser. So the Connect Four bot uses minimax with two upgrades:

  • A depth limit. Easy looks 2 moves ahead, medium 4 and hard 6. When it reaches the limit, it estimates how good the position is instead of playing it out.
  • Alpha-beta pruning. If one line of play is already clearly worse than an option found earlier, the bot stops exploring it. This lets it search much deeper in the same time.

The position estimate rewards discs in the centre column and open lines of two and three, and penalises the same patterns for the opponent. That is why the bot loves the middle column, just like strong human players.

Ludo: simple rules of thumb

Ludo involves dice, so you cannot plan a fixed sequence of moves. Instead, the Ludo bot scores every legal move with a few priorities that good players also use:

  1. Finish a token if possible.
  2. Capture an opponent, especially one that has travelled far.
  3. Move a threatened token to safety.
  4. Release a new token on a six.
  5. Otherwise, advance the token that is furthest ahead without stepping into danger.

It is not unbeatable, and it should not be. Ludo is meant to be a lively family game.

Rock Paper Scissors: reading your habits

In theory Rock Paper Scissors cannot be won reliably: a purely random player wins exactly a third of the time. But people are not random, and our bot uses that:

  • Easy really is random.
  • Medium counts which hand you throw most often and plays what beats it.
  • Hard remembers what you threw after each pair of your previous throws. If you tend to play scissors after rock-then-paper, it notices and counters.

If you feel the hard bot is reading your mind, it is reading your habits. Mixing up your choices on purpose is the only defence.

Air Hockey: a bot with hands

The Air Hockey bot works differently because it has to move a physical striker:

  • When the puck is far away or heading to the other side, it returns to a defensive spot in front of its goal.
  • When the puck is slow and on its side, it moves behind the puck and strikes towards your goal.
  • Difficulty controls how fast the striker can move and how quickly it reacts. The hard bot is not smarter so much as quicker.

Fair, not frustrating

None of our bots peek at hidden information or bend the rules. The dice are the same dice and the physics are the same physics. If a bot wins, it won fairly. Pick the level that gives you close games. That is where you improve the fastest.

Keep reading