nov 19
In class: start adapting the tic-tac-toe program I wrote before
into one that really knows how to play.
- Perhaps simplest to leave it as it is, and import it into a another file.
- Create some new objects that inherit from those, with slightly different behavior.
- Recursive min/max search for best move looks something vaguely like this:
# 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)