260 likes | 461 Views
Benjamin Casey C S 329 E Spring 2009. The Game of Nim. Background. Variants played since ancient times; resemblance to Chinese “Jianshizi” or “picking stones” Current name and theory developed by C. Bouton of Harvard in 1901; name taken from German nimm meaning “take”
E N D
Benjamin Casey C S 329 E Spring 2009 The Game of Nim
Background • Variants played since ancient times; resemblance to Chinese “Jianshizi” or “picking stones” • Current name and theory developed by C. Bouton of Harvard in 1901; name taken from German nimm meaning “take” • Fundamental to Sprague-Grundy thm., combinatorial game theory
Basics • Consists of a number of “nim-heaps”, each containing one or more objects • 2 Players alternate taking 1 or more objects from any nim-heap • Normal play: Player who takes the last object wins • Misère play: player who takes the last object loses • Other variants
The Amazing Dr. Nim http://www.youtube.com/watch?v=oxBghtQ8McA
Nimrod http://en.wikipedia.org/wiki/Nimrod_(computing)
Simple Example Player 1 takes 1 from heap 2 Player 1 takes 1 from heap 1 Player 2 takes 1 from heap 2 Player 2 loses
Theory • Theory completely solved for any number of heaps/objects by C. Bouton • Based upon binary digital sum of heap sizes • also known as “nim-sum” • write the size of each heap in binary • add the sizes without carrying • simple rule of thumb: • column w/ even # of 1’s = 0 • column w/ odd # of 1’s = 1
Binary Digital Sum 1 0 1 1 = 0 1 1 0 1 0 = 0 0
Binary Digital Sum • Nim-sum obeys commutative and associative laws of addition • Also obeys another property: • x + x = 0
Strategy • Winning strategy: finish each move such that the nim-sum is zero • if the nim-sum is non-zero upon your move, it is always possible to move such that the resulting nim-sum is zero (but also possible to have a non-zero result) • if the nim-sum is zero upon your move, it is impossible to move such that the resulting nim-sum is zero
Simple Example Nim-sum = 01 + 10 = 11 Nim-sum = 01 Nim-sum = 01 + 01 = 00 Nim-sum = 00
Proof • Let {x1, …, xn} = sizes of nim-heaps before move • Let {y1, …, yn} = sizes of nim-heaps after move • Let S = nim-sum of x1, …, xn • Let T = nim-sum of y1, …, yn • If move was made in heap k: • xi = yi for all i ≠ k • xk > yk
Strategy • To determine which nim-heap k to play in: • Calculate xi + S for in; call this Xi • The heap with Xi < xi is the heap you want to play in • To determine how many items to remove from heap k • Move such that yi = Xi
Example S = 10 + 11 + 100 = 101 X1 = x1 + S = 10 + 101 = 111 X2 = x2 + S = 11 + 101 = 110 X3 = x3 + S = 100 + 101 = 001 Thus k = x3 and you want to remove items such that y3 = X3 = 001 T = 10 + 11 + 01 = 00
Proof • T = 0 + T • T = (S + S) + T • T = S + (x1 + … + xn) + (y1 + … + yn) • T = S + (x1 + y1) + … + (xn + yn) • T = S + 0 + … + (xk + yk) + … + 0 • T = S + xk + yk
Therefore…. • If S = 0 • T = 0 + xk + yk ≠ 0, regardless of move • If S ≠ 0 • Let d be the position of the most significant non-zero bit in S • Choose k such that the value of xk in position d is also non-zero (such a k must exist) • Move such that yk = S + xk
Example S = 101 x1 = 010 x2 = 011 x3 = 100 101 100 001 = S + xk y3 = 001
Proof • T = S + xk + yk • T = S + xk + (S + xk) • T = (S + S) + (xk + xk) • T = 0
Endgame • When the next move will result in no remaining heaps of size ≥ 2 • Normal play: Move such that an even number of heaps of size 1 remain • Misère play: Move such that an odd number of heaps of size 1 remain
Example: Normal endgame Player 1 can move so that an even number of heaps of size 1 remain (Note that this still follows the strategy that T = 0. ) Player 2 loses again.
Example: Misère endgame Now T = 1. This is the only point where misère strategy differs from normal strategy. Player 1 can move so that an odd number of heaps of size 1 remain Player 2 is forced to take the last object, losing the misère game.