back to michaelgruen.com

What Am I In For?

Playing War (the card game)... how long will this take?

August 17, 2024

Drawpiles at war

My nephew asked to play cards.

“Sure!” I said, “any game you’d like.”

“Let’s play War!”

For the uninitiated, War is unequivocally—like its namesake—one of humanity’s worst inventions.

It goes as follows:

  1. Two players evenly split a deck into two draw piles
  2. Each player turns over the top card from their draw pile
  3. The higher-valued card takes the trick
  4. Players regenerate their draw pile when depleted, shuffling all cards currently in their possession
  5. Repeat until a player runs out of cards

The real action comes when two cards are of equal value: this means “War”.

  1. Each player burns three cards from their deck to put into a war chest (face down)
  2. Each player flips a forth card:
    • The higher-value card takes the trick, the war chest from both players, and the initial war-instigating pair. The hidden, face-down cards are revealed. (This is the fun, you see, and the only opportunity to capture the highest-valued card. e.g., an ace.)
    • If the forth cards are also of the same value… (gasp) you do it again!

There are some variations about what to do if you run out of cards during a “war”; but, this happens rarely. When playing with a five year old, it is an opportunity to bend the rules a bit (“Oh no, I ran out of cards!”) to hasten completion of this no-skill, random contest.

The only way to win War is not to play.

So I agree to play War.

My nephew is enthralled. I am counting down the minutes.*

Just how many minutes, exactly?

To make this interesting (for me at least), I start tracking our flipping and shuffling speed. My nephew is fast, but I am faster.

He likes to add in the two Jokers included in every deck of Bicycle playing cards. (They outrank the aces, he says.)

We get through ten tricks. Nine seconds by my count, clocked in Mississippis. Another ten tricks, eight Mississippis. Ten more, eleven seconds.

One trick per second. Average.

“War” happens. Three cards down, one card up. Four cards played in four seconds—parity with a normal card per trick rate.

His shuffling, less dexterous. On average, thirty-two seconds elapse before he can get back to card-flipping.

I sketch a War game-time algorithm in my head.

He narrates every trick. It does not impede game play.

I start thinking about which LLM will be best to code up a simulation. I am able to keep pace.

He accidentally flips over two cards. (They happen to be the same rank, so he carefully tucks the errant duplicate somewhere back into the deck. To keep the game suspenseful, I suppose.)

I estimate the chatbot and I will need a few back-and-forths to work through the edge cases.

He starts to accumulate a larger pile. We are both excited and for different reasons.

I wonder whether I should ask the LLM to write this simulation in javascript, python, or golang. I lean towards golang. For performance, of course.

We both flip eights. When the fog of “war” clears, it becomes clear he has taken possession of the last Joker protecting my ever-dwindling resources.

I consider whether posting my internal monologue of our game would be construed as dunking on my five year old nephew. While he can read now (!), I’m fairly sure he is not yet allowed access to the Internet.

Four cards remain in my hand. Defeat, a mathematical certainty.

He takes all four tricks, smiles, throws his cards on the table, and proclaims, “losers have to clean up!”, and runs off to play with his sister.

We’ll see who laughs last, kid.

Just how many minutes, exactly? (In code.)

I simulated 100,000 games of War on my M1 MacBook Pro.

LLM-assisted coding took forty-five minutes: about one minute to generate, and then another forty-four to debug for logical and coding errors. The compiled simulation runs in approximately 573.668084ms.

Assuming a one-second trick and a thirty-second shuffle time, a game of War with my nephew takes ~9m45s, give or take four minutes. The longest game would take thirty-eight minutes. After ten million simulated runs, all finished within forty-two minutes.

My nephew likes to add jokers to the deck. Statistically, this adds a minute to play time.

With two-second tricks and forty-five seconds of shuffling, you’re looking at a sixteen minute game, give or take six and a half minutes on average. Never more than an hour.

If you set a fifteen minute limit, you’ll finish about ninety percent of your games before time runs out.

There are two morals to this story:

One: if a five year old challenges you to a game of War, the odds are better than you would expect that it will be mercifully short.

Two: The only way to win is to play a different game.


Source Code

Available on GitHub.

*Nephew maybe reading this when you’re older: all time spent with you is great, but not all of it of equal value. I, too, loved War when I was your age, and it was a delight to play it with you once. Once was enough. How about a game of Cribbage?


← AI Couldn't Help But Wonder Digital Zen Gardens →