About Tic Tac Toe Game:
In this tutorial, we will create your own version of Tic-Tac-Toe scratch game also known as Noughts and Crosses . Tic-Tac-Toe is normally played with two players. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of their marks on the board in a row, column, or diagonal, they win. When the board fills up with neither player winning, the game ends in a tie.
Video Tutorial on How to make Tic Tac Toe Game on scratch
In this lesson, we will create Halloween version of Tic Tac toe with Pumpkins and Bats.
Lets sketch out the Tic Tac Toe Game:
- Creating Tic Tac Toe Board
- Keep Track of Moves:
- Checking for a winning line
- Checking for Ties
Lets start with a two player game, where each player takes a turn to put Pumpkin or a Bat into a box and if you get a line of Pumpkin’s or Bat’s you win!.
Tic Tac Toe Scratch Game Code:
Creating Tic Tac Toe Board
For this game we need 9 sprites – one for each box on the board. Name your sprite something which makes sense like, Box1, Box2, Box3, Box4, Box5, Box6, Box7, Box8, Box9. Each sprite contains three costumes: Blank, Pumpkin and Bat. The player clicks one of the nine squares, and that sprite changes its costume to display the move.
Program each sprite so that, If player clicked the sprite (Box), switch costume to ‘Pumpkin’ or ‘Bat’ and mark player played the box. After the player makes a move, the program checks whether they won or caused a tie, and then the game switches turns.
Keep Track of Moves:
As each play is made it will be necessary for you to track what boxes have been played, not only to ensure a player doesn’t play that box again, but also because you need to know if there is a winning line.
Create variable called A1, A2, A3, B1, B2, B3, C1, C2, C3 to represent each sprite, to track which player played which box.
First, you must figure out how to represent the data in a variable. The data are either ‘Pumpkin’ for the player 1, ‘Bat’ for the player 2, or a -1 for a blank space. Data in the each variable represents the state of the board.
The variable A1, A2, A3, B1, B2, B3, C1, C2, C3 keep track of whether the each box in Board is empty or filled with an ‘Pumpkin’ or ‘Bat’.
Checking For The Winner:
There are eight possible ways to win at Tic-Tac-Toe: you can have a line across the top, middle, or bottom rows; you can have a line down the left, middle, or right columns; or you can have a line across either of the two diagonals.
To detect wins and ties, the program needs a way to test the state of the whole gameboard.
By default all the variables are set to -1 (i.e, for Blank Box).
Create a subprogram to detect a win.
For a player 1, there are eight ways to win and each corresponds to 3 ‘Pumpkin’s in a row or column or diagonal.
For example if A1 =A2=A3 for player 1, then switch costume to ‘Pumpkin wins’ Or else switch costume to ‘Bat wins’. Likewise check for all eight possible winning moves.
Our created subprogram’s parameters will store all of the possible winning triples so, when It receive message ‘check for winner’ it will check code for which combinations are valid.
Checking for Ties
What if game end with neither player has won?
i.e, when there are no more empty boxes but neither player has won, when that happens switch costume to tie.