So I was looking up Mini-max for a Tic-Tac-Toe Game, but couldn\'t understand how the recursion worked? Okay, so basically here are my questions:
We'll use your tic-tac-toe as an example first.
Looking at your pseudocode:
max(a, b) is any function that returns the larger of a or b. This is usually provided by a math library or similar.depth is the maximum depth to which you will search.1 for a board position that wins for the player doing the analysis, -1 for a board position that wins for the other player, and 0 for any inconclusive position. In general, you'll have to cook up a heuristic yourself, or use a well-accepted one.If you haven't worked with graphs or trees yet, I suggest you do so first; the tree primitive, in particular, is essential to this problem.
As an answer to a comment in this thread asking for an example of determining whose turn it is for a given node, I offer this pseudo-Python:
who_started_first = None
class TreeNode:
def __init__(self, board_position = EMPTY_BOARD, depth = 0):
self.board_position = board_position
self.children = []
self.depth = depth
def construct_children(self, max_depth):
# call this only ONCE per node!
# even better, modify this so it can only ever be called once per node
if max_depth > 0:
### Here's the code you're actually interested in.
if who_started_first == COMPUTER:
to_move = (COMPUTER if self.depth % 2 == 0 else HUMAN)
elif who_started_first == HUMAN:
to_move = (HUMAN if self.depth % 2 == 0 else COMPUTER)
else:
raise ValueError('who_started_first invalid!')
for position in self.board_position.generate_all(to_move):
# That just meant that we generated all the valid moves from the
# currently stored position. Now we go through them, and...
new_node = TreeNode(position, self.depth + 1)
self.children.append(new_node)
new_node.construct_children(max_depth - 1)
Each node is capable of keeping track of its absolute depth from the 'root' node. When we try to determine how we should generate board positions for the next move, we check to see whose move it is based on the parity of our depth (the result of self.depth % 2) and our record of who moved first.