Koi-Koi is a Japanese card game for two players, played with a 48-card hanafuda deck: 12 suits named after the months, four cards each. A full game is up to 12 rounds, one per month. You play a card from your hand and match it to a table card of the same month. The cards you collect can form scoring combinations called yaku. Once you complete a yaku, you can stop and score the round. Or you can say "koi-koi" and keep playing for a bigger score. Your opponent then plays on, and if they complete a yaku first, you score nothing and they get double their points.
I built a digital version for iPhone, iPad and Mac, with a computer opponent in three levels. The hard part is not the rules. It is that the opponent plays a game with hidden information. It cannot see your hand or the draw pile, and a bot that quietly reads them is easy to write and easy to miss in testing.
So the project treats "the computer never peeks" as a requirement, and checks it in code, in tests, and in the verify script. This post walks through the strong level and those checks.
Sampling the hidden cards
The strong opponent is a one-ply Monte Carlo player. For each decision it does three things:
- It works out which cards are still unseen from its own side: the 48 cards minus the cards it can see, which are its own hand, the table, and both captured piles.
- It fills the unseen cards in at random, many times, and rolls each candidate action out to the end of the current round. The rollout plays both sides with the normal-level heuristic.
- It picks the action with the best average score difference.
The budget depends on the decision. When the bot has to choose between koi-koi and stop, it draws 300 samples. Otherwise it draws 40 samples per candidate and simulates only the top two heuristic moves. I gave the koi-koi decision the largest budget because a single round can swing the most there. That is my reasoning, not a documented design rule.
A sketch of the idea, not the shipped code:
// A sketch of the sampling loop. func choose(view: PlayerView, legal: [Action]) -> Action { let unseen = hiddenCards(from: view) // viewer's side only var best = legal[0] var bestScore = -Double.infinity for action in candidates(legal) { var total = 0.0 for _ in 0..<samples(for: legal) { let world = fillRandomly(unseen) // one guess at the hidden cards total += rollout(world, after: action) // normal heuristic to month end } if total > bestScore { bestScore = total; best = action } } return best } There is one hard check in the real code. The sampler precondition-checks that the number of unseen cards equals the opponent's hand size plus the draw pile size. If the bookkeeping is wrong, the app stops instead of quietly making decisions on bad data.
The type boundary
The first line of defence is the type system. The computer player only receives a PlayerView, the same snapshot a human sees, not the full game state. The rollout only runs on a state that the sampling function builds from that view plus the guessed hidden cards. The real game state is never passed in. Reaching it would need a new parameter, which is visible in code review.
A type boundary is not proof, though. A developer, or an AI agent, can break it in a single line. So there are two more layers.
The swap test
One test takes a game state, reshuffles the opponent's hand and the draw pile three times, and checks that all three computer levels make the same decisions. It covers the play, decide and choose phases. If a decision depended on a hidden card, the reshuffle would have a chance to change it, and the test would fail.
A second test swaps the opponent's first hand card with the first card of the draw pile and checks that the player's view is unchanged. This only checks the view, not the decisions.
Neither test proves much alone. A bot can read a hidden card and still pick the same move by chance. That is why the next layer exists.
Source scans and mutation checks
A third test, testCoreSourcesHaveNoMutableGlobalsAndCPUHasNoDirectStateAccess, scans every file under the core sources. It rejects static var, nonisolated(unsafe), and file-level var. In the computer-player files and the player file, it also rejects references to the simulation, the raw .sides and .deck fields, and reads of the opponent's hand. It allows .count and assignments, because the hand size is public information.
A guard you never see fail is not a guard. So my project notes record mutation checks: add a rule-breaking line on purpose, confirm the test turns red, then revert it:
- A global
nonisolated(unsafe) varadded toSimulation.swiftturns the source scan red. - Appending
s.deck[0]toCPUStrong.swiftturns the source scan red.
The notes also list a limit: a deliberate @unchecked Sendable bypass would not be caught by these scans. I think it is better to say that in a post than to hide it.
The verify gate and a win-rate alarm
Before a release, the project runs bin/verify. It runs the Swift tests, runs the simulator twice with the same seed to compare hashes, and checks computer strength. For the strong level against normal, the check plays 1,000 games with the seats swapped and requires at least 60 percent wins.
There is also a ceiling. If the strong level wins more than 90 percent against normal, verify fails and tells you to check whether the bot read real hidden cards. The script comment says strong beats normal about 62 to 64 percent of the time, so 90 is far above that. A bot that peeks could show an unusually high win rate. That is a warning sign only, not proof.
My project notes from 2026-11-12 record the strong level beating normal 63.6 percent and 61.8 percent with the seats swapped, seed 7. A later review fix recorded 63.6 percent and 61.7 percent. Both are above the floor and far below the ceiling.
The verify gate also fails on any rule violation and on any card-count mismatch. The strong level's decision time is checked against a 50 ms limit at the 95th percentile on Mac. My notes record about 1.4 to 2 ms in runs from the same date.
What this does not prove
A 63 percent win rate does not show that the bot is honest. It shows that the bot is strong and that the alarm did not fire. The honesty claim rests on the type boundary, the swap test, the source scans, and the mutation checks together. If one layer is weakened, the others are still there, but each has a gap, which is why the notes list the gaps.
How I build
An AI coding agent wrote most of the code and tests, and I reviewed them. One detail specific to this project: the same-seed hash check in verify also catches a bot that depends on system randomness or dictionary iteration order, not only one that peeks.
If you want to play against the strong level, the app is Hanafuda: Wa no Shirabe, on the App Store at https://apps.apple.com/us/app/hanafuda-wa-no-shirabe/id6834981530. The strong level needs a one-time $2.99 unlock, and the free download does not include it.
How do you test that a game AI is not cheating? I would like to hear if you have a check I have not thought of.
Drafted by AI agents from my project notes and code, then reviewed by me before posting.

