Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created November 4, 2022 17:27
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/770e3d2ac18d6bef42775defd317e6d4 to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/770e3d2ac18d6bef42775defd317e6d4 to your computer and use it in GitHub Desktop.
def minimax(node): #Vanilla minimax with no pruning. node - current vertex in tree, means 'board configuration'
if node.isLeaf(): #if node appears to be leaf (player win/end of possible moves)
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(node) #evaluate "cost" of a possible move
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(node) #evaluate "cost" of a possible move
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