Page 2: Creating a Computer Player

Unit 5, Lab 6, Page 2

MF: Project still needs to be wrapped up and updated online

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.

Have the Computer Move Randomly

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 3 Lab 1 Page 5: Building a Tic-Tac-Toe Board.

broadcast when I receive

You first saw broadcast and when I receive on Unit 1 Lab 2 Page 2: Making Programs Talk.

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 and also be able to use any data broadcasted. If you expand the broadcast block by clicking the right-pointing triangle, you can broadcast over any data that you want the sprites to recieve other than the message.

If you expand the when I receive block, by clicking the right-pointing triangle, you can use the data variable to access the broadcasted data.
when I receive () (data)

  1. If it isn’t open already, open your U5L6-TicTacToe project.
  2. Find the script that starts when I am clicked. That script handles a move by the human player. Now start a new script next to it:
    when I receive (any message) : if (message) = (square number) [nothing in script slot yet]
  3. Create an abstraction to contain everything inside the if statement from the when I am (clicked) script and use it in both scripts.
    when I am (clicked) {
if ((costume #) = 1) {
move in this square
}
}    when I am (clicked) {
if ((message) = (square number)) {
move in this square
}
}
  4. In order to debug the when I receive script, edit the move in this square block. After your code for Player X’s turn, temporarily pick a random vacant square and use broadcast with “computer’s turn!” as the message and use the random vacant square’s number as the data. (If your when I receive script is working, the computer will move in the corresponding square.)

  5. Take turns with your partner playing Tic-Tac-Toe against the computer. Fix any bugs you find.

Give the Computer a Simple Strategy

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? 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 chooses an edge if all of the corners and the center are already filled.
  4. Save your work