One card, two melds: how Peekaboard's Gin Rummy counts your hand
In Gin Rummy a single card can often fit in two places, and where you put it decides whether you can knock. Peekaboard sorts your hand into its best melds and shows your count before every discard. Here is how that counting works, and the one moment when the best answer changes.
Gin Rummy came to Peekaboard in version 1.2, as the third game at the table after Naval Duel and Bluff Cups. The App Store text has one line about it that sounds small: “The game finds your best melds and shows your count as you play.” That line is most of what this post is about, because it’s the part of the game I spent the most care on, and it’s the part you never see working.
The arithmetic nobody enjoys
If you haven’t played: you hold ten cards. A meld is a set (three or four cards of one rank) or a run (three or more cards of one suit in a row). Every card that isn’t in a meld is deadwood, and it counts against you. Face cards count 10, an ace counts 1, everything else its number. When your deadwood after a discard is 10 or less, you can knock and end the hand. With no deadwood at all, you go gin. Peekaboard uses the common rules from pagat.com, and the ace is always low: A-2-3 is a run, Q-K-A is not.
At a real table, the counting is your job. You fan your cards, push them into groups with your thumb and add up the leftovers in your head. It’s easy to miss a knock you could have made, or to knock when you can’t, and then someone at the table has to sort it out.
On a phone, the game can do that counting for you. But that only helps if the count it shows is the best one your hand can make. A count that’s a few points too high would quietly cost you a knock, and you’d have no reason to doubt it.
Where a shared card should go
The hard part is that one card can belong to two melds. Here’s a five-card example from the tests:
7♣ 7♦ 7♥ 5♥ 6♥
The 7♥ can finish the set of sevens, or it can finish the run 5♥ 6♥ 7♥. It can’t do both. If it goes in the run, the 7♣ and 7♦ are left over, and that’s 14 points of deadwood. If it goes in the set, the 5♥ and 6♥ are left over, and that’s 11. The better answer is the set, though a quick glance might pick the run, because runs look tidier.
With ten or eleven cards, these choices stack up. A set of four can also be four different sets of three. A run of five holds runs of three and four inside it. You can’t decide each card on its own, because moving one card changes what’s possible for the others.
So Peekaboard doesn’t try to be clever about it. It writes down every meld the cards could make, including those shorter runs and the sets of three inside a set of four. Then it searches through every combination of melds that don’t share a card, and keeps the one that melds the most points. A hand is small enough that this is fast. Each meld is stored as a little bit mask of the cards it uses, so checking whether two melds overlap is a single operation.
When two arrangements leave the same deadwood, the solver picks the one with fewer melds, then the one with fewer loose cards, and after that a fixed order, so the same hand always sorts the same way.

The screenshot from the App Store page shows the same choice in a real hand. There are four twos in it. Put the 2♠ with the other twos and you get a set of four, but then the A♠, 3♠ and 4♠ are left with nothing, 8 points of deadwood. Put it in the spades and you get A-2-3-4 as a run, the three other twos are still a set, the nines are a set, and nothing is left over once the 5♥ is discarded. The panel says “Gin! Every card is in a meld.” and the yellow button changes from Knock to Gin!.
What you see while you play
The solver runs every time your hand changes, and the table follows it. Your cards are always laid out the way you’d meld them: each meld together, then the loose cards from low to high. You don’t drag anything into place.
When it’s your turn to discard, the line above the buttons tells you where you stand. Before you choose, it says “Tap a card to discard. Deadwood now:” with the number. When you tap a card, it works out the best arrangement of the ten cards you’d have left and shows that instead: “Deadwood after the discard: 10. You can knock.” The Knock button only appears when that number is 10 or less. If you tap the card you just took from the discard pile, it tells you to choose another one, because the rules don’t let you throw it straight back.
The point of all this is that you get to think about the part of Gin Rummy that’s actually a game: which card to keep, and whether to knock now or hold out for gin. The adding up is done.
The one moment the best answer changes
The best arrangement of your own hand isn’t always the best answer at the end of a hand. When someone knocks, the other player lays their cards down too, and can add loose cards to the knocker’s melds. That’s called laying off, and it can change which arrangement is best.
Here’s a hand that shows it. The knocker has a run of 4♥ 5♥ 6♥. The other player holds:
7♣ 7♦ 7♥ 7♠ 8♥
On its own, the best way to meld that is the set of four sevens, which leaves the 8♥ as 8 points of deadwood. But the 8♥ can’t join the knocker’s run, because the 7♥ is missing in between. If the player keeps only three sevens as a set instead, the 7♥ can go on the end of the run, then the 8♥ after it, and the deadwood is zero.
So for the defender, the game doesn’t stop at the best arrangement. It goes through every arrangement where no other meld could still be added, lays off as many loose cards as it can on the knocker’s melds for each one, and keeps the one with the least deadwood left. A card that joins a run can make room for the next one, so it keeps trying until nothing else fits. If the knocker went gin, there’s no laying off at all, as in the standard rules.
This matters for the score, not just for neatness. If the defender ends up with the same deadwood as the knocker or less, that’s an undercut, and the defender wins the hand plus a 25 point bonus. Getting the lay-offs wrong would hand points to the wrong player.
Checking a counter you can’t eyeball
A count that’s wrong by a few points looks exactly like a count that’s right. So the solver has a test that checks it against a second, deliberately simple one. That one takes the first card of the hand and tries two things: leave it as deadwood, or put it in each meld that contains it, then does the same with whatever is left. It’s slow, but it’s hard to get wrong, and it shares no code with the real solver apart from the list of possible melds.
The test deals 600 random hands and both have to agree on every one. Half of those hands come only from the low cards, ace to six, because a narrow deck makes far more overlapping melds than a normal deal, and overlapping melds are where a solver would slip.
The same counter is used everywhere in the game. The rules engine uses it to accept or refuse a knock. Captain Rojo, the computer player, uses it to judge each card he might throw away. And the hint line uses it to tell you your count. There’s one answer to “how much deadwood is this”, and the player and the computer both get it.
At the table
Peekaboard is a table of games for two people and one iPhone. Naval Duel comes with the app, and the Full Pass is a one-time $4.99 purchase that unlocks Bluff Cups, Gin Rummy and every game added later, with no subscription. You can play three matches of each before you decide. It works offline, with no account and no ads. On iPhone Duo you lay the phone flat between you and fold your half up to peek at your cards.
More about the games and the Duo version is on the Peekaboard page.