Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created November 4, 2022 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzhin-dmitry/bb285812a8aa8e419d5d0b9a7cdc5a03 to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/bb285812a8aa8e419d5d0b9a7cdc5a03 to your computer and use it in GitHub Desktop.
def minimax(node, depth, min, max): #we add min and max - these are thresholds for pruning
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, best_evaluation, max) #just like we did before, but also update tresholds for pruning
if cur_evaluation > best_evaluation: #evaluate "cost" of a possible move but recursively decrease depth
best_evaluation = cur_evaluation
if cur_evaluation > max: #if we exceed treshold than we can stop computing this branch of a tree (make pruning)
return max
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, min, best_evaluation) #just like we did before, but also update tresholds for pruning
if cur_evaluation < best_evaluation: #if we define current node as min-node (i.e. we play with white stones and try to minimize cost)
best_evaluation = cur_evaluation
if cur_evaluation < min: #if we exceed treshold than we can stop computing this branch of a tree (make pruning)
return min
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