Toolstia / Nim game
Nim game
Nim is a simple strategy game with a deep mathematical solution. Take turns removing any number of objects from one heap. In the normal game, whoever takes the last object wins. You can also play the misere version, where taking the last object loses.
Your move. Take any number of objects from one heap.
Players take turns removing objects from a single heap. Whoever takes the last object wins, or loses in misere. The computer plays perfectly whenever it can, so you can only win by making no mistakes yourself. Starting from 3, 4 and 5, the player who moves first can force a win in the normal game.
How it works
- Choose the normal or misere rules.
- Press a Take button to remove that many objects from a heap.
- The computer replies. Take the last object to win, or avoid it in misere.
The rules
There are three heaps of 3, 4 and 5 objects. On your turn you take at least one object from a single heap. You move first.
The secret
Write each heap size in binary and combine them with XOR. A position with a result of 0 is losing for the player to move. The computer uses this rule.
Can you win?
From 3, 4 and 5 the first player can force a win, so you can win if you make exactly the right moves.
Misere
The strategy is the same until only heaps of size 1 are left, where the aim flips.
Questions
Does the computer ever make mistakes?
Only when it is in a losing position, where it makes a random move. Everything happens in your browser, and nothing you enter is sent anywhere.
What is the first winning move?
Take 2 from the heap of 3, leaving 1, 4 and 5, which combine to 0.
Why is it called Nim?
The name is usually traced to an old German word for take.