The game of Nim
Nim is a mathematical game of strategy in which two players take turns removing (or “nimming”) objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap or pile. Depending on the version being played, the goal of the game is either to avoid taking the last object or to take the last object.
How to win at Nim
What we have to realise is that the player who is left with a pile of 3, 2 or 1 stones is going to be the winner, but the player who is left with 4 stones in the pile is definitely a loser.
So, the winning strategy of the first player is to try and leave the second player with a four stone pile.
If we had 5, 6 or 7 stones in the original pile, the first player could easily do this, by taking 1, 2 or 3 stones to leave the second player with 4 stones and a losing position.
Unfortunately, the initial pile is bigger and has ten stones.
The question now becomes how can the first player force the second player to leave him with 5, 6 or 7 stones – positions from which he (the first player) can win.
Luckily, the first player can do this by taking two stones from the initial ten stone pile, leaving 8 stones in the pile for the second player. However many stones the second player now takes, 1,2 or 3, he leaves 5, 6 or 7 stones in the pile from which the first player can take the necessary number of stones to leave the second player with the losing four stone position.