FAQ: Minimax - Recursion In Minimax

This community-built FAQ covers the “Recursion In Minimax” exercise from the lesson “Minimax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Machine Learning

FAQs on the exercise Recursion In Minimax

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How long does the recursive function in part 1 normally take to run? Mine seems to have been running for quite a while, which I guess is normal for recursive sometimes, and I feel like I shouldn’t be in a infinity loop.

Hi, Can someone explain how the second minimax() function gets executed inside the “for move” loop?
I don’t get how minimax(new_board, not is_maximizing) is executed while being in the “for move” loop. What’s the “hypothetical_value” value? It seems to me like it should be infinite loop

def minimax(input_board, is_maximizing):
  # Base case - the game is over, so we return the value of the board
  if game_is_over(input_board):
    return evaluate_board(input_board)
  if is_maximizing == True:
    best_value = -float("Inf")
    symbol = "X"
  else:
    best_value = float("Inf")
    symbol = "O"
  for move in available_moves(input_board): #list possible moves
    new_board = deepcopy(input_board) #create a new board to track changes
    select_space(new_board, move, symbol)#make a hypothetical move
    hypothetical_value=minimax(new_board, not is_maximizing)#2nd move gain?
    if is_maximizing==True:
      if hypothetical_value>best_value:
        best_value=hypothetical_value
    elif is_maximizing==False:
      if hypothetical_value<best_value:
        best_value=hypothetical_value
  return best_value
1 Like

It doesn’t end in an infinite loop because we know it will eventually get to a resolution of the game so we will force it to get to the first if statement in the function and return a value there that will then work it’s way back up the calls.