Key Concepts

Review core concepts you need to learn to master this subject

Minimax algorithm game representation

When writing the minimax algorithm, a game is modeled as a tree. Different elements of the game (as the current state and all possible moves) are represented as different parts of the tree. This visual representation of the game is a great aid in order to implement the minimax algorithm.

Minimax
Lesson 1 of 2
  1. 1
    Have you ever played a game against someone and felt like they were always two steps ahead? No matter what clever move you tried, they had somehow envisioned it and had the perfect counterattack. T…
  2. 2
    For the rest of this exercise, we’re going to be writing the minimax algorithm to be used on a game of Tic-Tac-Toe. We’ve imported a Tic-Tac-Toe game engine in the file tic_tac_toe.py. Before start…
  3. 3
    An essential step in the minimax function is evaluating the strength of a leaf. If the game gets to a certain leaf, we want to know if that was a better outcome for player “X” or for player “O”. …
  4. 4
    We now know that we can evaluate the leaves of a game tree, but how does that help us? How are we going to use those values to find the best possible move for a game state that isn’t a leaf? Let’s…
  5. 5
    One of the central ideas behind the minimax algorithm is the idea of exploring future hypothetical board states. Essentially, we’re saying if we were to make this move, what would happen. As a re…
  6. 6
    We’re now ready to dive in and write our minimax() function. The result of this function will be the “value” of the best possible move. In other words, if the function returns a 1, that means a mov…
  7. 7
    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 m…
  8. 8
    Right now our minimax() function is returning the value of the best possible move. So if our final answer is a 1, we know that “X” should be able to win the game. But that doesn’t really help us &m…
  9. 9
    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 co…
  10. 10
    Nice work! You implemented the minimax algorithm to create an unbeatable Tic Tac Toe AI! Here are some major takeaways from this lesson. * A game can be represented as a tree. The current state of …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo