Skip to main content

如何玩尼姆

逐步教學

第 1 步,共 7 步

堆介紹

Nim is one of the oldest strategy games in mathematics. The board consists of several heaps (also called piles or rows) of objects.

In this standard configuration there are three heaps containing 3, 5, and 7 objects. Two players take turns removing objects. The rules are simple, but the strategy runs deep.

第 2 步,共 7 步

移除物件

On your turn you must choose exactly one heap and remove one or more objects from it. You can take as many as you like from that heap -- even the entire heap -- but you cannot take from more than one heap per turn.

Here Player 1 has removed 2 objects from heap 3 (reducing it from 7 to 5), and Player 2 has responded by removing 1 object from heap 2 (reducing it from 5 to 4).

第 3 步,共 7 步

最後拿的人輸

This version uses misere rules: the player who is forced to take the last object loses.

The goal is to leave your opponent with no choice but to take the final object. In the position shown, only one object remains in a single heap. Whoever's turn it is must take it -- and loses the game. Plan ahead to avoid being stuck in this spot!

第 4 步,共 7 步

Nim-Sum 策略

The key to perfect Nim play is the Nim-sum -- the bitwise XOR of all heap sizes.

- Compute the XOR of every heap: 3 XOR 5 XOR 7 = 1.

- If the Nim-sum is non-zero on your turn, you can always make a move that leaves your opponent with a Nim-sum of zero -- a losing position for them.

- If the Nim-sum is already zero on your turn, any move you make will leave a non-zero Nim-sum, giving your opponent the advantage.

In misere Nim the strategy flips in the endgame: when all heaps are 0 or 1, you want to leave an odd number of heaps with one object so your opponent takes the last.

第 5 步,共 7 步

正常模式與反向模式

Default Nim is misere — taking the last object loses. In normal mode, taking the last object wins. This completely changes the optimal strategy.

In misere, you want to leave your opponent with the last object. In normal mode, you want to be the one taking the last object. The Nim-sum strategy also flips: in normal mode, you always want to leave a Nim-sum of zero, with no endgame exception.

This board shows a game in normal mode (misere: off). Notice how the same position requires a different approach when the goal is reversed.

第 6 步,共 7 步

起始配置

Choose different pile arrangements to vary the game:

- Traditional (1-3-5-7): The classic Nim setup with 4 heaps and 16 total objects.

- Standard (3-4-5): Three balanced heaps for a quicker game.

- Quick (2-3-5): A fast variant that finishes in fewer moves.

- Pyramid (1-2-3-4-5): Five heaps in ascending order — the most complex starting position with 15 objects.

This board shows the Pyramid configuration. With 5 heaps, there are far more possible moves each turn, making the Nim-sum calculation more challenging.

第 7 步,共 7 步

遊戲結束

The game ends when all heaps are empty. The player who was forced to take the very last object loses.

In this example Player 1 took the final object from the last remaining heap and lost the game. Player 2 wins!

With perfect play from the starting position of [3, 5, 7], the first player can always force a win. Try it out!

準備好玩尼姆了嗎?