karata is two card games for Android, Mysterio and Classic, that you can play against bots. A card game has one job above all: follow its rules every time. A wrong penalty or a card that disappears ruins the game, and a player notices at once. Here is how I make sure that doesn't happen.
Writing the rules before the code
Before any code, I wrote the rules down in one file: 18 rules, M1 to M10 for Mysterio and C1 to C8 for Classic. Every rule has at least one example, played out card by card. Each example then became a test, so every rule is checked by the tests.
An engine with no UI
The rules live in their own Dart package, with no Flutter in it. The game state never changes in place: every move returns a new state. This means the rules can be tested without a phone, a screen or a single tap, and the tests run in seconds.
From a rule to a test
Rule M6 says that a 2, a 3 or a Joker attacks the next player, who must pick up 2, 3 or 5 cards. The attacked player can answer with another of these cards, and the penalties add up. The rule's first example: A plays 2♠, B answers with 3♠ (penalty 5), C has no answer and picks up 5 cards, then D plays normally. The test plays exactly that:
round = round.play(cards('2♠'));
expect(round.penalty, 2);
round = round.play(cards('3♠')); // same suit
expect(round.penalty, 5);
expect(round.currentPlayer, 2); // C
expect(round.canPlay(cards('K♥')), isFalse);
round = round.draw();
expect(round.penalty, 0); // the attack is over
expect(round.currentPlayer, 3); // D plays normally on 3♠Today the engine has 265 tests like this one.
32,000 random games, checked after every move
Examples only cover the cases I thought of. So the engine also plays games against itself: 20,000 games of Classic and 12,000 of Mysterio, with every player count and point limit. Each move is picked at random, and after every single move the test checks:
- no card was lost or duplicated (always 54 cards in Mysterio, 24 in Classic);
- the turn went clockwise;
- penalties add up as rule M6 says;
- each player's view shows their own hand and nothing more;
- at the end, scores and ranking follow the rules.
Over so many games, every seat must also win sometimes. The randomness is seeded, so a failure prints the seed, and that exact game can be replayed to find the problem.
Fast tests on every push, slow ones before a release
The 249 fast tests run in about 3.5 seconds. GitHub Actions runs them, with a strict analysis, on every push and pull request, then builds the APK. The 16 stress tests take about 25 seconds, so they're tagged slow and run on my laptop before a release. The app adds 223 tests of its own on top of the engine's.
The stress tests are what let me change a rule without fear: if a change breaks another rule somewhere, one of the 32,000 games finds it.
What I learned
- Writing the rules down before coding, and turning every example into a test, saved me from countless bugs.
- A bot that's fun to play against is a design problem, not just an algorithm: it must be strong but never cheat.