Searching winning strokes

As explained in help, a simple technique to evaluate the situation is to calculate three sums:

  • S1 sum of all matches,
  • S2 sum of the integer division by 2 of the number of matches in each row,
  • S3 sum of the integer division by 4 of the number of matches in each row.

Note that only the even or odd character of each sum enter into account, and you can adopt other techniques to obtain it, for instance count the number of odd results.

If S1, S2 and S3 are even, the situation is winning, and you can not find a winning stroke, you will leave the game in a non-winning situation, and your only hope is that your opponent makes a mistake, try to put the game in an unusual situation for him ...

If you have found the game in a non-winning situation which is not the finishing state or pre-finishing state (see states in help), the best stroke is a stroke that put again even S1, S2 and S3. This will require that the strokes changes the odd sums while keeping even the sums that were already.

You have to examine in each row if you can achieve this result by removing matches. Mathematicians have proved that there is always at least one solution, if you can not find a solution there is probably a miscalculation.

To help you, here are possible winning strokes in seven possible combinations of sum parities (it is indicated by E as even or O as odd the results in order S3, S2, and S1):

For more information you will find a very good article on Nim games in the English version of Wikipedia.