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.
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.)
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.
If you select "any message" from the when I receive
dropdown menu and you click the right-pointing triangle, you can use the data
variable to access the message.
when I am clicked
. That script handles a move by the human player. Now start a new script next to it:if
statement from the when I am (clicked)
script and use it in both scripts.Look in board.
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 that square number as the message. (If your when I receive
script is working, the computer will move in the corresponding square.)
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).
broadcast
instruction in place of random
.