What is the Nim Game?
Nim is a two-player mathematical game of strategy in which players take turns removing objects from distinct heaps. 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.
Variants of Nim have been played since ancient times. The game is said to have originated in China (it closely resembles the Chinese game of "Jianshizi", or "picking stones"), but the origin is uncertain; the earliest European references to Nim are from the beginning of the 16th century.
Its current name was coined by Charles L. Bouton of Harvard University, who also developed the complete theory of the game in 1901, but the origins of the name were never fully explained. The name is probably derived from German nimm! meaning "take!", or the obsolete English verb nim of the same meaning. Some people have noted that turning the word NIM upside-down and backwards results in WIN.
Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.
XOR Tricks for Winning at Nim
Although it takes some high-level math to find the secret strategy of Nim, employing that strategy really only requires an understanding of binary numbers.
In order to win at Nim, you have to know how to use the binary operation XOR, which stands for "exclusive or". What XOR really means is "x XOR y = 1 if either x or y is 1, but not if they are both 1." So, 0 XOR 0 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, and 1 XOR 1 = 0. Or, more simply, the result of the XOR operation is 0 if both arguments are the same and 1 of the arguments is different.
For any given situation in Nim, there is a number which determines whether or not that situation is a losing one. To find that number, you have to perform the XOR operation on the number of objects in each row successively. For example, the position is:
So to see if this is a losing position, we have to XOR the number of objects in each row, as follows:
1 XOR 3 XOR 5
which when shown in binary is:
001 XOR 011 XOR 101
Now we have to XOR each digit in each number and put each result below the column for that digit. Let's start with the rightmost digits.
1 XOR 1 XOR 1 is the same as (1 XOR 1) XOR 1
1 XOR 1 = 0, and 0 XOR 1 = 1. So 1 XOR 1 XOR 1 = 1.
Now continue with the rest of the columns until we have a new binary number.
001 XOR 011 XOR 101 = 111
If all the digits of this final number are zero, the position is a losing position !!!!!!! If it's your turn and the position is a losing position, you're in trouble. However, in this example, the number is non-zero, so we can turn the position into a losing position for our opponent.
Let's take the number 111 that we got from XORing the rows and try to find a row which, when XORed with 111, gives us a lower number than the row previously had. Well, we know if we do 001 XOR 111 it will be greater than 1, and 011 XOR 111 will be greater than 3, so we must do 101 XOR 111, which is 010, or in decimal 2, and is less than 5. So in order to give your opponent a losing position, you just have to remove 3 objects from the row of 5, leaving 2.
The steps needed to win are:
- When it is your turn, convert the number of objects in each row into binary numbers and XOR them.
- If the resulting number is
0, there's not much you can do to win. If it isn't
0, XOR it with a row and make a move so as to leave that many objects in that row.
For more information about Mathematical theory of game, see Wikipedia.
Using the Code
I'm not going to be detailing the game step by step or anything like that, but I'll talk about a few specific elements.
The code demonstrates the work of the standard widgets in C#. The following code illustrates the process of dynamic creation and initialization of these widgets (for example: buttons, ComboBoxes, image, etc.)
cmbbox = new ComboBox;
for ( i = 0; i < 7; i++)
cmbbox[i] = new ComboBox();
cmbbox[i].SelectedValueChanged += new System.EventHandler(
FlagColor = true;
catch (System.IndexOutOfRangeException exc)
for (int i =
0; i < 7; i++)
rdm = new Random(unchecked((int)DateTime.Now.Ticks));
Number_Members_In_One_Row = (rdm.Next(30)+1);
for(int k=1; k<=Number_Members_In_One_Row; k++)
After the above-stated code, we shall see 7
In the same way, methods
Fill_Images() create "heaps" with pencils.
Image image = Image.FromFile("GrayPen.bmp");
images = new PictureBox;
for(int i=0; i<7; i++,x+=107)
images[i] = new PictureBox ;
for(int j=0; j<30; j++)
images[i][j]= new PictureBox();
Image image = Image.FromFile("GrayPen.bmp");
for(int i=0; i<7; i++)
new_number_elements = cmbbox[i].Items.Count;
if(old_number_elements < new_number_elements)
for(int j=old_number_elements; j < new_number_elements;
else if(old_number_elements > new_number_elements)
PictureBox img = new PictureBox();
The program supports an opportunity for game of two players (one against another), and a mode of game of the person with a computer. The following code illustrates strategy of a computer (if we have, of course, chosen an option to play with a computer):
private void ComputerGame()
int  arr = new int;
for(int i= 0;i<7; i++ )
for(int k= 0;k<7 && tryResultElements!=0 ; k++)
for (n=1; n<=numElements; n++)
tryResultElements = numElements-n;
for(int m= 0;m<7; m++)
Image imageRed = Image.FromFile("RedPen.bmp");
SumOfImages = SumOfImages-num_choos_pens;
0;i < num_choos_pens; i++, j--)
switch(MessageBox.Show("New Game?","The game" +
"is over.Red won",MessageBoxButtons.YesNo))
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.