Strategy - using binary algebra

The principles

We use the binary representation, also called base 2, of numbers and binary exclusive or operation on two numbers, which is to make exclusive or on bits of same rank in the binary representation ( an exclusive or between two bits gives 0 if the two bits are identical, and 1 otherwise). For example 5 is represented in binary by 101, 3 is represented by 011, where the result of binary exclusive or is 110 that gives 6 in decimal. We denote ^ the operator binary exclusive or, as in computer languages, giving: 5 ^ 3 = 6.

Evaluation of the situation

The evaluation of the game situation depends on the end-game option: option one is forced to pick up the last object loses called "misère", or one option that can pick the last object wins called "normale".

Call n1, n2, n3 and n4 the number of matches of rows. We begin by calculating:

s = n1 ^ n2 ^ n3 ^ n4

In the option called "normale", the situation is winning if s = 0.

In the option called "misère", with at least one number greater than 1 the situation is winning if s = 0, otherwise the situation is winning if s = 1.

Searching winning strokes

If the game is in winning situation, there is no possibility of finding a winning stroke. We thus place only when the situation is not winning and there is still some matches.

We examine the rows in turn and first calculate:

di=ni-s ^ni.

In the option called "normale" and the option called "misère" with several rows with more than one match, if di is greater than 0, to remove this number from the row i is a winning stroke.

In the option called "misère" with a single row with more than one match, the winning stroke is to remove from this row all matches except one if di = ni, and else all matches (so you leave an odd number of rows with one match).

In the option called "misère" with no row with more than one match, to remove the match from a row having one match is a winning stroke (the position is supposed to be not winning and the game unfinished , there are 2 or 4 rows with one match).