Page 5: Building a Tic-Tac-Toe Board
Unit 3, Lab 1, Page 5
In this project, you will develop a program that plays Tic-Tac-Toe as well as you do.
On this first page, you use clones to display a Tic-Tac-Toe board and let two human players enter moves.
The project is spread out over different units because there’s a lot to do, and because some steps require techniques you haven’t learned yet. On this page, you’ll learn about cloning a sprite (like copying it, only better). In the future, you’ll extend the project to detect wins and ties and, ultimately, to let the computer be one of the players.
One approach to building this game is to use multiple sprites; each square of the board is a separate sprite. Each sprite knows where it is on the board, and, once positioned, the sprites never move. The player clicks one of the nine squares, and that sprite changes its costume to display the move.
You will use clones to create the 3×3 Tic-Tac-Toe board.
A clone is a copy of a sprite that shares information with its parent sprite (the original sprite). For example, clones have copies of any scripts from the parent, and if the parent’s script is changed, then the clones’ scripts change too. However, changes you make to a clone are not shared with the parent, so you can do things like move each clone to a different position.
Clones start with the same position, costumes, and scripts of their parent. But clones made by a program…
- Don’t appear in the sprite corral.
- Are temporary. Clicking the stop button or green flag will delete all temporary clones in the project.
(There can also be permanent clones, which are created differently, but they’re not used in this project.)
In this project, you’ll need nine clones, one for each square of the Tic-Tac-Toe board. So, you’d need something like:
The actual code you write will be slightly more than this, but this is the central idea. You’ll need nine clones, and you will hide the parent. This will allow you to control the squares of the grid with blocks like:
-
Before clicking anything else, read this demonstration script with your partner. Discuss what will happen when you click the sprite.
This script will help you learn about clones, but it won’t be part of your finished project.
-
Then click the sprite on the stage, and compare what happens with what you expected.
- Notice that the parent sprite (not the clone) moves to the new random position after cloning itself. Look back at the code; why does this make sense?
-
Note that both the clones and the parent are sprites. Both are clickable (try it) and draggable (try that too). This is different from the effect of the
stamp
Pen block, which just leaves a picture of the sprite on the stage. -
The
create a clone
block takes an input because it can copy any sprite. In this project, there’s only one sprite at the beginning, so your choices are either myself or Square (the sprite’s name), which in this case, means the same thing.
-
Prepare to create your project:
-
Delete the
when I am clicked
demonstration script from exercise 3. -
Connect the
when
⚑clicked
block toward the bottom of the scripting area to the script just below it.
-
Read that script and notice…
-
The
makeBoard
block is empty. You’ll write it in the next problem. -
The variable X’s turn? will be
true
when it’s X’s turn to play orfalse
when it’s O’s turn.When alternating between two values, it’s convenient to use Booleans because you can use
not
to switch between them and you can useif
to test the variable. (Recall thatif
requires atrue
/false
input.)
-
The
-
Delete the
-
Edit the block
makeBoard
to set up nine clones in three rows of three.
Tips:
- The costumes are all 50 steps tall and 50 steps wide.
- Make sure the clones start out with the empty square costume.
-
Be sure to
show
the original sprite before cloning it, so that the clones will be visible. After cloning nine times to create the board,hide
the original sprite so it does not interfere with the game as a tenth square. -
Click if you need an additional hint about
makeBoard
.
-
Now finish up:
-
Write a
when I am clicked
script that will make each clone wear the proper costume when clicked: X or O depending on whose turn it is. - Decide how a square should behave when it is clicked while already wearing an X or O costume.
-
Make the parent sprite not-draggable so that the clone squares aren’t accidentally moved around when the player clicks.
-
Write a
- Play one or two games of Tic-Tac-Toe with your partner, and fix any bugs. Then, discuss what else you might want to add to the project.
You can leave comments in the project file to remind yourself of things you might want to do later.