Playing games

Textbook: Chapter 14

One of AI's greatest successes. We study it through tic-tac-toe.

 

Games covered

Classical AI game techniques apply when:

Examples: tic-tac-toe, Connect-4, Othello/reversi, checkers, chess, go

Non-examples: poker, Boggle, Scrabble, Diplomacy

 

Game trees

		X | O | O
		--+---+--
		X |   |  
		--+---+--
		O | X |  

Question: How should X play?

Answer: Draw a game tree:

		          XOO
		          X--
		      ____OX-____
		     /     |     \
		    /      |      \
		  XOO     XOO     XOO
		  XX-     X-X     X--
		  OX-     OX-     OXX
		 /   \   /   \   /   \
		XOO XOO XOO XOO XOO XOO
		XXO XX- XOX X-X XO- X-O
		OX- OXO OX- OXO OXX OXX
		 |   |       |       |
		XOO XOO     XOO     XOO
		XXO XXX     XXX     XXO
		OXX OXO     OXO     OXX

Question: How big is game tree for an empty tic-tac-toe board?

Answer: There are 9! distinct tic-tac-toe games. There are at most 9!/(0!) + 9!/(1!) + 9!/2! + 9!/3! + 9!/4! + ... + 9!/9! boards in the tree.

 

Heuristics

Usually, the game tree is much too big to search all the way to a win or loss.

Idea: Use a heuristic function to estimate ``goodness'' of board.

When we get to particular depth, stop there instead of recursing.

Example heuristic

Consider this heuristic: Start with 0. Add 1 for each way with two X's and empty. Subtract 1 for each way with two O's and empty.

Doing the game tree with a depth of 2:

		          OX-  A
		          -O-
		      ____XOX____
		     /     0     \
		    /      |      \
		  OXX     OX-     OX-  B
		  -O-     XO-     -OX
		  XOX     XOX     XOX
		 /-1 \   / 0 \   / 0 \
		 |   |   |   |   |   |
		OXX OXX OX- OX- OXO OX-  C
		OO- -OO XOO XOO -OX OOX
		XOX XOX XOX XOX XOX XOX
		 0  -1   0   0   0   1

Pseudocode for heuristic Minimax

	Algorithm Minimax(board, player, depth):

	if board is win for X, then return 1000000.
	    else if board is win for O, then return -1000000.
	    else if board is tie game, then return 0.
	    else if depth = 0, then return Heuristic(board, player).
	end of if
	            { -infinity if player is X
	let best be {
	            {  infinity if player is O

	for each legal move on board, do:
	  Make move on board.
	  let value be Minimax(board, other player, depth - 1).
	  Undo move from board.

	  if player is X and value > best, then let best be value.
	      else if player is O and value < best, then let best be value.
	  end of if
	end of loop
	return best.

 

Alpha-beta search

Brilliant observation: Sometimes entire subtrees are irrelevant.

(In above case, before we get to C, we know A is at least 0. And B can't be more than 0 (since the left way gets 0, and O chooses a minimum). So we won't choose B, and so there's no point in looking at C.

 

Other tricks

 

Other examples of search

This sort of search can be used for any problem that can be described by a set of rules for legal "moves".
As one example, modern compilers sometimes employ searches for the most efficient machine code for a given program.

As another example, contra dances have a standard repertoire of moves, most of which can only be started from certain positions, and which leave the dancers in certain other positions.
So one could build a Random Contra Dance Generator.