Nim Game Analyzer

XOR strategy for Nim

CalculatorsFreeNo Signup
4.4(305 reviews)
All Tools

Loading tool...

About Nim Game Analyzer

A Nim game analyzer computing the Nim-value (XOR of all heap sizes) to determine winning and losing positions. Shows optimal moves, explains the strategy based on binary XOR, and handles multi-heap Nim. Client-side.

Nim Game Analyzer Features

  • Nim-value (XOR)
  • Win/loss check
  • Optimal moves
  • Multi-heap
  • Strategy explanation
Nim: players alternate removing any number of objects from one heap. Last to move wins. Strategy: XOR all heap sizes. If XOR=0, you're losing (P-position). If XOR≠0, you can win (N-position) by making XOR=0. Discovered by Sprague (1935) and Grundy (1939).

How to Use

Enter heap sizes:

  • Nim-value: XOR of heaps
  • Win/Lose: Position type
  • Moves: Optimal play

XOR Strategy

Nim-sum = h₁ ⊕ h₂ ⊕ ... ⊕ hₖ. If 0: every move makes it >0 (losing). If >0: there exists a move to make it 0 (winning). The move: reduce heap i to hᵢ ⊕ nim-sum (if this is smaller).

Variants

  • Misère Nim: last to move loses. Strategy: same XOR logic, but switch when all heaps ≤1
  • Wythoff's game: remove from one pile or equal from both
  • Fibonacci Nim: can remove up to 2× previous

Step-by-Step Instructions

  1. 1Enter heap sizes.
  2. 2Compute Nim-sum.
  3. 3Check win/lose.
  4. 4Find optimal move.
  5. 5Explore variants.

Nim Game Analyzer — Frequently Asked Questions

Why does XOR work for Nim?+

XOR has key properties: a⊕a=0, a⊕0=a, associative, commutative. If nim-sum=0, any move changes one heap, making nim-sum≠0 (can't XOR to 0 by changing one term). If nim-sum≠0, reduce the right heap to make XOR=0.

What's the connection to binary?+

XOR is bit-by-bit addition mod 2. Each bit position is an independent sub-game. Nim-sum=0 means every bit position has even total — a balanced position. Any move unbalances at least one bit.

Does this work for all impartial games?+

Yes! The Sprague-Grundy theorem says EVERY impartial game (both players have same moves available) is equivalent to a Nim heap. The Grundy value works like the heap size. This is the foundation of combinatorial game theory.

Share this tool: