Game of Nim ( Due 18 Jan 2013 )

Nim is a game of strategy played between two players. The players take turns in removing one or more counters from a given heap of counters among several distinct heaps. In normal play the player who takes the last counter wins. In the more common variation Nim (sometimes called misere game) the player who takes the last counter loses.

The best strategy for this game was given by C. L. Bouton in 1901. It is defined in terms of the nim-sum. Supposing there are three heaps - A, B, and C having a, b, and c counters. The nim-sum is given by

nim-sum = a ⊕ b ⊕ c

where ⊕ is the xor operator.

The first player who starts with a non-zero nim-sum will always win the game, if he knows the strategy. On the other hand, if the player starts with a zero nim-sum, he will lose if his opponent knows the strategy.

Let us assume that you are starting with a non-zero nim-sum X. Then compute the individual nim-sums with each heap as follows.

a ⊕ X = p

b ⊕ X = q

c ⊕ X = r

Find the first heap where the individual nim-sum is smaller than the heap size. Let us say that (q < b), then subtract or take from the second heap (b - q) counters, leaving q counters behind. If you now compute the nim-sum of all the heaps it will be zero. In your subsequent moves always remove counters such that the starting nim-sum is zero for your opponent.

Let us say the three heaps A, B, and C have 8, 13 and 5 counters. The nim-sum X is

X = 8 ⊕ 13 ⊕ 5 = 0

You will lose this game. In another game, you have three heaps of 9, 7, and 12 counters. The nim-sum X is

X = 9 ⊕ 7 ⊕ 12 = 2

Now compute the nim-sum with each individual heap

p = 9 ⊕ 2 = 11

q = 7 ⊕ 2 = 5

r = 12 ⊕ 2 = 14

Your winning move is to remove (7 - 5 = ) 2 counters from heap B, leaving 5 counters in that heap.

The end game will depend whether it is normal play or the misere game. In normal play you want to end up with an even number of heaps of size 1. In the misere game you want to end up with an odd number of heaps of size 1.

The program that you will write will be called Nim.py. You will read a file called nim.txt. The first line in nim.txt is the number of data sets n that you will have. This will be followed by n lines of data. Each line of data of will have 2 or more numbers. These numbers represent the number of counters in each heap. The numbers on a given line will be separated by one or more spaces. If the nim-sum of a given line of data is zero your program will output

Lose Game

Otherwise, your program will output the first heap from which you can remove the number of counters according to the strategy outlined above.

Remove 2 counters from Heap 2

If this was the data file (nim.txt) that was given to you:

4
3 4 5
8 13 5
123 675 296 864 917 532
9 7 4 12
Then your output will be as follows:
Remove 2 counters from Heap 1
Lose Game
Remove 239 counters from Heap 3
Remove 6 counters from Heap 2

The file that you will be submitting will be called Nim.py. We will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to the coding conventions discussed in class. The file will have a header of the following form:

#  File: Nim.py

#  Description:

#  Student's Name:

#  Student's UT EID:
 
#  Course Name: CS 313E 

#  Unique Number: 53260

#  Date Created:

#  Date Last Modified:
You do NOT have to submit the nim.txt file. We will be testing your code against ours.

Use the turnin program to submit your Nim.py file. The TAs should receive your work by 11 PM on Friday, 18 January 2013. We will be looking for clean logic and good documentation. There will be substantial penalties if you do not adhere to the guidelines.

References

  1. Article on Nim in Wikipedia