Creating a Computer Player

On this page, you'll make the computer play against the human player.

The game currently allows two human players to take turns playing. But you don't really need a computer to keep track of moves for two human players. We want the human player to take turns with a computer player that will move automatically. To simplify the project, we'll assume that the human player will always move first (playing X) and that the computer is playing O.

For now, you'll create a computer player that moves to random positions on the board. (It won't be a very good player, but you'll fix that once the structure of the code for the computer's move is in place.) Once the program decides where it wants to move, you'll use broadcast to send a message to tell the clone with the matching position number to record a move. (For the human player, when I am clicked serves this purpose, but the computer's moves aren't prompted by the user clicking a square.)

Recall that clones are temporary copies of a sprite. You learned about them in Unit 2 Lab 4 Page 4: Building a Tic-Tac-Toe Board.
broadcast when I receive message

Broadcast sends out a message to all the sprites in the project. Any scripts that start with when I receive and have a matching message in the input slot will run.

The message reporter reports the most recent message sent out.

  1. If it isn't open already, open your U5-TicTacToe project.
  2. This had said the following, but it's not right to put it at the end because then it is triggered after the "This space has been played. Try another space." message. How's this change? --MF, 9/6/18
  3. At the end of your when I am clicked script, use broadcast with a random number...
  4. After your code for Player X's turn, use broadcast with a random number from 1 to 9 as the message. (The computer will move in the corresponding square, once you've written the code to handle the message.)
  5. Then create a when I receive script that will accept any message as input. If the position number for the clone matches the message, make the computer move in the correct clone's space and check for a win or a tie.
  6. Instead of copying code from when I am clicked to when I receive, move that code into a custom block and call the custom block from both scripts. This is one of the ways to use abstraction in programming: don't have two copies of the same piece of code; instead, give it a name.
    If your computer player uses the wrong costume when it plays in the last square, it's because the parent sprite has the same position number. Revise make board, and ensure that the parent's position number isn't 1-9 so that it doesn't conflict with a clone.
  7. Take turns with your partner playing Tic-Tac-Toe against the computer. Fix any bugs you find.
  8. In the first move of the first time I tried this today, the computer played in the same space as I had just played, so I fixed that (knowing, as the curriculum designer that I don't really have to because of what comes next, but because the student won't know what's next), but then I noticed that when X wins, which it generally will against a random player, O then moves again--potentially even "winning" after me. So I tried to fix that, and after trying for a bit, I ended up deciding to stop and write about this potential time sink rather than sinking my own time into it. What to do? --MF, 9/6/18

    Also: we have no note about removing the Player O code, which we might consider adding, and the orange box in #3 is too late; I'd already done copy and paste before I saw it. Our directions really inspire "just doing what it says without thinking" I'm not sure what to do about that... (BTW, should we have said that earlier? I can't find a good place because we build up the code slowly, but I wondered it.) --MF, 9/6/18

Instead of having the computer move at random, you will start to give the computer some strategy. Unlike people, all squares on the tic-tac-toe board are not created equal. Playing in the center (position 5) or on a corner (positions 1, 3, 7, or 9) is better than playing on an edge (positions 2, 4, 6, or 8).
Tic-tac-toe grid showing happy faces in the corners and center and sad faces on the four edges

  1. Talk with Another Pair Why is playing in the center or a corner better than an edge? Develop a short, logical, and convincing explanation. Compare your explanation with that of other students.
  2. Create a best empty square block that reports the best position on the board that is not yet filled. Use it as input to your broadcast instruction in place of random.
  3. Take turns playing games against the computer. Make sure the computer player only choses an edge if all of the corners and the center are already filled.
  4. Now Is a Good Time to Save
NEXT YEAR
You will work on this project more in Unit 5 Lab X: Tic-Tac-Toe with a Strategic Computer Player.