111
13. Combinatorial Games Robert Snapp [email protected] Department of Computer Science University of Vermont Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 1 / 39

13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

13. Combinatorial Games

Robert [email protected]

Department of Computer ScienceUniversity of Vermont

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 1 / 39

Page 2: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

1 Two-Player Games of Perfect Information

2 Charles Bouton’s NimNim StrategiesNimtreesThe Secret of Nim

Parity Sums

3 Northcott’s Game

4 Wythoff’s Game

5 Kayles

6 Sprague-Grundy NumbersAnalysis of NimAnalysis of Whythoff’s GameAnalysis of Kayles

7 AppendixBinary NumbersPowers of Two

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 2 / 39

Page 3: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 4: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 5: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 6: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 7: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 8: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Two-player Games of Perfect InformationPlayers alternate turns.Each player has complete information about the current state.No elements of luck (e.g., no dice).No hidden information (e.g., no unexposed cards).Examples?

I Tic-Tac-Toe

I Dots and Boxes

I Sprouts

I Checkers (or Draughts)

I Chinese Checkers

I Chess

I Go

I Nim

I Northcott’s Game

I Whytoff’s Game

I Kayles

I Mancala & Bao

I Konane (Hawaiian Checkers)

I Othello (or Reversi)

I Hex (or Nash)

I Brigde-It

I Twixt

I Three-dimensional Chess

I Chinese Chess (Xiangqi)

I Go-Moku (5 in a row)

I Connect Four

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39

Page 9: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Charles Bouton’s NimA two-player game of perfect information, possibly of Chinese origin. It was analyzed byCharles Leonard Bouton in 1901.

A number of tokens are placed into a number of heaps (or rows).

Players alternate turns. During a turn, a player may remove an arbitrary number oftokens from a single heap.

The player who removes the last token from the board, wins.

In the related game, Nim misère, the player who removes the last token, loses.

For further reading, see

1 Martin Gardner’s, Hexaflexagons and Other Mathematical Diversions, University ofChicago Press, 1988, pp. 151–161.

2 Elwyn R. Berlekamp, John H. Conway, and Richard W. Guy, Winning Ways for yourmathematical plays, Vol. 1. Second Edition, A. K. Peters, Natick, MA, 2001,pp. 53–79.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 4 / 39

Page 10: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: Which tokens should she take?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 11: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: Which tokens should she take?Answer: The three red tokens (from the first heap).

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 12: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Bob’s turn: (He’s going down!) What might he do?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 13: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Bob’s turn: (He’s going down!) What might he do?Answer: Maybe he removes four blue tokens (from the second heap).

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 14: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: How does she respond?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 15: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: How does she respond?Answer: Smart Alice balances the remaining heaps by taking four tokens from thethird heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 16: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Bob’s turn: Poor Bob is not sure what he can do?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 17: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Bob’s turn: Poor Bob is not sure what he can do?Answer: He decides to take one token from the third heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 18: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: What should she do now?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 19: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Alice’s turn: What should she do now?Answer: She takes one token from the second heal: the copy-cat strategy.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 20: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sample Game: Alice vs. BobConsider an initial configuration having three heaps of size 3, 6, and 6, as shown.

Bob’s turn: Only two options exist. Both leave one token on the board for Alice totake. Thus, Bob resigns.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39

Page 21: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nim StrategiesBefore describing an optimal strategy for this game, let’s look at a few interestingcases.

1 What would you do, if it were your turn and all of the remaining tokens belong toa single heap? For example,

Answer: Remove the entire heap.2 What would you do if it were your turn, and there were two unequal heaps?

Answer: Remove the difference from the larger, making them equal.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39

Page 22: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nim StrategiesBefore describing an optimal strategy for this game, let’s look at a few interestingcases.

1 What would you do, if it were your turn and all of the remaining tokens belong toa single heap? For example,

Answer: Remove the entire heap.2 What would you do if it were your turn, and there were two unequal heaps?

Answer: Remove the difference from the larger, making them equal.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39

Page 23: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nim StrategiesBefore describing an optimal strategy for this game, let’s look at a few interestingcases.

1 What would you do, if it were your turn and all of the remaining tokens belong toa single heap? For example,

Answer: Remove the entire heap.2 What would you do if it were your turn, and there were two unequal heaps?

Answer: Remove the difference from the larger, making them equal.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39

Page 24: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nim StrategiesBefore describing an optimal strategy for this game, let’s look at a few interestingcases.

1 What would you do, if it were your turn and all of the remaining tokens belong toa single heap? For example,

Answer: Remove the entire heap.2 What would you do if it were your turn, and there were two unequal heaps?

Answer: Remove the difference from the larger, making them equal.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39

Page 25: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

List Representation

We adopting a list representation for each state. Let (1 2 3), correspond to the state

and let (2 2) correspond to the state

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 7 / 39

Page 26: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

NimtreesEarlier in the course we learned how trees can be used to enumerate (or count) thenumber of permutations (using a factorial tree), the number of sequences of cointosses (using a binary tree), and the number of different rolls of a pair of dice (using ahexary tree).

We now extend that idea to nim, and will enumerate every possible game that can beplayed from the state (2 2).

We first draw a root node and label it with the state (2 2). We shall call the first playerMax, and consider every possible move that Max can make. Max can

1 Remove one token from the first heap, yielding (1 2), or2 Remove two tokens from the first heap, yielding (2), or3 Remove one token from the second heap, yielding (2 1), or4 Remove two tokens from the second heap, yielding (2).

Since Case 3 is symmetric to Case 1, and Case 4 is symmetric to Case 2, we onlyconsider the first two cases.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 8 / 39

Page 27: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

We thus insert nodes for states (1 2) and (2) below the root node, (2 2), and connecteach to the root node with a green edge.

Next, we will consider the moves that the second player, called Min, can make fromeach of these two states.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 28: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

There are three moves that Min can make from the state (1 2): removing 1 from thefirst heap yields that state (2), 1 from the second yields (1 1), and 2 from the secondyields (1). There are only two moves that Min can make from the state (2): removingone token yields (1); removing 2, yields ( ): victory for Min.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 29: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

We continue to analyze each of the four active states in turn. Max can convert (2)into either (1) or ( ) (a win for Max). Max can convert (1 1) only to (1) (why?), and (1),on either subtree, to ( ), other wins for Max.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 30: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

Only two active nodes are left. Min can convert (1), on either subtree, to ( ), wins forMin. Note that in this full expanded nimtree, the root node is the initial state, andevery leaf node is an empty list, which we have colored to indicate the player whoremoved the last token.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 31: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

We will now prove that Min (the second player) can always win from the initialposition, (2 2), no matter how Max plays.

First we assign a score ofC1 to each game that Max wins, and a score of �1 to eachgame that Min wins. These values can be assigned to the leaf nodes, as shown.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 32: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

Next, we assign a score to node of the tree that reflects the outcome of the gameassuming that both sides play optimally. We will work from the bottom of the tree (leafnodes) to the top (root).

Given the state (1), Min can only remove the last token. Thus the score of any state(1) given to Min is �1, as shown.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 33: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

We continue to assign scores to nodes one step up the tree. Since Max wants tomaximize the score, given the state (2), Max would select () over (1). Thus the scoreof (2) should equal the maximum score of its descendants. This same rule applies ifno choice exists, as is the case for states (1 1) and (1).

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 34: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

Since Min wants to minimize the score, given the state (1 2), Min would select (1 1)over (2) or (1). Thus the score of (1 2) should equal the minimum score of itsdescendants. This same rule applies to the state (2) in the right subtree.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 35: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

All that is left is to assign a score to the root node, (2 2). Note that it is Max’s turn, sothe score of (2 2) should be the maximum score of its descendants. However, thelatter are equal to �1. Consequently, the score of the root node is also �1, so Min isdestined to win.

In more general contexts, Nimtrees are called Max-Min trees.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 36: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Nimtrees (cont.)

(2 2)(1 2) (2)

(2) (1)(1 1)

(1)()

(1)()

(1)() ()

() ()

C1

�1�1

C1 C1

�1

�1�1

C1 �1C1 C1

�1 �1�1

All that is left is to assign a score to the root node, (2 2). Note that it is Max’s turn, sothe score of (2 2) should be the maximum score of its descendants. However, thelatter are equal to �1. Consequently, the score of the root node is also �1, so Min isdestined to win.

In more general contexts, Nimtrees are called Max-Min trees.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39

Page 37: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Now you try!

Hint: Look for powers of 2 (1, 2, 4, 8, 16, 32, : : :).

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 10 / 39

Page 38: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Now you try!

Hint: Look for powers of 2 (1, 2, 4, 8, 16, 32, : : :).

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 10 / 39

Page 39: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 40: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 41: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 42: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 43: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 44: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 45: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 46: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 47: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 48: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimBefore you make a move, group each heap into unique powers of 2. For example, thestate (5 9 11 14) can be partitioned as,

Note that there are 3 groups of 8, 2 groups of 4, 2 groups of 2, and 3 groups of 1.Identify the powers of 2 with an odd multiplicity (here 8 and 1).Then find a move that leaves each power of 2 with an even multiplicity. Either,

1 remove 9 tokens from the second heap, or2 remove 9 tokens from the third heap, or3 remove 7 tokens from the fourth heap.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39

Page 49: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimThe first step is to resolve each heap into a sum of different powers of 2.

1 Find the largest power of 2 in the heap.2 Subtract this power of 2 from the heap size to obtain the remainder.3 Reapply steps 1 and 2, until the the remainder equals a power of 2.

;; Returns the largest power of 2 not greater than num.(define (largest-binary-power num)

(letrec ((pot-aux (lambda (pot)(if (> (* 2 pot) num)

pot(pot-aux (* 2 pot))))))

(pot-aux 1)))

;; Decompose num into a list of powers of 2.(define (binary-rep num)

(if (= num 0)’()(let ((pot (largest-binary-power num)))

(cons pot (binary-rep (- num pot))))))

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 12 / 39

Page 50: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of Nim

For example, if a certain heap contained 328 tokens, we1 Identify 256 D 28 as the largest power of 2 contained in 328.2 Subtract 328 � 256 D 72. Thus, 328 D 256C 72.3 The remainder, 72 is not a power of 2, so we continue.4 Identify 64 D 26 as the largest power of 2 contained in 72.5 Subtract 72 � 64 D 8. Thus, 328 D 256C 64C 8.6 Since the remainder 8 D 23 is a power of 2, we can stop. Thus,

328 D 256C 64C 8 D 28C 26

C 23D 101001000 .base 2/:

The rightmost number is written in binary notation. The 0 in the rightmost columnindicates that 20 does not appear in the sum. Neither does 21 nor 22. The three 1scorrespond to 23, 26, and 28 which add up to 328. Thus, 101001000 is called thebinary representation of 328.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 13 / 39

Page 51: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of NimThe powers of 2 are said to be “balanced” if each power of 2 occurs an even numberof times after the heaps have been grouped as sums of powers of two.

The configuration (1 2 3) is balanced because,

1 D 20;

2 D 21;

3 D 21C 20;

i.e., both 20 and 21 occur twice: an even number of times.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 14 / 39

Page 52: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Parity Sums

Another way to demonstrate that a state is balanced is to1 Represent each heap size as a binary (base 2) number.2 Compute the parity sum of each column (or place).

I The parity sum equals 1 if there are an odd number of 1s in that column,I otherwise it is 0.

3 If the parity sums in every colomun zero, then the state is balanced.

Thus, for the state (1 2 3)

Decimal Binary1 012 103 11

Parity Sum 00 D 0) state is balanced.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 15 / 39

Page 53: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Parity Sums (cont.)Here is another example. Consider the state (5 9 11 14). By the first method wedecompose each heap size as a sum of powers of two

5 D 4 C 1 D 22 C 20

9 D 8 C 1 D 23 C 20

11 D 8 C 2 C 1 D 23 C 21 C 20

14 D 8 C 4 C 2 D 23 C 22 C 21

Note that 8 D 23 occurs three times, 4 D 22 occurs twice, 2 D 21 occurs twice, and1 D 20 occurs three times. Since some powers of 2 occur an odd number of timesthis state is unbalanced.

By the other procedure, we would write

Decimal Binary5 01019 100111 101114 1110

Parity Sum 1001 = 9 ¤ 0) state is unbalanced

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 16 / 39

Page 54: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Philosophical RemarkWhat do you think is the difference between these two procedures? “Very little,”perhaps: both methods determine the oddness or evenness of each power of 2.Humans players likely prefer the first method, as it is easy to imagine each heap asconsisting of groups of powers of 2. For example the heaps for the state (5 9 11 14)can be horizontally regrouped as

The second method, however, is easier for a computer. The resulting parity sum isoften called the Sprague-Grundy number G.s/ of the current state s. TheSprague-Grundy number is a useful concept as it can be extended to other games ofperfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 17 / 39

Page 55: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Secret of Nim

Bouton, who first analyzed Nim, called the balanced states safe, and the unbalancedstates unsafe.

The three essential facts of Nim, and Nim misère, are1 From every safe state, one must move to an unsafe one.2 From any unsafe state, one can move to at least one safe state.3 Both Nim and Nim misère end in a safe state: ( ).

If a game of Nim begins in a safe state (e.g., (1 2 3)), then the second player canalways win: the first player must move to an unsafe state, and thus the second playercan rebalance the powers of two. For example, if Max removes the 20 from the firstheap, Min would remove 20 from the third, etc.

If a Nim game begins in an unsafe state, then the first player can win by moving to asafe state, and returning to a safe state in each subsequent turn.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 18 / 39

Page 56: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Northcott’s Game

5

0

114241

Rules:Game begins with exactly one red and one black token in each row of a rectangular grid.

One player choses red; the other player takes black and moves first.

Players alternate turns. During a turn, a player moves one of his or her tokens to anyvacant square in the same row, so long as it does not cross the opponent’s token in thatrow.

The first player unable to move, because his or her pieces are blocked, loses.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 19 / 39

Page 57: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Northcott’s Game

5

0

114241

It turns out the Northcott’s Game is essentially the same game as Nim, where the heap sizescorrespond to the number of empty spaces that lie between two tokens on the same row.One slight difference, is that by moving away from one’s opponent, one can add tokens,instead of removing them, during one’s turn. However, this does not change the outcome, asthe following player can remove the new empty spaces on his or her next turn.How should black move in the state above?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 19 / 39

Page 58: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Wythoff’s Game

Wythoff’s Game is played like Nim, except

1 There are initially only two heaps; and2 Each turn a player may either remove some tokens from a single heap, or the

same number of tokens from both heaps.

Can you obtain an optimal strategy?

Note that Whytoff’s game can also be played with a sufficiently large chessboard. Ifthe size of the first heap is x, and the size of the second is y, then place the queenon the square that belongs to both column x C 1 (from the left side), and row y C 1

(from the top). Players alternate turns moving the queen one or more squares eitherto the left, up, or along the upper-left diagonal, so long as the queen never leaves theboard. The first player who moves the queen to the upper-left-most square is thewinner.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 20 / 39

Page 59: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Whythoff’s Queen in State (6 8)

q

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 21 / 39

Page 60: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Constrained NimConsider a game of Nim in which players can only remove one, two, or three tokensfrom a single heap during a turn.

Can you obtain an optimal strategy for this variation?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 22 / 39

Page 61: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

The Game of KaylesThis game begins with an arbitrary number of tokens in a single row:

Two players alternate turns. During a turn a player may remove either one or twoadjacent tokens from a single heap. Note that if tokens are removed from the middleof a heap, then their removal creates a gap that divides that heap into two.

As in Nim, the player who removes the last token wins. Find an optimal strategy.

The game defined above is actually quite transparent. A more challenging version,as defined by H. E. Dudeney, begins with the initial state that consists of the followingtwo heaps,

(See, Henry Ernest Dudeney, The Canterbury Puzzles, Dover, NY.)

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 23 / 39

Page 62: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded NumberFor every finite list of integers greater than or equal to zero, there exists a minimuminteger that is not contained within the list. Berlekamp, Conway, and Guy (2001) callthis integer the minimal excluded number, or mex for short.

For example,

(mex ’(0 1 2 4 5)) ) 3(mex ’(2 4 5)) ) 0(mex ’()) ) 0

Can you implement mex in racket?

The Minimum Excluded Number is also called the Sprague-Grundy Number, in thecontext of combinatorial games.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 24 / 39

Page 63: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Racket definition of mex

;;; (mexaux i loi) returns the smallest integer not less;;; than i that is absent from the list of integers loi.;;; mexaux is an auxiliary function used by mex.(define (mexaux i loi)

(if (memq i loi)(mexaux (+ i 1) loi)i))

;;; (mex loi) returns the smallest non-negative integer that is;;; absent from the list of integers loi. mex stands for the;;; minimum excluded integer: the Sprague-Grundy number of games;;; of perfect information. Thus (mex ’(0 3 1 3)) => 2, and;;; (mex ’()) => 0.(define (mex loi)

(mexaux 0 loi))

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 25 / 39

Page 64: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

More efficient definition of mex

;; mex evaluates the minimal excluded (non-negative) number from;; the list of integers that is bound to the argument loi.;; Thus, (mex ’(0 3 1 3)) => 2, and (mex ’()) => 0.(define (mex loi)

(letrec ((mexaux (lambda (i)(if (memq i loi)

(mexaux (+ i 1))i))))

(mexaux 0)))

This function uses the special built-in keyword letrec to define a local recursivefunction called aux, that does the heavy lifting.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 26 / 39

Page 65: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded Numbers in Nim

(2 2)

(1 2) (2)

(2) (1)(1 1)

(1)

()(1)

()(1)

() ()

() ()

0

00

0 0

0

11

2 01 1

3 20

Here we have redrawn the nimtree, but have relabeled each vertex by the parity sum(or Sprague-Grundy number) of that state. Note that value of each node equals themex of its immediate descendants (child nodes). This property can be used to findoptimal strategies for other games of perfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39

Page 66: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded Numbers in Nim

(2 2)

(1 2) (2)

(2) (1)(1 1)

(1)

()(1)

()(1)

() ()

() ()

0

00

0 0

0

11

2 01 1

3 20

Here we have redrawn the nimtree, but have relabeled each vertex by the parity sum(or Sprague-Grundy number) of that state. Note that value of each node equals themex of its immediate descendants (child nodes). This property can be used to findoptimal strategies for other games of perfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39

Page 67: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded Numbers in Nim

(2 2)

(1 2) (2)

(2) (1)(1 1)

(1)

()(1)

()(1)

() ()

() ()

0

00

0 0

0

11

2 01 1

3 20

Here we have redrawn the nimtree, but have relabeled each vertex by the parity sum(or Sprague-Grundy number) of that state. Note that value of each node equals themex of its immediate descendants (child nodes). This property can be used to findoptimal strategies for other games of perfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39

Page 68: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded Numbers in Nim

(2 2)

(1 2) (2)

(2) (1)(1 1)

(1)

()(1)

()(1)

() ()

() ()

0

00

0 0

0

11

2 01 1

3 20

Here we have redrawn the nimtree, but have relabeled each vertex by the parity sum(or Sprague-Grundy number) of that state. Note that value of each node equals themex of its immediate descendants (child nodes). This property can be used to findoptimal strategies for other games of perfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39

Page 69: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Minimal Excluded Numbers in Nim

(2 2)

(1 2) (2)

(2) (1)(1 1)

(1)

()(1)

()(1)

() ()

() ()

0

00

0 0

0

11

2 01 1

3 20

Here we have redrawn the nimtree, but have relabeled each vertex by the parity sum(or Sprague-Grundy number) of that state. Note that value of each node equals themex of its immediate descendants (child nodes). This property can be used to findoptimal strategies for other games of perfect information.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39

Page 70: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague Grundy Numbers for Whytoff’s GameLet .i j / denote an arbitrary state in Whytoff’s Game, where i; j 2 f0; 1; : : :g.

1 First we assign the value 0 to the final winning state (0 0). Thus theSprague-Grundy number for (0 0) is,

G.0 0/ D 0:

(Sprague-Grundy numbers for safe states will be colored green.)2 Next, we consider the states (0 1) and (1 0). Each of these states has only one

possible subsequent state: (0 0). Thus,

G.0 1/ D G.1 0/ D mexfG.0 0/g D mexf0g D 1:

(Sprague-Grundy numbers for unsafe states will be colored red.)3 Now we consider the state (1 1). By the rules of the game (1 1) has three

possible successors: (1 0), (0 1), and (0 0). Thus,

G.1 1/ D mexfG.1 0/; G.0 1/; G.0 0/g

D mexf1; 1; 0g

D 2:

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 28 / 39

Page 71: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0

1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

The cell in the i -th row of the j -th column will contain the Sprague-Grundy numberfor the state with i tokens in the first heap, and j tokens in the second. Let’s fill in thefour values found so far. Can you complete the table?

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 72: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1

2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 73: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2

3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 74: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3

4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 75: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4

5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 76: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5

6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 77: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6

7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 78: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7

8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 79: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8

9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 80: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 81: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1

2 0 4 5 3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 82: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2

0 4 5 3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 83: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0

4 5 3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 84: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4

5 3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 85: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5

3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 86: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3

7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 87: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7

8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 88: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8

6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 89: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6

10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 90: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2

0 1 5 3 4 8 6 7 11

3

4 5 6 2 0 1 9 10 12

4

5 3 2 7 6 9 0 1 8

5

3 4 0 6 8 10 1 2 7

6

7 8 1 9 10 3 4 5 13

7

8 6 9 0 1 4 5 3 14

8

6 7 10 1 2 5 3 4 15

9

10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 91: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0

1 5 3 4 8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 92: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1

5 3 4 8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 93: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5

3 4 8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 94: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3

4 8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 95: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4

8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 96: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8

6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 97: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6

7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 98: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7

11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 99: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4

5 6 2 0 1 9 10 12

4 5

3 2 7 6 9 0 1 8

5 3

4 0 6 8 10 1 2 7

6 7

8 1 9 10 3 4 5 13

7 8

6 9 0 1 4 5 3 14

8 6

7 10 1 2 5 3 4 15

9 10

11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 100: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5

6 2 0 1 9 10 12

4 5 3

2 7 6 9 0 1 8

5 3 4

0 6 8 10 1 2 7

6 7 8

1 9 10 3 4 5 13

7 8 6

9 0 1 4 5 3 14

8 6 7

10 1 2 5 3 4 15

9 10 11

12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 101: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Whytoff’s Game (cont.)

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

0 1 2 3 4 5 6 7 8 9

1 2 0 4 5 3 7 8 6 10

2 0 1 5 3 4 8 6 7 11

3 4 5 6 2 0 1 9 10 12

4 5 3 2 7 6 9 0 1 8

5 3 4 0 6 8 10 1 2 7

6 7 8 1 9 10 3 4 5 13

7 8 6 9 0 1 4 5 3 14

8 6 7 10 1 2 5 3 4 15

9 10 11 12 8 7 13 14 15 16

As in nim, players only need to know which states are safe. Each is indicated by a 0.The optimal strategy is to move always to a safe state, if possible. If the game startsin an unsafe state, then a skillful first player will win. Otherwise, the second player,playing optimally, will win.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39

Page 102: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

The state tree for a game of kayles with initially five contiguous tokens. Note thatredundant states are not expanded.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 30 / 39

Page 103: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

0

First we assign a Sprague-Grundy number (SGN) of 0 to each terminal state ().

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 31 / 39

Page 104: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

1 1

Since the SGN of each state is the mex of SGNs of all of its descendants, we canassign an SGN of 1 to the state (1), everywhere it appears.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 32 / 39

Page 105: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

2 1

2 0 0 2

0 2 1 0 0

The state (2) has two descendants: (1) and (), with SGNs of 1 and 0, respectively.The SGN of state (2) is thus (mex ’(1 0)) ) 2. The state (1 1) has a singledescendant of (1) with an SGN of 1. Thus, its SGN is (mex ’(1)) ) 0.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 33 / 39

Page 106: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

2 1

3 2 3 0 0 3 1 3 3 2

33

0 2 1 0 0

States (3) and (1 2) both share the same set of descendants: (2), (1), and (1 1),which have SGNs of 2, 1, and 0, respectively. Thus, they share an SGN of (mex ’(21 0)) ) 3.State (1 1 1) has only one descendant, (1 1), which has an SGN of 0. Consequently,(1 1 1) is assigned an SGN of 1.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 34 / 39

Page 107: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

2 1

3 2 3 0 0 3 1 3 3 2

30231

0 2 1 0 0

Since the descendants of state (4) have SGNs of 3, 2, and 0, the state (4) is assigneda SGN of (mex ’(3 2 0)) ) 1. Similarly, the state (1 3) is assigned an SGN of(mex ’(0 1 3)))2, while (2 2) is assigned the value (mex ’(2 3))) 0.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 35 / 39

Page 108: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

2 1

3 2 3 0 0 3 1 3 3 2

3023

4

1

0 2 1 0 0

Finally the initial state (5), the root node is assigned a SGN of (mex ’(0 1 2 3))) 4.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 36 / 39

Page 109: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Sprague-Grundy Numbers for Kayles (cont.)

(4) (3)

(3) (2)

(2) (1)

(1) (1)()

()

(1 1) (2) (1) (1 1) (1 1)

(1 2) (1 1) (1 1) (1 2) (1 2) (2)(3)(1 1 1)

(1 3) (2 2) (1 2)

(5)

0

1 0 1

2 1

3 2 3 0 0 3 1 3 3 2

3023

4

1

0 2 1 0 0

Given the initial state (5), the optimal move for the first player is to remove the centertoken so that the state becomes (2 2). As this state has a Sprague-Grundy number of0, what ever moves the second play makes, the first player will always have a moveto another state with a Sprague-Grundy number of 0. By continuing in this manner,the first player is insured an eventual victory.

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 37 / 39

Page 110: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Table of binary numbersFor reference, here is a table of the first 64 binary numbers:

Decimal Binary Decimal Binary Decimal Binary Decimal Binary0 000000 16 010000 32 100000 48 1100001 000001 17 010001 33 100001 49 1100012 000010 18 010010 34 100010 50 1100103 000011 19 010011 35 100011 51 1100114 000100 20 010100 36 100100 52 1101005 000101 21 010101 37 100101 53 1101016 000110 22 010110 38 100110 54 1101107 000111 23 010111 39 100111 55 1101118 001000 24 011000 40 101000 56 1110009 001001 25 011001 41 101001 57 111001

10 001010 26 011010 42 101010 58 11101011 001011 27 011011 43 101011 59 11101112 001100 28 011100 44 101100 60 11110013 001101 29 011101 45 101101 61 11110114 001110 30 011110 46 101110 62 11111015 001111 31 011111 47 101111 63 111111

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 38 / 39

Page 111: 13. Combinatorial Games - UVMrsnapp/teaching/cs32/lectures/nim.pdf1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity

Table of powers of twoFor reference, here are the first 48 powers of 2.

n 2n n 2n n 2n

0 1 16 65536 32 42949672961 2 17 131072 33 85899345922 4 18 262144 34 171798691843 8 19 524288 35 343597383684 16 20 1048576 36 687194767365 32 21 2097152 37 1374389534726 64 22 4194304 38 2748779069447 128 23 8388608 39 5497558138888 256 24 16777216 40 10995116277769 512 25 33554432 41 2199023255552

10 1024 26 67108854 42 439804651110411 2048 27 134217728 43 879609302220812 4096 28 268435456 44 1759218604441613 8192 29 536870912 45 3518437208883214 16384 30 1073741824 46 7036874417766415 32768 31 2147483648 47 140737488355328

Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 39 / 39