Computer Science and Software Engineering University of Wisconsin - Platteville 4. Game Play CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 6
What kind of games? 2-player games Zero-sum games: If 1 is win, 0 is draw and -1 is loss, the total score at the end of a game is 0. No element of chance No dice, no card drawn Players are rational
Game Tree Nodes: possible states of the game Leaf nodes: final state: win, loss or draw Edges: moves
Searching Game Tree Game tree usually have very high branching factors and are very deep! tic-tac-toe: 9! = 350,000 nodes So it is not feasible to do any exhaustive search. prune the search tree use heuristics Another issue: two-player game means the opponent may interrupt your planned move! we must assume the opponent will attempt to make us lose.
General Minimax Search Try to maximize some score the opponent is trying to minimize. the score is usually the evaluation function e(). Minimax Search: Take turns in state-space search only assign evaluated value to the leaf nodes our turn (max node): pick branch resulting in position with maximal e() opponent s turn (min node): pick branch resulting in position with minimal e() Algorithm: minimax(player,board) if(game over in current board position) return winner children = all legal moves for player from this board if(max's turn) return maximal score of calling minimax on all the children else (min's turn) return minimal score of calling minimax on all the children
Bounded Minimax Problem: in order to apply Minimax search, the entire tree has to be examined! Solution: Only examine the tree to a certain depth! All nodes at this depth is considered to be leaf nodes and are evaluated using an evaluation function. n-move look-ahead: examining space after expanding n levels, or plys. expand state space by n plys evaluate state of each node at nth level propagate results up: if node represents computer's move, pick maximum; if it's opponent's move, pick minimum
Evaluation function Evaluation function: how likely to win the game from this state. Usually a weighted linear function Examples: Chess Score = 9q + 5r + 3b + 3n + p Where q,r,b,n and p represents #of queens, #of rooks, #of bishops, #of knights and #of pawns Tic-Tac-Toe Score = #of cells open to form a line If two computers are playing the game, how good the evaluation function is will decide the winner! O X
Tic-Tac-Toe Search Example e() = #of cells open to MAX # of cells open to MIN Look one step ahead! From Nilsson s textbook Step 1
Bounded Lookahead Example MAX MIN MAX MIN
Exercise: Minimax MAX MIN 4 3 9 1 80 2 3 5 2 7 6
Bounded Lookahead Issues When to cut off (decide it is a leaf)? cut off at a position where the next move is unlikely to cause a large change in the relative positions of the two players. Horizon problem: an extremely long sequence of moves that lead to a strong advantage for one player but go beyond the bounded search. solution: singular-extension heuristic: If the evaluation function of a move is much better than others, keep searching.
Pruning Problem: in order to apply Minimax search, the entire tree has to be examined! Solution: prune sections of the game tree. If a move is worse than another move already examined, further examining is pointless. MAX Does the opponent need to check the branch rooted at MAX node 7?
Alpha-Beta Pruning : the value of the best possible move you can make, that you have computed so far. : the value of the best possible move your opponent can make, that you have computed so far. alpha-beta pruning can reduce the number of nodes to be evaluated by half!
Alpha-Best Pruning Example >= DFS Traversal! <= <= <= >= >= Whenever determine value of node, propagate result up through tree. Before exploring child, check that exploring it could improve the results. Suppose value of MAX node is at least x; can ignore any (MIN) child with value x. Suppose value of MIN node is no more than y; can ignore any (MAX) child with value y.
Alpha-Beta Pruning Algorithm alpha-beta(current, alpha, beta) if isroot(current) alpha = -infinity; beta = infinity if isleaf(current) return e(current) if ismaxnode(current) alpha = max(alpha, alpha-beta(children, alpha, beta) if alpha >= beta cut off search below current if isminnode(current) beta = min(alpha, alpha-beta(children, alpha, beta) if alpha >= beta cut off search below current
Exercise: alpha-beta pruning MAX MIN 4 3 9 1 80 2 3 5 2 7 6
Summary 2-player game assumptions Game tree General minimax search bounded lookahead alpha-beta pruning