This community-built FAQ covers the “Which Move?” exercise from the lesson “Minimax”.

**Paths and Courses**

This exercise can be found in the following Codecademy content:

Machine Learning

##
FAQs on the exercise *Which Move?*

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* () 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** () below!

Agree with a comment or answer? **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!

I wondered why at the end of this section, the result for the following command:

print(minimax(o_winning, False)) or print(minimax(o_winning, True))

I get the same result [-1 , 4]

when the board is:

o_winning = [

[“X”, “X”, “O”],

[“4”, “X”, “6”],

[“7”, “O”, “O”]

]

So I understand that this means that O will win the game, however why would it be 4 if it was O’s next move? Surely it would be either 6 or 7 because that wins the game?

Why is the position 4 the same whether you’re maximising for X or O?

4 Likes

I was wondering the same thing. I think it might have something to do with the fact that the end goal of the program isn’t necessarily to win, but to maximize the number of winning states and minimize the number of losing states. Picking 4 results in two possible future win states for O, and none for X, as opposed to a singular winning state (in which O has won).

It is because the `best_move`

changes only when `move`

is `4`

in the first `for move`

loop. The `for move`

loop proceeds in the order of `4`

, `6`

, `7`

, so let’s follow the behavior of the program in this order. Whether `is_maximizing`

is `True`

or `False`

, the following things happen.

When `move`

is `4`

, `minimax(new_board, not is_maximizing)[0]`

returns `-1`

, so `best_value`

changes from minus or plus infinity to `-1`

and `best_move`

changes from `""`

to `4`

.

Next, when `move`

is `6`

, `minimax(new_board, not is_maximizing)[0]`

also returns `-1`

. Since both `best_value`

and `hypothetical_value`

are `-1`

this time, `best_move`

does not change (so it remains `4`

). The similar thing happens when `move`

is `7`

.

Therefore the function returns `[-1, 4]`

in the end.

1 Like

IMO, The minimax algorithm as coded produces the wrong result for the o_winning board.

o_winning = [

[“X”, “X”, “O”],

[“4”, “X”, “6”],

[“7”, “O”, “O”]

]

If playing as X (it is X’s turn) minimax returns [-1, 4] from minimax(o_winning, True)

But playing position 4 is actually the worst play for X because by playing in 4, X forces O to win the game since O can now only play in 6 or 7 and both result in O winning. X has a potential to tie the game if X instead plays in 6 or 7 leaving open the possibility for O to make a mistake by playing in 4. If a win is not possible for X then the minimax algorithm should prefer a path that might lead to leaf that is a tie over a path that leads only to leaves that are all losses for X. The best_value should have scored the path leading to leaves that are a tie over any path leading to only losses. The problem is that the algorithm presumes that O will always make the best possible move for O but in so doing X basically ‘gives up’ by playing position 4.

So my question is … how could minimax be updated so that in this case it would have made the better choice for X of 6 or 7 instead of the completely fatal choice of 4?

1 Like