« Back to Main Page

Machine Learning - Tic Tac Toe - Mentor Guide

From Bold Idea Knowledgebase


In this project students will take an existing Tic Tac Toe game written in Python and improve its AI using machine learning. In previous projects, students have trained the AI through the ML4K website. In this project, students will be using a new function, `model.


Make sure the ml4k library is up-to-date

You'll need ml4k >= 0.7 installed. You can verify this in the Python IDE (Thonny) by going to Tools -> Installed Packages. To update on chromebooks, you can run the Bold Idea Update app. Otherwise, you can open a terminal window and run sudo pip install --upgrade ml4k (on chromebooks, the sudo password is just "boldidea").

Step 1 - How do you play Tic Tac Toe?

Start with a group discussion and talk through Page 1 of the worksheet. The point is to get students to think about *how* they make a decision on what the next best move is.

In order to make that decision, they need to know *two* things:

  1. What is the current state of the board?
  2. Given the state of the board, what is the next best move?

We will convert these 2 things into numbers so that we can use them as examples to train a machine learning model how to win at Tic Tac Toe.

Step 2 - Download and explore the game code

Have students download the code using the link on the worksheet.

Important: Once you download `tictactoe.py`, don't open it just yet. You first need to open the file manager and move the file to your "Linux Files" folder (this only applies to Chromebooks)

Once you've downloaded the file, use the Python IDE to open it and explore the code.

The 2 variables they'll be using the most are AI_MODE and TRAINING . Make sure they understand what these do by reading the code comments.

Proceed with the instructions as outlined in the worksheet, and pay attention to the question prompts. Take time to talk through these w/ the students and make sure they understand what's going on.

Step 3 - Set up the ML project

This section is pretty self-explanatory in the worksheet. If you want the boxes to line up in the way they're pictured in the worksheet, you may need to resize the browser window.

There are two parts to set up:

1: Set up the training data values This represents the board state values that will be provided with each training example.

2: Set up the labels (training buckets) This represents the next best move based on a given board state. As we train using Python, these will begin to fill up with examples.

Step 4 - Train the AI by playing

In this step students will define the function that populates the training data. Follow the worksheet step-by-step, being sure to read through everything. Pay special attention to the callouts.

Step 5 - Use the ML model

In this step students will add another function that will be called when AI_MODE is set to "ml". This is how you can test your model to see how smart it is.

How to improve the ML model : If TRAINING mode is on, the ML model learns from whomever the winner is. If player O wins (regardless of whether it's the computer or a human), that player's moves will be uploaded as training examples.

If the ML AI makes a bad move, you can train it to make the correct move by doing the following:

  1. Take note of each move made during the game, and the point at which O made the mistake.
  2. Set AI_MODE to None (no quotes), and TRAINING to True
  3. Play as both players using the same moves just before player O made the mistake. Now, make the correct moves so that O wins the game . These winning moves will be added to the training data.
  4. Go back to the ML4K website and re-train the model (just press the blue button).
  5. Then go back to your script, change AI_MODE to "ml" to see if the model has improved!