Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created November 4, 2022 17:46
Show Gist options
  • Save ronzhin-dmitry/f2666b861002eec49d9e0f5bd9da0520 to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/f2666b861002eec49d9e0f5bd9da0520 to your computer and use it in GitHub Desktop.
def minimax(node, depth): #node - current vertex in tree, means 'board configuration'. depth - recursion depth count.
if node.isLeaf() or depth == 0: #if node appears to be leaf or recursion limit exceeded
return evaluate_board(node) #some evaluation function for node in a tree (returns some value)
children = getChildrenConfigurations(node) #some function to get all possible children nodes (i.e. possible configurations) from current node
if node.type() == max_node: #if we define current node as max-node (i.e. we play with black stones and try to maximize cost)
best_evaluation = MIN_VALUE #initialize with minimum possible value
for child_node in children:
cur_evalutaion = minimax(child_node, depth - 1) #evaluate "cost" of a possible move but recursively decrease depth
if cur_evaluation > best_evaluation: #in case this move is more preferable, we memorize it
best_evaluation = cur_evaluation
return best_evaluation #return best cost found
if node.type() == min_node: #if we define current node as min-node (i.e. we play with white stones and try to minimize cost)
best_evaluation = MAX_VALUE #initialize with maximum possible value
for child_node in children:
cur_evalutaion = minimax(child_node, depth - 1) #evaluate "cost" of a possible move but recursively decrease depth
if cur_evaluation < best_evaluation: #in case this move is more preferable, we memorize it
best_evaluation = cur_evaluation
return best_evaluation #return best cost found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment