Backtracking and Games: Eric Roberts CS 106B January 30, 2012
Backtracking and Games: Eric Roberts CS 106B January 30, 2012
Backtracking and Games: Eric Roberts CS 106B January 30, 2012
that generates a vector showing all subsets of the set formed from the letters in set. For example, calling generateSubsets("abc") should return a vector containing the following eight strings, in some order:
"" "a" "b" "c" "ab" "ac" "bc" "abc"
The solution process requires a branching structure similar to that used to solve a maze. At each level of the recursion, you can either exclude or include the current letter from the list of subsets, as illustrated on the following slide.
Has no 'b'
Has a 'b'
Has no 'b'
Has a 'b'
Has no 'c'
Has a 'c'
Has no 'c'
Has a 'c'
Has no 'c'
Has a 'c'
Has no 'c'
Has a 'c'
{}
{c}
{b}
{ b, c }
{a}
{ a, c }
{ a, b } { a, b, c }
53 35 30 25 20 10 Positions evaluated: ~ 1000 10 5 9000 8000 7000 6000 5000 4000 3000 2000
Game Trees
As Shannon observed in 1950, most two-player games have the same basic form:
The first player (red) must choose between a set of moves For each move, the second player (blue) has several responses. For each of these responses, red has further choices. For each of these new responses, blue makes another decision. And so on . . .
A Simpler Game
Chess is far too complex a game to serve as a useful example. The text uses a much simpler game called Nim, which is representative of a large class of two-player games. In Nim, the game begins with a pile of coins between two players. The starting number of coins can vary and should therefore be easy to change in the program. In alternating turns, each player takes one, two, or three coins from the pile in the center. The player who takes the last coin loses.
There are 11 coins left. I'll take 2. There are 9 coins left. Your move: 1 There are 8 coins left. I'll take 3. There are 5 coins left. Your move: 2 There are 3 coins left. I'll take 2. There is only one coin left. You lose.
The implementation of the Nim game is really nothing more than a translation of these definitions into code.
A Minimax Illustration
Suppose that the ratings two turns from now are as shown. From your perspective, the +9 initially looks attractive. Unfortunately, your cant get there, since the 5 is better for your opponent. The best you can do is choose the move that leads to the 2.
+7
+6
+9
+1
A turn consists of taking any number of coins, but they must all be from the same row.
The End