โ† Back to list

๐ŸŽฎ The Nim Game: The Secret XOR Strategy That Guarantees You Win

Imagine playing a simple game.

Md Johirul Islam ยท 2026-05-24 11:50 ยท 0 claps ยท 2.7 min read paywalled
#math #logic #puzzle #algorithms #xor
Open on Medium โ†—
Wiki topics: ๐Ÿ’ป ยท Programming ๐Ÿ“ ยท Mathematics

๐ŸŽฎ The Nim Game: The Secret XOR Strategy That Guarantees You Win

Imagine playing a simple game.

There are a few piles of stones on a table:

Pile A: 3 stones  
Pile B: 4 stones  
Pile C: 5 stones

Two players take turns.

On each turn, a player can remove any number of stones from exactly one pile.

The player who removes the last stone wins.

The Question

Is this game just luckโ€ฆ or can you guarantee a win?

At first glance, it feels random. You might try:

  • Taking the biggest pile
  • Removing as many stones as possible
  • Or just โ€œplaying smartโ€

But none of that works consistently.

And yet:

There is a perfect strategy. And it always wins.

The Hidden Structure

This game is not about intuition.

It's about binary.

Let's rewrite the piles in binary:

3 = 011  
4 = 100  
5 = 101

Now compute their XOR:

011 โŠ• 100 โŠ• 101 = 010

That's 2 (non-zero)

The Rule That Changes Everything

Here is the entire secret:

โœ… If XOR = 0 โ†’ Losing position โœ… If XOR โ‰  0 โ†’ Winning position

That's it.

That one rule determines the entire game.

Why XOR?

XOR measures something subtle:

Balance

When XOR = 0:

  • The piles are in a perfectly balanced state
  • No matter what move you make, you break that balance

When XOR โ‰  0:

  • The position is unbalanced
  • You can always make a move to restore balance

The Winning Strategy

Step 1:

Compute XOR of all piles

Step 2:

If XOR โ‰  0 โ†’ you are in a winning position

Step 3:

Make a move that makes:

New XOR = 0

Example (Winning Move)

We had:

3 โŠ• 4 โŠ• 5 = 2

Now your goal is to change one pile so the XOR becomes 0.

Let's try reducing 5:

3 โŠ• 4 = 7  
We want: 7 โŠ• X = 0  
So: X = 7

So instead of 5, we want 7 โ€” not possible.

Try another approach:

Target:

New value of pile = old XOR โŠ• old pile

For pile 5:

New = 5 โŠ• 2 = 7 โŒ (not smaller)

Try pile 3:

New = 3 โŠ• 2 = 1 โœ…

So change pile A from 3 โ†’ 1

New configuration:

1 โŠ• 4 โŠ• 5 = 0 โœ…

Perfect. You've forced a losing position on your opponent.

What Happens Next?

No matter what your opponent does:

  • They must change XOR from 0 โ†’ non-zero
  • You can always bring it back to 0

Eventually:

  • They are forced into the last move
  • You win

The Deep Insight

This is the same idea you saw before โ€” but used differently.

Intuition (Without Math)

Think of XOR = 0 as perfect balance

Your goal is simple:

Always give your opponent a balanced position

Because from a balanced state:

  • Every move creates imbalance
  • And imbalance can always be fixed

You become the one controlling the system.

Why This Feels Like Magic

Because the winning strategy is invisible.

Two players can make identical-looking moves โ€” but:

  • One is preserving balance
  • The other is slowly losing control

The difference is not the moveโ€ฆ

Itโ€™s the structure behind the move

Real-World Connection

This isn't just a game.

The same idea powers:

โœ… Cryptography

  • XOR used for secure encoding

โœ… Error correction

  • Recover missing data in storage systems

โœ… AI & Game Theory

  • Optimal decision strategies use hidden invariants

The One-Line Strategy

Calculate XOR. If itโ€™s not zero, make it zero. Repeat until you win.

Final Thought

The Nim Game teaches something profound:

Winning isnโ€™t about making clever moves. Itโ€™s about maintaining the right invariant.

Once you see the pattern, the game stops being a game.

It becomes inevitable.


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
89a7551c4c4d
slug
the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
url
https://medium.com/@johirbuet/the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
canonical_url
https://medium.com/@johirbuet/the-nim-game-the-secret-xor-strategy-that-guarantees-you-win-89a7551c4c4d
author_url
https://medium.com/@johirbuet
status
ok
fetched_at
2026-06-09 15:37:30