Learn

Amazing! Our minimax() function is now returning a list of [value, move]. move gives you the number you should pick to play an optimal game of Tic-Tac-Toe for any given game state.

This line of code instructs the AI to make a move as the "X" player:

select_space(my_board, minimax(my_board, True)[1], "X")

Take some time to really understand all of the parameters. Why do we pass True to minimax()? Why do we use [1] at the end of minimax()?

Instructions

Take some time to play a game against the computer. If you’re playing with "X"s, make your move as "X", and then call minimax() on the board using is_maximizing = False. The second item in that list will tell you the AI’s move. You can then enter the move for the AI as "O", make your next move as "X", and call the minimax() function again to get the AI’s next move.

You can also try having two AIs play each other. If you uncomment the code at the bottom of the file, two AI will play each other until the game is over. What do you think the result will be? The file will run for about 15 seconds before showing you the outcome of the game.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?