All writing

5 min read

Losing to my own bot

Four rewrites to get a card-game bot that plays well with hidden cards, and what AI coding assistants were good and bad at along the way.

Earlier this year I lost a game of Touti, and I was very happy about it. I lost to a bot I built.

Touti is a Moroccan trick-taking card game: four players, two teams, forty cards, an auction, a trump suit chosen by whoever buys the contract. Think Bridge, but Moroccan. My wife Hiba and I turned it into an app for iPhone, Android and the browser. The bot inside it took four complete rewrites. This is the story of those rewrites, and of what AI coding assistants did and didn’t do for me along the way.

Why a Touti bot is hard

You can’t see most of the cards. You hold 10 of 40. The other 30 are in three hands you know nothing about at the start, except what the auction suggested.

You play with a partner you can’t talk to. The only signals are the bids and the cards themselves.

Points beat tricks. The buying team must collect at least the number it bid, out of 130 points in a round. Winning a trick is worth nothing if it costs you the contract.

The game tree is enormous. Every card is a branch, every hidden card multiplies the possibilities, and there are ten tricks to go.

About five years ago I tried to build this and gave up. The interface and the networking were fine. The bot was the problem, and writing and debugging a game AI alone in my evenings was too slow to be worth it.

In January 2025 I tried again, with one question in mind: how far can I go with AI coding assistants?

Rewrite 1: a bot that cheats

The first bot used Monte Carlo tree search (MCTS), the family of algorithms behind many strong game programs. MCTS doesn’t need a hand-written evaluation: it plays thousands of quick games from the current position, keeps statistics on which moves led to wins, and spends more of its time on the promising ones.

It played beautifully, because it could see everyone’s cards. Standard MCTS assumes perfect information, so the bot was searching the real deal, hidden hands included. That’s cheating, and you can’t ship a bot that peeks. It also teaches you nothing about the real problem, which is deciding without knowing.

Rewrite 2: minimax, too short-sighted

Next I tried minimax, the classic algorithm for two-sided games: assume each side plays its best reply and search the tree for the line that holds up.

The tree in Touti is far too big to search to the end, so minimax has to stop a few cards ahead and score the position with a rule of thumb, usually the points in hand. That’s the trap. A bot that counts points a few cards ahead grabs points now and loses contracts later. Touti is won on the outcome of the round, not on the next trick.

Rewrite 3: neural networks, garbage in

Then I tried to learn the game instead of searching it. A neural network learns from examples, and here was the catch: there is no big database of expert Touti games. The examples had to come from games I generated myself, played by bots that were not good yet. Garbage data in, garbage results out. The network learned the bots’ habits, mistakes included.

Rewrite 4: IS-MCTS

The design that shipped is Information Set Monte Carlo Tree Search (IS-MCTS). The idea is simple: if you can’t see the hidden cards, imagine many ways they could be dealt, search each one, and pick the move that works best across all of them.

An information set is everything the bot can’t tell apart from its own seat: all the deals that match its hand and what it has seen. The bot tracks, for every unseen card, who could still hold it. The play narrows this down: a player who fails to follow suit has none left in that suit. So does the auction: in Touti you can pass by saying “As”, which means you hold an ace, and a buyer usually holds strength in the trump they chose.

Determinization turns that uncertainty into concrete worlds. The bot samples complete deals from those probabilities, with every hand the right size and no card where it can’t be. In each sampled world it runs ordinary MCTS, then it adds up the results for each legal card across worlds and ranks them by chance of making the contract, expected score and how often the search chose them.

The shipped budget is 35 determinizations × 350 iterations: 35 imagined deals, 350 search iterations in each, about 12,000 simulated playouts for every card the bot plays. Near the end of a round, with three cards or fewer per hand, an exact solver gives the true value of each world instead of a random playout.

The result? The bot is really good, but not perfect. It makes brilliant plays and, sometimes, very dumb mistakes. Honestly, not that different from LLMs.

What the AI assistants were good at

Writing code. Given a clear design, they produced working code much faster than I could: a rules engine, a search, a test, a command-line tool.

Explaining things I didn’t know. I’m a data person, not a game-AI researcher. The assistants walked me through search algorithms and their tradeoffs.

Building the tools around the work. Things I would never have found time for got built: an arena that plays thousands of games between two bot versions and keeps the results, a replay viewer over recorded games, end-to-end tests that drive the real game. When the codebase got big, I had an assistant extract the relationships between classes and methods into a CSV, then build a small Python tool to explore the architecture interactively.

Grinding through experiments. Later, I let AI agents run the arena on their own for two to three days. They proposed changes, tested each one against the current best bot, and kept only changes that won by a statistically meaningful margin. The final champion won 4% more games than the original baseline.

What they were bad at

Architecture. Left alone, an assistant solves the problem in front of it. It adds a special case, a fallback, a second way of doing something that already exists. Each change looks reasonable. A few hundred of them and the code fights you. The rules file my coding agents read starts its principles with “No workarounds, no fallbacks”, and that line is there for a reason.

Real problem-solving. The assistants made each approach work as well as it could. Deciding that an approach was wrong for this game, and why, was my job.

Knowing what “good” looks like. An assistant can’t tell a smart Touti play from a dumb one unless you give it a way to measure. I annotated 19 hard positions with the card I would play; after one round of search upgrades the bot went from agreeing with me on 69% of them to 96%. That kind of yardstick has to come from someone who knows the game.

The short version

  • “Vibe coding” is fine for a proof of concept. It doesn’t scale.
  • AI is great at writing code, and weak at architecture and real problem-solving.
  • No, it isn’t taking our jobs. It’s a force multiplier, not a replacement.

What started as a learning project became a real product. Touti is free, with no ads, on iPhone, Android and the browser. Thanks to Hiba, my co-designer, for the countless hours of brainstorming, testing and interface ideas. If you play, try the Expert bots. Then tell me who won.