Nim — Classic Stone Removal Game vs Computer
Take 1-3 stones from a pile. The player who takes the last stone wins. Play vs an optimal AI.
How to play
- You and the AI take turns removing 1, 2, or 3 stones from the pile.
- The player who takes the very last stone WINS.
- Optimal strategy: always leave a multiple of 4 stones after your turn.
- Click 'Take 1', 'Take 2', or 'Take 3' to make your move.
- Click 'New Game' to start with a fresh random pile.
Take 1, 2, or 3 stones from a pile of 15–21. The player who takes the last stone wins. The AI plays optimally — can you find the winning strategy?
How it works
Nim is one of the oldest and most studied combinatorial strategy games. In this version, a single pile of stones sits between you and the computer. On each turn, a player must take 1, 2, or 3 stones from the pile. The player who takes the very last stone wins.
The game has a precise mathematical solution based on the size of the pile. If the pile has a number of stones that is a multiple of four (4, 8, 12, ...) when it is your turn, you are in a losing position against an optimal opponent no matter what you do. Otherwise, you can always win by taking enough stones to leave a multiple of four for your opponent.
The AI in this widget plays the optimal strategy, so it will beat you every time if you start from a losing position. The game is therefore as much a puzzle as a competition: figure out the pattern, apply it consistently, and you can beat the AI whenever you move first from a non-multiple-of-four pile.
Nim is a fantastic introduction to game theory, the concept of P-positions (previous-player wins) and N-positions (next-player wins), and how perfect information games can be solved by backward induction.
Worked example
Win from a pile of 13 stones
- The pile starts at 13 stones. Note that 12 is the nearest multiple of 4 below 13.
- Take 1 stone on your first turn, leaving 12 (a multiple of 4) for the AI.
- Whatever the AI takes (1, 2, or 3), you take enough to bring the total removed that round to 4. If the AI takes 2, you take 2; if it takes 3, you take 1.
- After each exchange of moves the pile decreases by 4: 12, 8, 4.
- When 4 stones remain and it is the AI's turn, it must leave you 1, 2, or 3 — and you take the last stone(s) and win.
You win by always leaving multiples of 4 for the AI.
Common mistakes to avoid
- Taking stones at random without checking whether your move leaves a multiple of 4 for the opponent.
- Forgetting that the goal in this normal-play version is to take the last stone — do not confuse it with misere variants where the last stone loses.
- Assuming the AI makes mistakes: the AI plays optimally, so if you are in a losing position the only way to win is to hope the game resets to a favourable pile size.
Key terms
- Nim
- A mathematical strategy game in which players alternately take objects from a pile (or piles); the player who takes the last object wins (normal play) or loses (misere play).
- P-position (losing position)
- A board state where the player whose turn it is loses with optimal play by both sides; in single-pile Nim with 1-3 take limit, these are multiples of four.
- N-position (winning position)
- A board state where the player whose turn it is can guarantee a win with correct play.
Frequently asked questions
- What is the winning strategy?
- Always leave a number of stones that is a multiple of 4 after your turn. If your opponent plays optimally, the player who inherits a multiple of 4 is in a losing position.
- Does the AI always win?
- The AI plays the optimal Nim strategy. You can still win if you start in a position where the stone count mod 4 is not 0 — i.e., immediately take the right number.