11 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 Double Array
[ [0,0,0] [0,0,0] [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
Row[1][1]==Row[1][2]==Row[1][3] or Row[2][1]==Row[2][2]==Row[2][3] or Row[3][1]==Row[3][2]==Row[3][3]
Row[1][1]==Row[2][1]==Row[3][1] or Row[1][2]==Row[2][2]==Row[3][2] or Row[1][3]==Row[2][3]==Row[3][3]
Row[1][1]==Row[2][2]==Row[3][3] or Row[1][3]==Row[2][2]==Row[3][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.
Ref
- scratch example https://scratch.mit.edu/projects/199155900/
- My OOP TicTacToe example https://github.com/stoneskin/TicTacToe/
- python tic-tac-toe example https://www.askpython.com/python/examples/tic-tac-toe-using-python
- pygame tic-tac-toe example https://www.geeksforgeeks.org/tic-tac-toe-gui-in-python-using-pygame/