Tic Tac Toe
We're gonna continue on our `Tic Tac Toe` game developed from last semester. This is a two-player game where each player takes turns to mark a square on a 3x3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
This time, we're gonna make some adjustments so that human players can play against the computer.
Starter Code
Click to view or download the starter code for this project.Code Refactor
Code refactoring is the process of restructuring existing code without changing its external behavior or functionality. It improves the internal structure, readability, and maintainability of the code, making it easier to understand, debug, and extend in the future.
Common Refactoring Techniques
- Extract Method: Pulling out a piece of code from a long function into its own method to reduce complexity and improve readability.
- Rename Variables: Giving variables meaningful names to improve clarity.
- Remove Duplicate Code: Eliminating redundant code by creating reusable functions or classes.
- Separation of Concerns: Use python modules to separate different functionalities into distinct files, making the codebase more organized and manageable.
Task
We're going separate the starter coderun.py
into multiple files:
game_logic.py
: Contains the game logic functions likecheck_win
andcheck_tie
.colors.py
: Contains the color constants used in the game.drawings.py
: Contains the drawing functions likeredraw
andbutton_rect
.main.py
The remaining code after moving functionsout. It contains the main running loop of the code
main.py
should look like after refactoring:
Please finish the rest of files
Complete Refactored Source Code
Player vs. Computer
To make the game more interesting, we're gonna add a computer player that can play against the human player. The computer player will make its move based on a simple algorithm that you will implement.
Computer Strategies
We've disucssed several strategies that the computer can use to make its move:-
The computer makes a move on the first empty location on the board.
-
The computer makes a random move on the board.
-
The computer makes a move to occupy the center of the board.
-
The computer makes a move to occupy a corner of the board.
-
The computer makes a move to occupy an edge of the board.
-
The computer makes a move that will lead to a win.
-
The computer makes a move to block the opponent from winning.
Nov. 24, 2024
We have no class next week. Have a good Holiday!!End of Year Assignment
Dec. 15, 2024
No class next two weeks, we'll resume on Jan. 4th, 2025We've talked about game menu screen this weekend. Basically, your game should have two views
- Menu Mode
- Game Mode
Menu Mode allows user to select among the three levels, namely Easy, Hard and Pro
Game Mode is where the game is played. The computer player will make its move based on the selected level.
Please find the latest source code here:Homework
Try to complete the game on your own during the break.
We'll discuss the solution on Jan. 4th, 2025.
Here's a demo of a finished game

Class Meet 01/04/2025
We've compleleted the following on class:- menu view button click
- navigation logic of two game views
- clicking on a level shows Game Board view
- clicking on Start Over shows Menu view
- Implement three strategies for computer move, namely, easy, hard and pro,
- Determine the computer strategy based on the user-selected level
Jan 11, 2025
Please find the Homework hereClass Announcement
- Jan 18, 2025: Chinese New Year Party at Chinese School
- Jan 25, 2025: Chinese New Year break
- Feb 1, 2025: Chinese New Year break