🎮 How to Lose Every Game: The Mathematics of Losing Strategies

Kumkum RajKumkum Raj
5 min read

✨ Introduction: What If Losing Is a Mathematical Certainty?

You’re sitting across from a friend.
A table holds three small piles of coins — maybe 5, 3, and 1.

It’s your turn.

You can remove any number of coins from any single pile.
The only rule: whoever removes the last coin wins.

You make your move, confident.
But a few turns later… you lose.

What just happened?

Welcome to Nim — a deceptively simple game where mathematics decides the winner long before the first move is made.

This blog will guide you through the beautiful world of combinatorial game theory, showing how a simple operation called XOR, a concept called Grundy numbers, and tools from discrete mathematics allow you to analyze, solve, and even dominate such games — all without needing to guess.

Let’s start with the basics.


🧮 Part 1: The Game of Nim — Simple Yet Surprising

🧩 The Rules

Nim is played with any number of piles, each containing one or more tokens.

  • Players take turns.

  • On your turn, you can remove any number of tokens (at least one) from any one pile.

  • The player who takes the last token wins.

Try playing a few times with a friend. You’ll notice something strange:

  • Some starting positions are easy wins

  • But others feel like traps, even if you don’t know why.

Let’s explore an example:

Starting Piles: 5, 3, 1
Your move.

Can you force a win?

To answer that, we’ll leave behind guesses — and turn to math.

I’ve created a live, fully interactive Nim game demo on CodePen. You can view and test it here: Nim Game


💻 Part 2: Binary Numbers and XOR — The Hidden Gameboard

What if I told you that all the secrets of Nim lie in binary numbers?

Let’s take our piles: 5, 3, 1
Now convert each to binary:

PileDecimalBinary
15101
23011
31001

Now compute the XOR (⊕) of all three values:

101  
⊕ 011 → 110  
⊕ 001 → 111

Final result: 111 (binary) = 7 (decimal)

🤔 What does this mean?

The rule is:

  • If the XOR of all pile sizes = 0, you are in a losing position

  • If XOR ≠ 0, you are in a winning position

So in this case, since 111 ≠ 0 → ✅ You can win!

But how?

By making a move that turns the XOR total to 0 — mathematically forcing your opponent into a losing position.

This is where Grundy numbers come in.


🧠 Part 3: Grundy Numbers — How Games Are Ranked

The idea of Grundy numbers comes from a powerful result in game theory called the Sprague–Grundy theorem. It tells us:

Any position in any impartial game is equivalent to a pile in Nim of size n, where n is the Grundy number.

Let’s define it more clearly:

  • The Grundy number of a position = the minimum non-negative integer (mex) not appearing in the Grundy numbers of its options.

  • In Nim, the Grundy number of a pile of size n is just n itself.

So:

Pile SizeGrundy NumberBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10101010

This rule is simple for Nim, but it becomes profound when applied to other games like Wythoff’s Game, Chomp, or Domineering, where computing Grundy numbers requires recursion or dynamic programming.


🔁 Part 4: Recursion, Induction, and Game Trees

Every position in Nim can be represented as a node in a graph:

  • Nodes = game states (pile sizes)

  • Edges = legal moves

  • Leaves = end positions (no more moves)

This turns the game into a tree, and Grundy numbers flow upward from the leaves — calculated using recursion:

Grundy(n) = mex{ Grundy(n – 1), Grundy(n – 2), ..., Grundy(0) }

Where mex is the minimum excluded value.

You can prove patterns using mathematical induction, and optimize them using memoization — linking the theory to real-world AI game solvers and dynamic programming.


🕹️ Part 5: Strategy in Action

Suppose you're facing this starting position:
Piles: 6, 4, 2

Binary:

PileBinary
6110
4100
2010

Now compute:

110 ⊕ 100 = 010  
010 ⊕ 010 = 000 → Losing position

If your opponent gives you this position and knows the strategy, you’ve already lost — unless they make a mistake.


📚 Part 6: Beyond Nim — Other “Losing Games” Solved by Discrete Math

Here’s where it gets exciting. Grundy logic works for many other games:

GameDescriptionMath Behind It
ChompEating an L-shaped chocolate barPositional logic, game graphs
Wythoff'sTwo piles, remove from one or bothBeatty sequences, irrational cuts
KaylesKnock over adjacent pinsDynamic programming + Grundy
Take-AwayRemove ≤ k coins per moveModulo arithmetic, recursion

These games are impartial, deterministic, and have perfect information — perfect for discrete math modelling.

Conclusion: Lose Smarter, Win Always

Once you understand Nim, you’ll start noticing patterns in everything:

  • When to speak last in a debate

  • When to walk away from a negotiation

  • When to play… and when to pass

Because in mathematics, just like in life, sometimes the best way to win… is to force the other person to lose first.

And the strategy?

It’s all in the math.

📘 References

  • Berlekamp, E., Conway, J., & Guy, R. – Winning Ways for Your Mathematical Plays

  • Gardner, M. – Mathematical Games, Scientific American

  • Sprague–Grundy Theorem – Wikipedia

  • Nim Game – Brilliant.org

  • 3Blue1Brown – Binary and Nim

  • MIT OpenCourseWare – Discrete Mathematics for Computer Scientists

  • OpenAI – AI-assisted conceptual explanation and code for Nim Game

20
Subscribe to my newsletter

Read articles from Kumkum Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kumkum Raj
Kumkum Raj