← All articles

Blackjack Player: the 2017 dissertation where it all started

11 min read

  • C#
  • Unity
  • Algorithms
  • Card counting
  • University

Long before Perfect Pair Blackjack existed, there was Blackjack Player: my final-year project for a BSc in Computer Science, submitted in October 2017. It was an Android app built in Unity and C#, written up in a 72-page report, and its aim went beyond letting someone play blackjack. I wanted the app to play itself: to simulate hundreds of hands using different strategies, including card counting, and show which one made money.

This article looks at what I built, how it worked, what the results showed, and what I’d now do differently.

2017 · DISSERTATION Blackjack Player Unity · C# · Android advice mode Hi-Lo simulation NATIVE REWRITE Blackjack for iOS Swift · SwiftUI drag-and-drop chips native animations 2026 · LIVE Perfect Pair Blackjack HTML · CSS · JavaScript Perfect Pairs + 21+3 ppblackjack.co.uk
Three versions of one idea. The dissertation came first. Its card model, shuffle and strategy logic were rebuilt, and improved, in each version after it.

The brief

The report set out three playing modes:

  • Default mode: play blackjack as you would online, with a bank balance stored on the device, betting chips, hit, stand, double down and insurance.
  • Advice mode: a toggle that tells you the mathematically best move for your hand against the dealer’s up card, taken from the basic strategy table.
  • Simulation mode: enter a number of hands and let the computer play them using either basic strategy or the Hi-Lo card-counting system, raising its bets when the count is in its favour, then report wins, losses, pushes and profit.

The game used four decks (208 cards), the dealer stood on 17, and a natural blackjack paid 3:2.

The Blackjack Player game scene in Unity: a green felt table with Hit, Stand, Split, Deal and double buttons, five betting chips from 1 to 50, a bank balance of 400 and an Advice toggle.
The game scene, with the Advice toggle bottom right
The Blackjack Player simulation scene: a field for the number of hands to play, Simulate and Reset buttons, a choice between Basic Strategy and Card Counting, a progress bar, and results for hands won, lost, pushed, played, splits and bank roll.
The simulation scene: pick a strategy and a number of hands

Research first

Before writing any code, the report covered the maths behind the game. Played perfectly, basic strategy cuts the house edge to well under 1%. It’s a lookup table, giving one correct action for every combination of player hand and dealer up card.

Card counting goes further. The key insight, from Edward O. Thorp’s Beat the Dealer (1966), is that the remaining cards change the odds as the shoe is dealt. A shoe rich in tens and Aces favours the player, while one rich in low cards favours the dealer. Thorp described several systems, including counting fives and counting tens. I chose Hi-Lo, the most widely used:

  1. Give every card a value: 2–6 count +1, 7–9 count 0, and 10s and Aces count −1.
  2. Keep a running count as cards are dealt.
  3. Divide by the number of decks remaining to get the true count, which measures how favourable the shoe is regardless of how deep into it you are.
  4. Bet more when the true count is high, and the minimum when it isn’t.

The report also compared technologies. Unity won over Unreal Engine because it uses C#, which I knew well. Android won over iOS because building for iOS needed a Mac to run Xcode. Ironically, the second version of this game was a native SwiftUI app.

Cards as integers

In Unity, every card was a prefab: a reusable game object with a sprite renderer, a CardModel script holding an array of card-face sprites, and a CardFlipper script. The flip animation used an AnimationCurve to scale the card’s X axis from 1 down to 0 and back again, swapping the sprite from back to face halfway through, so the card appears to turn over.

Under the hood, though, a card was just an integer index from 0 to 207. The shoe, the player’s hand and the dealer’s hand were each a CardStack: a list of integers, with cards dealt by popping an index off the shoe and pushing it onto a hand. A card’s rank came from modular arithmetic, since each suit has 13 cards:

card index = 103
card rank  = 103 % 13 = 12  →  King

Shuffling used the Fisher–Yates algorithm, which gives every ordering of the deck an equal chance. The report covers both versions of the algorithm and the difference between pseudo-random and truly random number generators, a real concern in online gambling, where a predictable seed would let someone predict the cards. The shoe was refilled and reshuffled whenever fewer than 40 cards remained, so a hand could never run out of cards mid-deal.

Strategy as data, not code

Rather than hard-code basic strategy and Hi-Lo into a wall of if statements, I stored both as XML documents, one entry per player hand and dealer up card. The simulation parsed the right document at runtime and looked up the advice.

Hi-Lo adds a twist: some decisions depend on the count. For a hard 8, basic strategy always says hit, but Hi-Lo says double down against a dealer 5 or 6 once the count is even slightly positive:

<cardInfomation yourhand="8">
  <dealerupcard value="5">
    <adviceIfIndexIsGreater>DOUBLEDOWN</adviceIfIndexIsGreater>
    <hiLoIndex>0.05</hiLoIndex>
  </dealerupcard>
  <dealerupcard value="9">
    <adviceIfIndexIsGreater>HIT</adviceIfIndexIsGreater>
  </dealerupcard>
</cardInfomation>

If the player’s current Hi-Lo index beats the threshold, the simulation takes the stronger play; otherwise it falls back to the safe one. Keeping strategy as data meant I could correct a table entry without recompiling, and I could test the parser on its own by writing every lookup out to a text file and checking it against the published charts.

How the count drives play and betting

A CardCounter class tracked the running count as every card was dealt, and derived two numbers from it: one for how much to bet, and one for how to play.

Card dealt player or dealer Score the card 2–6+1 7–90 10–A−1 Running count sum so far True count ÷ decks remaining → how much to bet Hi-Lo index ÷ cards remaining → how to play
One count, two decisions. The true count scales the bet before the deal; the Hi-Lo index is checked against the XML strategy during the hand.

The betting system was a bet spread: one unit when the count was neutral or negative, climbing steeply as the shoe turned in the player’s favour, up to 12 units at a true count of +5 or more.

0 12 1× 2× 4× 8–9× 12× below +2 +2 +3 +4 +5 or more 2 × true count betting units
Bet small, wait, then bet big. Card counting gains an edge only on the rare hands when the count is high, so the bet spread is what turns that edge into profit.
if (cardCounter.CalculateTrueCount() < 2)
{
    PlayerBettingUnit = 1;
    PlayerBet = PlayerBettingUnit * PlayerBet;
}
// … +2 → 2 units, +3 → 4 units …
else if (cardCounter.CalculateTrueCount() >= 4 && cardCounter.CalculateTrueCount() < 5)
{
    int DoubledTrueCount = Convert.ToInt32(cardCounter.CalculateTrueCount() * 2);
    PlayerBet = DoubledTrueCount * PlayerBet;
}
else if (cardCounter.CalculateTrueCount() >= 5)
{
    PlayerBettingUnit = 12;
    PlayerBet = PlayerBettingUnit * PlayerBet;
}

The play itself was recursive: on a hit, the simulation drew a card, looked the new hand up in the strategy again, and kept going until it stood or bust. Then a single DealersTurnAndEndConditions() method played out the dealer’s hand and settled every bet, including split hands.

The results

I ran each strategy ten times for 100 hands, starting each run with a bankroll of 400:

Strategy Avg won Avg lost Avg pushed Avg final bankroll Avg profit Best run Worst run
Basic strategy 42.8 52.7 7.0 843.5 +443.5 +1,260 −265
Hi-Lo counting 44.7 48.7 7.3 832.3 +432.3 +1,295 −297

Hi-Lo won more hands and lost fewer, exactly as the theory predicts. Both strategies more than doubled the bankroll on average, but the report is honest about the caveats. A bug in how split hands were counted inflated basic strategy’s totals; you can see it in the hands-played column, which goes above 100 for basic strategy but not for Hi-Lo. And the swings were enormous, with individual runs ranging from a 297-chip loss to a 1,295-chip gain.

Looking at it now, the bigger issue is sample size. A hundred hands is nowhere near enough for blackjack’s small edges to show through the noise. The real difference between these strategies is a fraction of a percent, and seeing it reliably takes hundreds of thousands of hands.

What didn’t get finished

The report is candid about this, and I still think that’s the most valuable part. Splitting worked in the simulation but never made it into the playable game. The simulation’s reset button didn’t clear everything. The in-app instructions were cut for time. And there were no automated tests: Unity’s Test Runner was available, but testing came down to a manual test table of 26 scenarios and Debug.Log() output.

From 2017 to now

Rebuilding this game twice since has been a good measure of what I’ve learned:

  • Cards became objects. Representing a card as a number and deriving its rank with % 13 was clever, but fragile. In the web version, a card is { suit, rank, faceUp } and there’s nothing to decode.
  • The shuffle got faster. The dissertation used the original Fisher–Yates (O(n²)); today’s game uses the modern in-place swap (O(n)). For 208 cards it doesn’t matter. Knowing why it doesn’t matter is the point.
  • Splitting finally shipped. The rule that defeated me in 2017 works in the web game, with up to four hands and one card each for split Aces.
  • Logic moved out of the god-object. In 2017 almost everything lived in one Game Controller script, and the report itself says it should be broken up. In the web version, the rules are small, pure functions that are easy to read and test.
  • If I revisited the simulator, I’d run millions of headless hands with a seeded random generator, so results could be repeated, and chart how each strategy’s edge converges, rather than eyeballing ten runs of 100.

The full report is on GitHub, and Perfect Pair Blackjack is where the idea ended up. The technical write-up covers how that version works.