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 1 Two-Player Games of Perfect Information 2 Charles Bouton’s Nim Nim Strategies Nimtrees The Secret of Nim Parity Sums 3 Northcott’s Game 4 Wythoff’s Game 5 Kayles 6 Sprague-Grundy Numbers Analysis of Nim Analysis of Whythoff’s Game Analysis of Kayles 7 Appendix Binary Numbers Powers of Two Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 2 / 39 Two-player Games of Perfect Information Players 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? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Two-player Games of Perfect Information Players 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? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Two-player Games of Perfect Information Players 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? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Two-player Games of Perfect Information Players 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? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Two-player Games of Perfect Information Players 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? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Two-player Games of Perfect Information Players 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 I Checkers (or Draughts) I Chinese Checkers I I I Hex (or Nash) I Brigde-It I Twixt I Three-dimensional Chess Go Nim I Northcott’s Game I Othello (or Reversi) Chess I I Konane (Hawaiian Checkers) I Sprouts I I Chinese Chess (Xiangqi) I Go-Moku (5 in a row) I Connect Four Whytoff’s Game Kayles Robert R. Snapp © 2010, 2012 Mancala & Bao I 13. Combinatorial Games CS 32, Fall 2012 3 / 39 Charles Bouton’s Nim A two-player game of perfect information, possibly of Chinese origin. It was analyzed by Charles 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 of tokens 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 2 Martin Gardner’s, Hexaflexagons and Other Mathematical Diversions, University of Chicago Press, 1988, pp. 151–161. Elwyn R. Berlekamp, John H. Conway, and Richard W. Guy, Winning Ways for your mathematical 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 the third heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 Sample Game: Alice vs. Bob Consider 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 to take. Thus, Bob resigns. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 5 / 39 Nim Strategies Before describing an optimal strategy for this game, let’s look at a few interesting cases. 1 What would you do, if it were your turn and all of the remaining tokens belong to a 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 Nim Strategies Before describing an optimal strategy for this game, let’s look at a few interesting cases. 1 What would you do, if it were your turn and all of the remaining tokens belong to a single heap? For example, 2 What would you do if it were your turn, and there were two unequal heaps? Answer: Remove the entire heap. Answer: Remove the difference from the larger, making them equal. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39 Nim Strategies Before describing an optimal strategy for this game, let’s look at a few interesting cases. 1 What would you do, if it were your turn and all of the remaining tokens belong to a single heap? For example, 2 What would you do if it were your turn, and there were two unequal heaps? Answer: Remove the entire heap. Answer: Remove the difference from the larger, making them equal. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39 Nim Strategies Before describing an optimal strategy for this game, let’s look at a few interesting cases. 1 What would you do, if it were your turn and all of the remaining tokens belong to a single heap? For example, 2 What would you do if it were your turn, and there were two unequal heaps? Answer: Remove the entire heap. Answer: Remove the difference from the larger, making them equal. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 6 / 39 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 Nimtrees Earlier in the course we learned how trees can be used to enumerate (or count) the number of permutations (using a factorial tree), the number of sequences of coin tosses (using a binary tree), and the number of different rolls of a pair of dice (using a hexary tree). We now extend that idea to nim, and will enumerate every possible game that can be played from the state (2 2). We first draw a root node and label it with the state (2 2). We shall call the first player Max, and consider every possible move that Max can make. Max can 1 2 3 4 Remove one token from the first heap, yielding (1 2), or Remove two tokens from the first heap, yielding (2), or Remove one token from the second heap, yielding (2 1), or 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 only consider the first two cases. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 8 / 39 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 We thus insert nodes for states (1 2) and (2) below the root node, (2 2), and connect each to the root node with a green edge. Next, we will consider the moves that the second player, called Min, can make from each of these two states. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 There are three moves that Min can make from the state (1 2): removing 1 from the first heap yields that state (2), 1 from the second yields (1 1), and 2 from the second yields (1). There are only two moves that Min can make from the state (2): removing one token yields (1); removing 2, yields ( ): victory for Min. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 Only two active nodes are left. Min can convert (1), on either subtree, to ( ), wins for Min. Note that in this full expanded nimtree, the root node is the initial state, and every leaf node is an empty list, which we have colored to indicate the player who removed the last token. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 9 / 39 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 We will now prove that Min (the second player) can always win from the initial position, (2 2), no matter how Max plays. First we assign a score of C1 to each game that Max wins, and a score of 1 to each game 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 Next, we assign a score to node of the tree that reflects the outcome of the game assuming that both sides play optimally. We will work from the bottom of the tree (leaf nodes) 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 1 () 1 1 We continue to assign scores to nodes one step up the tree. Since Max wants to maximize the score, given the state (2), Max would select () over (1). Thus the score of (2) should equal the maximum score of its descendants. This same rule applies if no 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 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 its descendants. 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 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, so the score of (2 2) should be the maximum score of its descendants. However, the latter are equal to 1. Consequently, the score of the root node is also 1, so Min is destined 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 Nimtrees (cont.) (2 2) 1 (1 2) 1 (2) C1 (1 1) 1 (1) C1 (1) C1 C1 (1) () 1 C1 () C1 () (1) 1 1 () () (2) 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, so the score of (2 2) should be the maximum score of its descendants. However, the latter are equal to 1. Consequently, the score of the root node is also 1, so Min is destined 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 Now you try! Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 10 / 39 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 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim Before you make a move, group each heap into unique powers of 2. For example, the state (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, or 2 remove 9 tokens from the third heap, or 3 remove 7 tokens from the fourth heap. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 11 / 39 The Secret of Nim The first step is to resolve each heap into a sum of different powers of 2. 1 2 3 Find the largest power of 2 in the heap. Subtract this power of 2 from the heap size to obtain the remainder. 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 The Secret of Nim For example, if a certain heap contained 328 tokens, we 1 2 3 4 5 6 Identify 256 D 28 as the largest power of 2 contained in 328. Subtract 328 256 D 72. Thus, 328 D 256 C 72. The remainder, 72 is not a power of 2, so we continue. Identify 64 D 26 as the largest power of 2 contained in 72. Subtract 72 64 D 8. Thus, 328 D 256 C 64 C 8. Since the remainder 8 D 23 is a power of 2, we can stop. Thus, 328 D 256 C 64 C 8 D 28 C 26 C 23 D 101001000 .base 2/: The rightmost number is written in binary notation. The 0 in the rightmost column indicates that 20 does not appear in the sum. Neither does 21 nor 22 . The three 1s correspond to 23 , 26 , and 28 which add up to 328. Thus, 101001000 is called the binary representation of 328. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 13 / 39 The Secret of Nim The powers of 2 are said to be “balanced” if each power of 2 occurs an even number of 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 21 C 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 Parity Sums Another way to demonstrate that a state is balanced is to 1 2 Represent each heap size as a binary (base 2) number. Compute the parity sum of each column (or place). I I 3 The parity sum equals 1 if there are an odd number of 1s in that column, otherwise it is 0. If the parity sums in every colomun zero, then the state is balanced. Thus, for the state (1 2 3) Decimal 1 2 3 Parity Sum Robert R. Snapp © 2010, 2012 Binary 01 10 11 00 D 0 ) state is balanced. 13. Combinatorial Games CS 32, Fall 2012 15 / 39 Parity Sums (cont.) Here is another example. Consider the state (5 9 11 14). By the first method we decompose each heap size as a sum of powers of two 5 9 11 14 D 4 C 1 D 22 C 20 3 D 8 C 1 D 2 C 20 3 1 D 8 C 2C 1 D 2 C 2 C 20 3 2 D 8 C4 C 2 D 2 C 2 C 21 Note that 8 D 23 occurs three times, 4 D 22 occurs twice, 2 D 21 occurs twice, and 1 D 20 occurs three times. Since some powers of 2 occur an odd number of times this state is unbalanced. By the other procedure, we would write Decimal 5 9 11 14 Parity Sum Robert R. Snapp © 2010, 2012 Binary 0101 1001 1011 1110 1001 = 9 ¤ 0 ) state is unbalanced 13. Combinatorial Games CS 32, Fall 2012 16 / 39 Philosophical Remark What 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 as consisting 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 is often called the Sprague-Grundy number G.s/ of the current state s . The Sprague-Grundy number is a useful concept as it can be extended to other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 17 / 39 The Secret of Nim Bouton, who first analyzed Nim, called the balanced states safe, and the unbalanced states unsafe. The three essential facts of Nim, and Nim misère, are 1 2 3 From every safe state, one must move to an unsafe one. From any unsafe state, one can move to at least one safe state. 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 can always win: the first player must move to an unsafe state, and thus the second player can rebalance the powers of two. For example, if Max removes the 20 from the first heap, 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 a safe 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 Northcott’s Game 5 0 1 1 4 2 4 1 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 any vacant square in the same row, so long as it does not cross the opponent’s token in that row. 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 Northcott’s Game 5 0 1 1 4 2 4 1 It turns out the Northcott’s Game is essentially the same game as Nim, where the heap sizes correspond 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, as the 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 Wythoff’s Game Wythoff’s Game is played like Nim, except 1 2 There are initially only two heaps; and 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. If the size of the first heap is x , and the size of the second is y , then place the queen on 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 either to the left, up, or along the upper-left diagonal, so long as the queen never leaves the board. The first player who moves the queen to the upper-left-most square is the winner. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 20 / 39 Whythoff’s Queen in State (6 8) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Robert R. Snapp © 2010, 2012 q 13. Combinatorial Games CS 32, Fall 2012 21 / 39 Constrained Nim Consider a game of Nim in which players can only remove one, two, or three tokens from 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 The Game of Kayles This 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 two adjacent tokens from a single heap. Note that if tokens are removed from the middle of 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 following two heaps, (See, Henry Ernest Dudeney, The Canterbury Puzzles, Dover, NY.) Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 23 / 39 Minimal Excluded Number For every finite list of integers greater than or equal to zero, there exists a minimum integer that is not contained within the list. Berlekamp, Conway, and Guy (2001) call this integer the minimal excluded number, or mex for short. For example, (mex ’(0 1 2 4 5)) ) 3 (mex ’(2 4 5)) ) 0 ) 0 (mex ’()) Can you implement mex in racket? The Minimum Excluded Number is also called the Sprague-Grundy Number, in the context of combinatorial games. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 24 / 39 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 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 recursive function called aux, that does the heavy lifting. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 26 / 39 Minimal Excluded Numbers in Nim (2 2) 0 (1 2) 3 (2) 2 (1) 1 0 () 0 (1 1) 0 () (1) 1 (2) 2 (1) (1) 1 1 0 () 0 () 0 () 0 () 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 the mex of its immediate descendants (child nodes). This property can be used to find optimal strategies for other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39 Minimal Excluded Numbers in Nim (2 2) 0 (1 2) 3 (2) 2 (1) 1 0 () 0 (1 1) 0 () (1) 1 (2) 2 (1) (1) 1 1 0 () 0 () 0 () 0 () 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 the mex of its immediate descendants (child nodes). This property can be used to find optimal strategies for other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39 Minimal Excluded Numbers in Nim (2 2) 0 (1 2) 3 (2) 2 (1) 1 0 () 0 (1 1) 0 () (1) 1 (2) 2 (1) (1) 1 1 0 () 0 () 0 () 0 () 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 the mex of its immediate descendants (child nodes). This property can be used to find optimal strategies for other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39 Minimal Excluded Numbers in Nim (2 2) 0 (1 2) 3 (2) 2 (1) 1 0 () 0 (1 1) 0 () (1) 1 (2) 2 (1) (1) 1 1 0 () 0 () 0 () 0 () 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 the mex of its immediate descendants (child nodes). This property can be used to find optimal strategies for other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39 Minimal Excluded Numbers in Nim (2 2) 0 (1 2) 3 (2) 2 (1) 1 0 () 0 (1 1) 0 () (1) 1 (2) 2 (1) (1) 1 1 0 () 0 () 0 () 0 () 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 the mex of its immediate descendants (child nodes). This property can be used to find optimal strategies for other games of perfect information. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 27 / 39 Sprague Grundy Numbers for Whytoff’s Game Let .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 the Sprague-Grundy number for (0 0) is, G.0 0/ D 0: 2 (Sprague-Grundy numbers for safe states will be colored green.) 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: 3 (Sprague-Grundy numbers for unsafe states will be colored red.) 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 The cell in the i -th row of the j -th column will contain the Sprague-Grundy number for the state with i tokens in the first heap, and j tokens in the second. Let’s fill in the four values found so far. Can you complete the table? Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 29 / 39 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 3 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 3 7 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 3 7 8 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 3 7 8 6 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 0 4 5 3 7 8 6 10 2 3 4 5 6 7 8 9 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 0 4 5 3 7 8 6 10 0 4 5 3 7 8 6 10 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 0 4 5 3 7 8 6 10 1 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 4 5 6 7 8 9 3 4 5 6 7 8 9 4 5 3 7 8 6 10 5 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 5 6 7 8 9 4 5 6 7 8 9 5 3 7 8 6 10 3 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 4 5 3 5 6 7 8 9 5 6 7 8 9 3 7 8 6 10 4 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 4 5 3 5 5 3 4 6 7 8 9 6 7 8 9 7 8 6 10 8 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 4 5 3 5 5 3 4 6 6 7 8 7 8 9 7 8 9 8 6 10 6 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 4 5 3 5 5 3 4 6 6 7 8 7 7 8 6 8 9 8 9 6 10 7 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 3 3 4 5 4 4 5 3 5 5 3 4 6 6 7 8 7 7 8 6 8 8 6 7 9 9 10 11 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 5 3 4 8 6 7 11 3 3 4 5 4 4 5 3 5 5 3 4 6 6 7 8 7 7 8 6 8 8 6 7 9 9 10 11 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 starts in 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 Sprague-Grundy Numbers for Whytoff’s Game (cont.) 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 0 4 5 3 7 8 6 10 2 2 0 1 5 3 4 8 6 7 11 3 3 4 5 6 2 0 1 9 10 12 4 4 5 3 2 7 6 9 0 1 8 5 5 3 4 0 6 8 10 1 2 7 6 6 7 8 1 9 10 3 4 5 13 7 7 8 6 9 0 1 4 5 3 14 8 8 6 7 10 1 2 5 3 4 15 9 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 starts in 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 Sprague-Grundy Numbers for Kayles (5) (4) (3) (2) (1) (1) () (2) (1 1) (3) (1 2) (2) (1 1) (1) (1 1) (1 1) (1 3) (2 2) (1 2) (1 1 1) (3) (1 2) (1 2) (2) (1 1) (1) () The state tree for a game of kayles with initially five contiguous tokens. Note that redundant states are not expanded. Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 30 / 39 Sprague-Grundy Numbers for Kayles (cont.) (5) (4) (3) (2) (1) (1) 0() (2) (1 1) (3) (1 2) (2) (1 1) (1) (1 1) (1 1) (1 3) (2 2) (1 2) (1 1 1) (3) (1 2) (1 2) (2) (1 1) (1) 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 Sprague-Grundy Numbers for Kayles (cont.) (5) (4) (3) (2) 1 (1) 1 (1) 0() (2) (1 1) (3) (1 2) (2) (1 1) 1 (1) (1 1) (1 1) (1 3) (2 2) (1 2) (1 1 1) (3) (1 2) (1 2) (2) (1 1) 1(1) 0 () Since the SGN of each state is the mex of SGNs of all of its descendants, we can assign 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 Sprague-Grundy Numbers for Kayles (cont.) (5) (4) (3) 2 (2) 1 (1) 2(2) 1 (1) 0(1 1) 0() (3) (1 2) 0(1 1) 2(2) 0(1 1) 1 (1) 0(1 1) (1 3) (1 2) (1 1 1) (2 2) (3) (1 2) (1 2) 2(2) 0 (1 1) 1(1) 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 single descendant 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 Sprague-Grundy Numbers for Kayles (cont.) (5) 3(3) (4) 3(3) 2 (2) 1 (1) 2(2) 1 (1) 0(1 1) 0() 3(1 2) 0(1 1) 2(2) (1 3) 0(1 1) 3(1 2) 1(1 1 1) 3(3) 1 (1) 0(1 1) (2 2) 3(1 2) 3(1 2) 2(2) 0 (1 1) 1(1) 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 ’(2 1 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 Sprague-Grundy Numbers for Kayles (cont.) (5) 3(3) 2(1 3) 1(4) 3(3) 2 (2) 1 (1) 2(2) 1 (1) 0(1 1) 0() 3(1 2) 0(1 1) 2(2) 0(1 1) 3(1 2) 1(1 1 1) 3(3) 1 (1) 0(1 1) 0(2 2) 3(1 2) 3(1 2) 2(2) 0 (1 1) 1(1) 0 () Since the descendants of state (4) have SGNs of 3, 2, and 0, the state (4) is assigned a 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 Sprague-Grundy Numbers for Kayles (cont.) 4 (5) 3(3) 2(1 3) 1(4) 3(3) 2 (2) 1 (1) 2(2) 1 (1) 0(1 1) 0() 3(1 2) 0(1 1) 2(2) 0(1 1) 3(1 2) 1(1 1 1) 3(3) 1 (1) 0(1 1) 0(2 2) 3(1 2) 3(1 2) 2(2) 0 (1 1) 1(1) 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 Sprague-Grundy Numbers for Kayles (cont.) 4 (5) 3(3) 2(1 3) 1(4) 3(3) 2 (2) 1 (1) 2(2) 1 (1) 0(1 1) 0() 3(1 2) 0(1 1) 2(2) 0(1 1) 3(1 2) 1(1 1 1) 3(3) 1 (1) 0(1 1) 0(2 2) 3(1 2) 3(1 2) 2(2) 0 (1 1) 1(1) 0 () Given the initial state (5), the optimal move for the first player is to remove the center token so that the state becomes (2 2). As this state has a Sprague-Grundy number of 0, what ever moves the second play makes, the first player will always have a move to 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 Table of binary numbers For reference, here is a table of the first 64 binary numbers: Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary 000000 000001 000010 000011 000100 000101 000110 000111 001000 001001 001010 001011 001100 001101 001110 001111 Robert R. Snapp © 2010, 2012 Decimal 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Binary 010000 010001 010010 010011 010100 010101 010110 010111 011000 011001 011010 011011 011100 011101 011110 011111 Decimal 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 13. Combinatorial Games Binary 100000 100001 100010 100011 100100 100101 100110 100111 101000 101001 101010 101011 101100 101101 101110 101111 Decimal 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 Binary 110000 110001 110010 110011 110100 110101 110110 110111 111000 111001 111010 111011 111100 111101 111110 111111 CS 32, Fall 2012 38 / 39 Table of powers of two For reference, here are the first 48 powers of 2. n 2n n 2n n 2n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108854 134217728 268435456 536870912 1073741824 2147483648 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4294967296 8589934592 17179869184 34359738368 68719476736 137438953472 274877906944 549755813888 1099511627776 2199023255552 4398046511104 8796093022208 17592186044416 35184372088832 70368744177664 140737488355328 Robert R. Snapp © 2010, 2012 13. Combinatorial Games CS 32, Fall 2012 39 / 39
© Copyright 2026 Paperzz