All projects

Personal

Touti

A Moroccan team card game for iPhone, Android and the browser, with bots that search thousands of possible deals before every card.

Role
Co-designer and sole developer
Period
2025 – 2026
Stack
Unity 6 · C# · .NET · Photon Realtime · PlayFab · Azure Functions · Azure SQL · SQLite · Python
bot designs, the last one shipped
4
sampled deals × search iterations per card
35 × 350
agreement with expert picks on 19 hard positions
69% → 96%
ads: the game is free
0
A Touti game in progress on a tablet: a wooden table, Spanish-suited cards, three bot players and the player's hand at the bottom.

Touti is a Moroccan trick-taking card game for four players in two teams. Think Bridge, but Moroccan. With my wife, Hiba Hilal, as co-designer, I built it for iPhone, Android and the browser, with online tables, a learn-to-play course and bots good enough that I lose to them. The bots took four complete rewrites. The version that shipped samples 35 possible layouts of the cards it can’t see, runs 350 search iterations in each, and only then plays a card.

It’s free, with no ads, at touti.meknassi.ca.

The problem

I tried to build Touti once before, about five years earlier, and stopped. The interface and the networking were not the hard part; the bot was. Writing and debugging a game AI alone, in my spare time, was too slow to be worth it.

In January 2025 I started again with a different question: how far can I get with AI coding assistants?

The game explains why the bot is hard. Each player gets 10 cards from a 40-card Spanish-suited deck. An auction decides which team commits to a score, from 70 to 210 points. Your partner can push you higher (+10, +20, +30), and passing with “As” tells the table you hold an ace. The buyer picks the trump suit. Then come 10 tricks with strict rules: follow suit, trump if you can’t, and beat the card on the table when you can. After winning a trick, the buying team may “sing” a marriage (the 11 and 12 of one suit) for 20 points, or 40 in trumps, up to a cap set by the bid. At the end, the buying team either makes its number or the defenders score it.

For a bot that means:

  • Imperfect information. You see 10 of the 40 cards.
  • Team play with almost no communication. The bids and the cards are the only signals.
  • Outcome over output. Winning a trick matters less than making or breaking the contract.
  • A huge game tree, branching at every card.

Constraints

  • One developer, on my own time. Hiba shaped the design, the interface and the testing; the code was mine and my AI assistants’.
  • Phones and browsers. A bot must think on a phone without making people wait, and the browser build has a single thread, so the search has to share each frame with the animations.
  • Fair online play. One player’s device runs the authoritative game, and older app versions in the field must keep working with newer ones.
  • Three languages, two scripts. French, English and Darija, written in Latin or Arabic letters. Arabic letters must be joined and laid out right to left, which Unity’s text engine doesn’t do on its own.

What I built

  • The game, in Unity 6 for iOS and Android, plus a web build playable at touti.meknassi.ca.
  • Online play with Photon: public tables to sit down at right away, private tables with friends where bots take the empty seats, and reconnecting to your seat after a dropped connection.
  • Accounts and a backend. PlayFab handles sign-in, one account across devices, and texts and translations that change without a release. Azure Functions with Azure SQL record every round, compute standings, and serve weekly and all-time leaderboards.
  • Learn to play: a course in three modules (the basics, singing, bidding), each ending with practice rounds against the bots, a coach that explains what is happening, and hints.
  • Tarra9: lay out your remaining cards and claim every trick that’s left. The game checks the claim, and the bots do it to you too.
  • The tooling behind the bots: a .NET command-line arena that plays thousands of games between two bot versions and stores the results in SQLite, a replay viewer over recorded games, unit tests, and end-to-end tests that drive a Linux build of the game through scripts.
Two Touti screenshots side by side. Left: the auction, where the player's partner has pushed +10 and the bid picker shows 120. Right: the first trick, with coins as trumps at 120.
Left: my partner pushes +10 on my bid of 100, so I must go to at least 120. Right: I took the contract at 120 with coins (oros) as trumps.

How it works

The card-play bot uses Information Set Monte Carlo Tree Search (IS-MCTS). In plain terms: it can’t see the other hands, so it imagines many complete deals consistent with what it knows, searches each one, and picks the card that does best across all of them.

  1. What do I know? An information set tracks, for every hidden card, who could still hold it. Play narrows it down: a player who doesn’t follow suit has no cards left in that suit. Bids shift the odds: a player who said “As” holds an ace, and the buyer probably holds strength in their trump suit.
  2. Imagine 35 deals. The bot samples 35 complete layouts of the hidden cards from those probabilities, adjusted so every sampled hand has exactly the right number of cards and never contains a card that player can’t hold.
  3. Search each deal. In each one, a standard Monte Carlo tree search runs 350 iterations: pick a promising line, expand it, play the rest of the round out, and record whether the team made its contract and by how much. Near the end of a round, with three cards or fewer per hand, an exact solver computes the true outcome instead of playing it out at random.
  4. Vote. The results from all 35 searches are combined per legal card and ranked by chance of winning the round, expected score and visits.
  5. Check. A thin layer of rules catches known blind spots, such as dumping valuable cards into a trick the team can no longer win, unless the search strongly prefers that card.

Bidding runs on its own engine. Machine learning came back here in a narrower role: a gradient-boosted model, trained on games the bots played in the arena, estimates what a hand is worth, and a solver raises the bid when a hand provably wins every trick. Beginner and Expert bots use the same search budget; they differ in how much risk they take in the auction and how well they remember the cards.

Every bot decision is logged with the information set behind it, so I can replay a real game from production and ask why the bot played what it played.

Results

  • Shipped, free and without ads, on the App Store, Google Play and the web. Most players enjoy playing with and against the bots, and online multiplayer is growing slowly.
  • A bot that plays well, not perfectly. It makes brilliant plays and, now and then, a very dumb one. Honestly, not that different from an LLM.
  • Measured improvement. I annotated 19 hard positions with the card a strong player would choose. One round of search upgrades (an opponent model, a margin-aware objective, the exact endgame solver) raised the bot’s agreement from 69% to 96%, at the same speed.
  • Agents tuning bots. I set AI agents loose on the arena for two to three days: they proposed configuration changes, tested each one against the current champion, and promoted only statistically significant winners. The final champion won 4% more games than the original baseline.
  • Built with AI coding assistants. They wrote code fast and well. The architecture, the debugging of hard problems and every judgment about what “good play” means stayed with me. That’s the longer story of the four rewrites.

What I’d do differently

Build the measuring stick first. The arena turned “this bot feels better” into a number with a confidence interval, and it only arrived after the fourth bot design.

Design for the browser from day one. The web build has one thread, so I had to rework every search to run in small slices between frames. It’s cheaper to plan for that than to retrofit it.

Fix the search before adding rules. Each guard rule is a correction bolted onto the search. They work, but every one is also a sign the core algorithm missed something, so I now treat them as a last resort.