Tuesday, January 24, 2017

The Game of Nim

Hello all-

Before I get to the game of Nim, there are a few things you all will need to know in order to understand my presentation.

Review

1. Binary Numbers

For the CS majors, and probably some of the Math majors, this will be review. If so, feel free to skip. For those who don't know (or don't remember) binary numbers, you can represent any base-10 number (Example: 5, 18, 135) using only zeroes and ones. To do so, you'll need to break the base-10 number down into powers of two.  In binary, the rightmost digit is representative of 20, the second rightmost digit is 21, the third rightmost is 22, and so on.

Example:

The number 5 can be written as the sum of 4 + 1, or 22 + 20.This means, that 5 in binary is 101, or 1*22 + 0*21 + 1*20.

Example:

The number 18 can be written as the sum of 16 + 2, or 24 + 21. Then, in binary, we would represent 18 as 10010.

Example:

Finally, the number 135 can be written as the sum of 128 + 4 + 2 + 1, or 27 + 22 + 21 + 20. Hence, in binary, 135 is 10000111.

Got it? Good. If not, Google it.

2. Exclusive-Or

The second thing you'll need to remember is the exclusive or operation. A simple way to remember this is to think of it as arithmetic mod 2. For example, 0 ⊕ 0 = 0, because 0+0 = 0 mod 2. Likewise, 1 ⊕ 1 = 0, because 1+1 = 2 = 0 mod 2. Be careful! One thing to be aware of is 1 ⊕ 1 ⊕ 1 = 1, since 1+1+1 = 3 = 1 mod 2. If this is confusing, don't worry too much. I'll spend some time on this in class.

Also feel free to Google this, or leave a question in the comments- I'll try to answer it in class.

3. N and P Positions

Make sure that you're comfortable with the idea of N and P positions, as we'll use them to analyze Nim. If you're iffy on this, review Natasha's blog post below.


On to Nim!

Ok, so the way Nim works is similar to the subtraction game we played with Natasha in class.

Rules:

  1. Start with three piles of objects, call them coins.
  2. On your turn, you may remove any number of coins from any pile, but only that pile.
    • Example move: Take one coin from one pile, leaving the rest in the pile.
    • Example move 2: Take an entire pile, so that there are only two piles left.
  3. The game is over when all the coins are gone, or your three piles each have zero coins in them.
  4. The person who took the last coin (or pile of coins) wins.
You can play Nim with three piles here by clicking on Level 4. We will also probably play a game as a class during my presentation, just to clear everything up.


The Big Question: When in Nim are you in a P-Position, and when are you in an N-Position?


Before I tell you the answer, feel free to write some conjectures in the comments!

Done? Okay.

It turns out you can calculate whether or not a situation in Nim is a P-Position or N-Position, using binary numbers and exclusive or to calculate the Nim Sum.

Then what's the Nim Sum?

The nim sum is the binary representations of the number of objects in your piles, exclusive-ored together.

Huh?

So, let's say you have three piles in your game, with 13, 12, and 8 objects respectively. Then in binary, you have 1101 (13), 1100 (12), and 1000 (8) objects in each pile. To exclusive-or them, we can line them up vertically, and do our mod 2 arithmetic.


So, our answer is 1001, which in base-10, equals 9. This 9 is our nim sum!

How does that relate to P-Positions and N-Positions?

It turns out, that in any given situation, if your nim sum equals zero, you're in a P-Position. Now let's prove it.


Bouton's Theorem


Bouton's Theorem states that a position in Nim, (x1, x2, x3) is a P-Position if and only if the nim sum of its components is zero, x1 ⊕ x2 ⊕ x3 = 0.

In other words, this is the claim that we need to prove.

Proof

Just like in our Subtraction game proof, let's start our proof by building some sets, and then we'll show that they consist of all the situations that satisfy the definition of P and N Positions.

$$\textit{Let} \ P = \{(x_1, x_2, x_3)| x_1 \oplus x_2 \oplus x_3 = 0\}$$

$$\textit{Let} \ N = \{(x_1, x_2, x_3)| x_1 \oplus x_2 \oplus x_3 > 0\}$$

Also note that there is only one terminal position in Nim, (0,0,0), when the last of the coins has been taken.

Step 1: Show all terminal positions are in P

    This is fairly obvious. We have one terminal position, (0,0,0), and 0 ⊕ 0 ⊕ 0 = 0,
so it's in our set P.

Step 2: For any n ∈ N, you can move to a situation in P.

    Let (n1, n2, n3) be a situation  N. Then n1 ⊕ n2 ⊕ n3 > 0.

    Hence our nim sum is greater than zero.

    So, set up your nim sum columns, and compute your nim sum in binary. Go to the leftmost column in your sum that has a 1. We know that there is one, since our nim sum is greater than zero. Pick one of the piles (n1, n2, or n3) that also has a 1 in that column. Make that 1 a 0, and adjust the rest of the columns of that pile so that you have all 0's in your nim sum.

Note: We will do examples of this in class, because I know it's confusing to read about.

    How do we know we made a legal move? Since the leftmost column is being reduced from a 1 to a 0, we know we're taking objects away from that pile, even if we change the rest of the number to 1's. Hence, you can always take away some number of objects from one pile to move into a P-Position if your nim sum is > 0.

Step 3: For any p ∈ P, you have to move to a situation in N.

    Let (p1, p2, p3) be a situation  P. Then p1 ⊕ p2 ⊕ p3 = 0.

    Then when you set up your nim sum columns, the nim sum is comprised entirely of zeros. Remember: another way to think of exclusive or is arithmetic mod 2. This means that there is no way to change only one pile so that you keep zeros in all the columns. Since you can only change the numbers from zeros to ones and vice versa, changing only one pile will always change one of your nim sum columns from a zero to a one, creating a nim sum > 0.

    Hence, from any position in P, you must move to a position in N.


DONE!

Conclusion

What does all this mean? Basically, it means that for any situation in Nim, it is always possible to figure out whether or not you're in a P-Position or an N-Position. Why is this important? We'll discuss in upcoming classes how Nim is representative of all combinatorial impartial games, but that's beyond the scope of my presentation. Throughout this post, we've discussed and analyzed the game of Nim, using nim sums, and we've talked about how they relate to N and P Positions. We've also proved Bouton's Theorem true, using the same model discussed in class. I'll leave you with some ideas for discussion topics, either in the comments or class:

  • Misere Nim: a variation where the last person to take coins loses
  • Other variations on Nim
  • Does Bouton's Theorem apply to Nim with more than 3 piles?
  • Anything you have questions on
Citations:

Bouton, Charles L. "Nim, A Game with a Complete Mathematical Theory." The Annals of Mathematics 3.1/4 (1901): 35-39. Web.

Ferguson, Thomas S. "Game Theory." (n.d.): n. pag. Web. <http://myslu.stlawu.edu/~nkomarov/450/comb.pdf>.

"Theory of Impartial Games." The Mathematics of Toys and Games. N.p.: n.p., 2009. N. pag. Print.

13 comments:

  1. Hi Madison,

    I'm not sure how much you're planning on speaking about Misere Nim tomorrow, but I am curious to know how it applies to Bouton's Theorem. Since the rules are reversed from traditional Nim, would this mean that if the Nim sum of your components is 0 you would be in an N-position instead of a P-position? Or is it more complicated than that?

    Thanks,
    Gavin

    ReplyDelete
    Replies
    1. It turns out that Misere Nim isn't too difficult to understand once you get Normal Nim. What you can do in Misere Nim, is play like normal, until there is only one pile with more than one object in it. Then, on your turn, reduce that pile (with more than 1 object), so that you leave an odd number of piles with one object. So, if there are three piles (1,1,4), reduce the 4 pile to 1 (1,1,1), then your opponent will have to take one (1,1,0), you can take 1 (1,0,0), and your opponent will have to take the last one and lose.
      If you look at the sources listed for the presentation, Ferguson does a good job explaining Misere Nim in section 2.5.

      Hope this helps!

      Delete
  2. After reading through the presentation I am still a little shaky on using modular arithmetic with binary numbers. I am going to read it over a few more times to familiarize myself as much as I can, but in your presentation will you be able to provide some other examples of this? If so I think that would be super helpful.

    Thanks,
    Jack

    ReplyDelete
  3. I was wondering what restricting the amount of coins you can take from each pile to a certain number would do. I'm assuming the nim sums for the P-positions will be more than just zero, so what will those numbers be? Is there a pattern to it?

    Thanks,
    Katie

    ReplyDelete
    Replies
    1. Yes!! Great question. We'll see next week how we can "sum" disjoint games, and so each nim-pile can be thought of as a separate subtraction game (exactly like the one we talked about on day one, where you could remove exactly 2 or 3 coins from each pile). It'll turn out that the "nimber arithmetic" Madison discussed above and in her presentation today will be what governs how all of that works.

      Delete
  4. Hey Madison! This is my first time ever seeing binary numbers. However, I am trying my best to learn. So, I want to run this practice problem by you to see if I did it right and to see if my terminology is correct. (Arbitrary example: 11000 converting to base 10 would be (1 x 2^4) + (1 x 2^3) + (0 x 2^2) + (0 x 2^1) +(0 x 2^0) = 24 base 10 = 11000 base 2?
    Thanks,
    Greg

    ReplyDelete
    Replies
    1. That's right! I can spend some extra time on this during the presentation too if that would help!

      Delete
  5. Hey guys!

    I see that some people are asking about binary numbers! It may help to try to think about it in terms of integer division.
    If you take any given number, you can keep dividing it by 2 (until we reach 1). When the number we are dividing is odd we mark a “ 0 “ next to it. When the number we are dividing is even we mark a “ 1 “ next to it. Once we are done, we will have a vertical list of 0’s and 1’s and if we rewrite this list backward (that is, from the bottom up) we have our binary number! Let’s take 18 in Madison's example.
    18/2 = 9 (18 is even so we have 0)
    9/2 = 4 (9 is odd so we have 1)
    4/2 = 2 (4 is even so we have 0)
    2/2 = 1 (2 is even so we have 0)
    2/1 (1 is odd end so we have 1)
    Now, if we rewrite this list going from the bottom up, we get a binary number of 10010 !

    I hope this helps! :)
    - Haley

    ReplyDelete
    Replies
    1. This is great. Dividing is easier than working in exponentials for me. Although I can see where it would quickly become time consuming. I doubt the computing power required is any greater though??

      Delete
  6. I tried to see if Bouton's Theorem applies to Nim with more than 3 piles by doing an example with 4 piles and following the steps of the theorem outlined above. I had 12 (1100), 3 (0011), 11 (1011), and 8 (1000) objects in each pile. My original nim sum was 12 and I was able to move to a P position by following the theorem and removing all the elements in the 12 pile. According to the theorem, I now have to move to a situation in N. So I removed all of the elements in the 3 pile and came up with a nim sum of three (success). From here I followed the prescribed process to attempt to get a nim sum of zero, but it did not work. So, it seems that it is not true that for any n ∈ N, you can move to a situation in P when there is more than 3 piles (contradicting the theorem).
    I’m not sure if this is absolutely true but it was a good exercise to understand the theorem anyways!

    ReplyDelete
  7. If both players know the Bouton's proof, does the first or second player have the advantage? How would the game be effected if we added more players?

    ReplyDelete
    Replies
    1. Great question Mac. I believe that the player with the advantage depends on how many sticks the player begins with in each pile, as well as how many piles the player has.

      Theorem: A position is a losing position if and only if every power of two occurs an even number of times.

      This implies that if there are an even number of piles a player wants to force his/her opponent to having the nim sum mod 2 = 0. In the case that there are an odd amount of piles, a player should move to make the nim sum mod 2 = 1.

      Please confirm.

      Delete
    2. So you guys were on the right track- If both players know Bouton's Theorem, and play ideally, then who wins is really going to depend on the very first position of the game. If the nim sum of the very first piles = 0, then Player 2 is in luck, because he can guarantee a win, but if the nim sum > 0, then Player 1 can always win. It all comes down to how they start the game.

      Delete

Note: Only a member of this blog may post a comment.