An Intro to
Programming
with Python

Fall 2006
course
navigation

nov 19

I put another turtle graphics picture in the turtle graphics folder.
In class: start adapting the tic-tac-toe program I wrote before into one that really knows how to play.
# Recursive search for best move from current position. # Returns 'value' of board position and best move as a pair. # The value is assigned by a min/max algorithm that sees the entire tree of possible moves. # Quicker wins have a higher value. # The values for X and O are opposites; one positive and one negative. function value_and_best_move(board, whose_turn): if board.winner == whose_turn: return 9 - move_number # bigger for earlier win elif board.winner == other_turn: return -(9 - move_number): else: # Can't tell; look at next level down in the tree results = [] for move in board.all_possible_moves(): board.make_move(move) results.append(value_and_best_move(board, other_turn)) board.undo_move(move) return maximum(results)
http://cs.marlboro.edu/ courses/ fall2006/python/ notes/ nov_19
last modified Friday November 17 2006 7:06 am EST