The crucial factor for an optimal (winning) strategy is the nim-value. For details of how to calculate it, see Nim-Value. The circle-plus symbol used in this Demonstration indicates the operation of calculating the nim-value.
Snapshot 1: if at any point in the game, the nim-value is zero for a given player, the position is safe, which means that player can win with correct play
Snapshot 2: otherwise, the position is unsafe and the player will always lose if the other player plays correctly
Snapshot 3: to achieve a safe position, find a heap whose size is less than the min-value of with the nim-value of all the heaps; reduce that heap to , resulting in a nim-value of zero