Learn
Minimax
Recursion In Minimax

Nice work! We’re halfway through writing our `minimax()` function — it’s time to make the recursive call.

We have our variable called `best_value` . We’ve made a hypothetical board where we’ve made one of our potential moves. We now want to know whether the value of that board is better than our current `best_value`.

In order to find the value of the hypothetical board, we’ll call `minimax()`. But this time our parameters are different! The first parameter isn’t the starting board. Instead, it’s `new_board`, the hypothetical board that we just made.

The second parameter is dependent on whether we’re the maximizing or minimizing player. If `is_maximizing` is `True`, then the new parameter should be false `False`. If `is_maximizing` is `False`, then we should give the recursive call `True`.

It’s like we’re taking the new board, passing it to the other player, and asking “what would the value of this board be if we gave it to you?”

To give the recursive call the opposite of `is_maximizing`, we can give it `not is_maximizing`.

That call to `minimax()` will return the value of the hypothetical board. We can then compare the value to our `best_value`. If the value of the hypothetical board was better than `best_value`, then we should make that value the new `best_value`.

### Instructions

1.

Let’s make that recursive call!

Inside the for loop after calling `select_space()`, create a variable named `hypothetical_value` and set it equal to `minimax()` using the parameters `new_board` and `not is_maximizing`.

To help us check if you did this correctly, return `hypothetical_value` instead of `best_value`. We’ll change that return statement soon!

2.

Now that we have `hypothetical_value` we want to see if it is better than `best_value`.

Inside the for loop, write another set of `if/else` statements checking to see if `is_maximizing` is `True` or `False`

If `is_maximizing` is `True`, then `best_value` should become the value of `hypothetical_value` if `hypothetical_value` is greater than `best_value`.

If `is_maximizing` is `False`, then `best_value` should become the value of `hypothetical_value` if `hypothetical_value` is less than `best_value`.

Switch your return statements back to returning `best_value`.

3.

Wow! Great work, our minimax function is done. We’ve set up a couple of boards for you. Call `minimax()` three different times on the boards `x_winning`, and `o_winning`. In each of those boards, it’s `"X"`‘s turn, so set `is_maximizing` to `True`.

Print the results of each. What does it mean if the result is a `-1`, `0` or `1`?

You can also try running `minimax()` on `new_game`. This might take a few seconds — the algorithm needs to traverse the entire game tree to reach the leaves!