Play Nim against the computer or a friend in Normal play or Misere, at three levels, with a winning-move hint and a nim-sum table that shows why the move works.
Turn Your turn
Stones left 12
Heaps 3, 4, 5
This session -
Saved wins -
This game counts yes
Tap a stone to pick it and every stone to its right in that heap, then press Take. Tapping the same stone again also takes. On a keyboard, arrow keys move between stones, Enter or Space picks and then takes, and Escape clears the pick. Showing the winning move, or using Undo against the computer, means the game is not counted in your results.
| Heap | Stones | Binary |
|---|
Nim is solved. In Normal play, XOR the heap sizes together to get the nim-sum. If it is 0 the player to move is losing against perfect play; if not, there is always a move that makes it 0, and that is the winning move. Misere uses the same rule until a move would leave only heaps of 0 or 1 stones; from there the winner leaves an odd number of single stones. Easy plays a random legal move. Medium plays the winning move about 70% of the time and a random legal move otherwise. Perfect always plays the winning move when there is one, and when the position is already lost it takes one or two stones from a random heap, which keeps the game going in case you slip.
As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.
There are several heaps of stones, drawn as rows. Players take turns. On your turn you remove at least one stone, and as many as you like, but all from one heap.
Tap a stone to pick it. You pick that stone and every stone to its right in the same heap, which is exactly what you would take; the page highlights them and says, for example, “Take 3 from heap 2”. Press Take to commit. Tapping the same stone a second time also takes. Escape clears your pick.
On a keyboard, the arrow keys move between stones, Home and End jump to the ends of a heap, and Enter or Space picks and then takes. Every stone is a labelled button, such as “Heap 2, stone 3 of 4”.
The setups are Classic 3, 4, 5, Four heaps 1, 3, 5, 7, Five heaps 1 to 5, and a random setup with three heaps of 1 to 7 stones, new every game. You can play the computer, going first or second, or hand the screen back and forth with a friend.
Show the winning move tells you the best move for the current position, or says plainly that there is none. Undo takes back your move and the computer’s reply. Both are fine for learning, but a game where you used either against the computer is not counted in your results.
Nim is solved, and the solution is short. Write each heap size in binary and add the columns without carrying, which is the XOR of the sizes. The result is the nim-sum.
Some positions you can check by hand with the Explain table on the page:
The levels. Easy plays a random legal move. Medium plays the winning move about 70% of the time and a random legal move the rest of the time, which makes it beatable but not silly. Perfect always plays the winning move when one exists. When the position is already lost for it, Perfect takes one or two stones from a random heap to keep the game going in case you slip.
How it is tested. The strategy is checked against a separate brute-force search that knows nothing about nim-sums: it tries every move from every position and records who wins. That search covers all 2,401 positions with up to four heaps of up to six stones, for both Normal play and Misere. In every one, the formula and the search agree, and the move the engine picks is legal and leaves the opponent in a lost position. The tests also play well over a thousand seeded games: the Perfect computer never loses from a winning position, whatever level it faces and from either seat, and a player who moves at random lost to it in all of 200 seeded games from a winning start, in each rule set.
Nothing is uploaded and there is no account. Your saved wins, counted separately for each rule set and level, are stored in your browser’s local storage as small whole numbers inside a small JSON object under one key. The current setup (rules, heaps, opponent, level and which seat you play) is kept in the page address so you can link to it. Clearing site data removes the saved wins.
Only games against the computer add to the saved wins, and only when you used neither the winning-move hint nor Undo. The “This session” tally beside the board counts the same unaided games against the computer, losses as well as wins. It is kept separately for each rule set and level, so after you change the rules or the level it shows that setting’s own count (and Copy result reports that setting’s count); two-player games have their own tally for each rule set. The tallies reset when you leave the page.
Everything runs locally in your browser.
For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.