So I Built A Cupcake Card Game Simulator

Orchard Toys do a great line in table top games for kids.  They include games of chance, strategy and memory using thick card and clear, colourful illustrations. I heartily recommend any of the line as good value for money birthday presents.  (They also have a Pirate Memory Game, a fact which will be hilarious to fans of Little Britain).
One of their games is Where’s My Cupcake?  Children as young as 3 can play with adults on an equal footing because the game is entirely based on chance.  Players take turns to pick a cupcake card off a central pile, and see if it matches one of the cake cards laid out on the table.  If it does, they add both cards to their pile.  If it does not, they check to see if anyone has a matching card on the top of their pile, asking “would you like a cupcake?”  If no-one claims the cupcake card, its placed on the table and the next person takes a turn.  Play continues like that until the pile of cards are exhausted. The player with the most cupcake cards is the winner. Full instructions are here.
The only problem with the game is that because it is entirely based on chance, its actually very hard to let a very young person win, if you want them to!  Sometimes, a string of bad luck can mean they miss several opportunities to put a cupcake on their plate, and they might lose several games in a row.  For someone just learning how to share and play fair, this can be demoralising to the point where they refuse to play.  It would be nice to be able to optimise their chance of victory.
Since the game is entirely procedural, the outcome of the game is pre-determined from the moment the cards are shuffled.   However, the shuffling involves 30 cards with 10 designs on them, which means there are 4.39 x 1039 possible combinations.  Even the fastest super computer in the world would take several millennia to evaluate every combination.
Nevertheless, I decided to script a virtual version of the game, so I could simulate many hundreds of games and discover which player is statistically most likely to win.  Armed with that knowledge, I can ensure that the person I want to prevail is sat in that spot when the game is played, and thereby decrease the likelihood of tears before bedtime.

2-287-where-s-my-cupcake-game-830-standardThe scripts

Embarrassingly, the only procedural coding language I know is PHP.  Its probably not the most efficient language for the task, but its all I have to work with.
My code combines the array and shuffle functionality in PHP to create a randomised ‘stack’ of 30 cupcake cards.  It then works its way through the stack, evaluating each card in turn and comparing it to other arrays the represent the other parts of the game: a ‘mat’ array, representing the table on which cupcake cards are sometimes placed; and a ‘plates’ array which represents the cardboard plates where players hoard their cupcake cards.  An additional ‘top card’ array logs the design of the most recently placed cupcake, which is also relevant to the game.
The PHP script simply loops through this evaluation thirty times, until all the cards in the stack have been evaluated.  Finally, the script looks at the plate array to work out which player has the most cards and is therefore the winner.
All this sits inside an additional loop.  Once the game has been completed, an additional array logs how many times each player has won.  All the scores and tallies are output to a grid.
I created separate scripts for two player, three player and four player games.  I also fashioned a form to easily access each of these simulators.  Finally (actually, initially) I created a ‘verbose’ version of the simulator that outputs each step in the game and also the state of the stack, mat and plates at the end of each turn.
I uploaded all of these scripts to GitHub if you want to look at (and maybe improve upon) the PHP code.

The results

I ran the scripts to simulate 10,000 games with two players, three players and four players.  Winners score 1 point, and when two or more players are tied, they each receive 1 point.

Two players

Player A Player B
5736 5204

Three players

Player A Player B Player C
4184 3754 3405

Four players

Player A Player B Player C Player D
4296 3776 1919 1685

Here’s a chart of the four player results.

Running the simulations again yields results with very similar proportions. I ran a 10,000 game simulation 5 times, and graphed the scores.

I think the results are clear, conclusive and to be expected:  Its always better to go first in Where’s My Cupcake?
However, going first only offers a slight advantage.  Over many dozens, hundreds or thousands of games, Player A will always prevail.  But when you only play three or four rounds over (say) 20 minutes, there’s every chance that the other players will beat Player A (although with much less regularity if you are player D).

What next?

The logical next step is to modify my scripts so that one can actually play the game with a child.
However, there is also some further mathematical detective work to do.  Uniform win ratios suggest that although there are many trillions of possible sequences of cards in the deck, perhaps only a small number of card positions affect the outcome.
If you think about it, this is self evidently true.  Since the mat has a full compliment of cupcakes on it at the start of the game, and no-one has any cupcake cards on their plates, the first card in the pack never matters.  It will always go, along with its pair on the mat, to Player A.
There will be more complex rules of that nature too, perhaps related to how often two cards of the same design fall next to each other in the deck, and the point at which matching a cupcake card to one on the mat becomes less than a 50% chance.  I’m still unsure of the precise mathematics that explain why the win ratios are what they are, and why they seem so consistent.  Chaos theory may come into play.

One Reply to “So I Built A Cupcake Card Game Simulator”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.