10.13 Build a TicTacToe Game
Tic‐Tac‐Toe is a very simple two players game. So only two players can play at a time. Or you could build one player play with computer.
This game is also known as Noughts and Crosses or Xs and Os game. One player plays with X and the other player plays with O. In this game we have a board consisting of a 3X3 grid.
11.1 Design the Game
1. Figure out the rules of Tic Tac Toe Game
- Two plays control the keyboard in ture
- The game board is 3X3 grid
- The player who first get 3 dot in one row will win
2. Design the Game UI
- Game Start, Game Over
- Board, back ground
- Chess: Nothing, X, O
- User input position: n or (x,y)
ex. position of 2nd column and 3rd row
- Option 1: 8 (position #8 of total positions)
- Option 2: (x,y)
3. Design the Game Data Structure
-
Option 1: Use List of object with value 0/1 for each position
[0,0,0,0,0,0,0,0,0]
-
Option 2: User 3 Array
Row1 [0,0,0] Row2 [0,0,0] Row3 [0,0,0]
4. Design the Logic to decide who won the game
-
for Option 1, need check all below possibility
Row[1]=Row[2]=Row[3] or Row[4]=Row[5]=Row[6] or Row[7]=Row[8]=Row[9] Row[1]=Row[4]=Row[6] or Row[2]=Row[5]=Row[8 ]or Row[3]=Row[6]=Row[9] Row[1]=Row[5]=Row[9] or Row[3]=Row[5]=Row[6]
-
for Option 2, check below
Row1[1]=Row1[2]=Row1[3] or Row2[1]=Row2[2]=Row2[3] or Row3[1]=Row3[2]=Row3[3] Row1[1]=Row2[1]=Row3[1] or Row1[2]=Row2[2]=Row3[2] or Row1[3]=Row2[3]=Row3[3] Row1[1]=Row2[2]=Row3[3] or Row1[3]=Row2[2]=Row3[1]
11.2 Implementation Plan
- Load the UI object/Image
- Add the code for UI interactive
- Add the code for Data Structure
- Add the code for winning Judgment Logic
Challenge
- Change the one of the player to AI
- Coding the Logic of the AI player to find the best place to put.
Examples
- scratch TicTacToe https://scratch.mit.edu/projects/199155900/
- scratch TicTacToe https://scratch.mit.edu/projects/13969717/