In this lab, you will continue your Tic-Tac-Toe project to make the program analyze the game board to detect wins and ties.
On this page, you'll prepare by allowing the program to record the moves that are made, and teach it the winning groups of three squares.
When you first wrote Tic Tac Toe, it was easy to detect illegal moves (moving to a square that's already wearing an X or O costume) because each sprite could test for that without knowing about the rest of the game board. But to detect wins and ties, the program needs a way to test the state of the whole game board, not just one individual piece. So, you'll use a global variable, , to keep track of whether the square in each position is empty or filled with an X or an O.
You learned about global variables on Unit 2 Lab 1 Page 4: Keeping Score with Global Variables.
Each sprite needs to know its own position on the board so that you can use the item of
block to replace that position with an X or an O. So, you'll create a sprite variable, , and each clone will have its own version of this variable.
A sprite variable is like a global variable in that it doesn't belong to a particular script, but it does belong to a particular sprite.
You learned about initializing variables on Unit 2 Lab 1 Page 1: Developing a Number Guessing Game with Script Variables.
You make a sprite variable similarly to how you make global variables:
To program a way to detect a win, you need to determine what counts as a win. You'll store each possible winning triple as a list of three position numbers (a triple). For example, this winning triple could be represented as because the win is in the 3rd, 5th, and 7th positions:
You could check for each possible win separately:
But that's way too much code, especially because you'll have to do something very similar again, later on, when the computer actually plays against you. Instead, you'll create a list to store all of the possible winning triples so you can check it against the current state of the game stored by board.