the game of nim

22
Benjamin Casey C S 329 E Spring 2009

Upload: talor

Post on 15-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

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” - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Game of  Nim

Benjamin CaseyC S 329 ESpring 2009

Page 2: The Game of  Nim

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

Page 3: The Game of  Nim

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

Page 4: The Game of  Nim

http://www.youtube.com/watch?v=oxBghtQ8McA

Page 5: The Game of  Nim

http://en.wikipedia.org/wiki/Nimrod_(computing)

Page 6: The Game of  Nim

Player 1 takes 1 from heap 2

Player 2 takes 1 from heap 2

Player 1 takes 1 from heap 1

Player 2 loses

Page 7: The Game of  Nim

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

Page 8: The Game of  Nim

1 01 1

= 0 1

1 01 0

= 0 0

Page 9: The Game of  Nim

Nim-sum obeys commutative and associative laws of addition

Also obeys another property: x + x = 0

Page 10: The Game of  Nim

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

Page 11: The Game of  Nim

Nim-sum = 01 + 10

= 11

Nim-sum = 01 + 01

= 00

Nim-sum = 01

Nim-sum = 00

Page 12: The Game of  Nim

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

Page 13: The Game of  Nim

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

Page 14: The Game of  Nim

S = 10 + 11 + 100 = 101X1 = x1 + S = 10 + 101 = 111X2 = x2 + S = 11 + 101 = 110X3 = x3 + S = 100 + 101 = 001

Thus k = x3 and you want to remove items such that y3 = X3 = 001

T = 10 + 11 + 01 = 00

Page 15: The Game of  Nim

T = 0 + TT = (S + S) + TT = S + (x1 + … + xn) + (y1 + … +

yn)T = S + (x1 + y1) + … + (xn + yn)T = S + 0 + … + (xk + yk) + … + 0

T = S + xk + yk

Page 16: The Game of  Nim

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

Page 17: The Game of  Nim

S = 101x1 = 010x2 = 011x3 = 100

101100001 = S + xk

y3 = 001

Page 18: The Game of  Nim

T = S + xk + yk

T = S + xk + (S + xk)T = (S + S) + (xk + xk)

T = 0

Page 19: The Game of  Nim

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

Page 20: The Game of  Nim

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.

Page 21: The Game of  Nim

Player 1 can move so that an odd number of heaps of size 1 remain

Now T = 1. This is the only point where misère strategy differs from normal strategy.

Player 2 is forced to take the last object, losing the misère game.

Page 22: The Game of  Nim