📝 Overview
In this assignment, you'll create an interactive TicTacToe (Tic-Tac-Toe) game where a player
competes against the computer. This project will help you practice:
2D Lists : Representing the game board as a 3x3 grid
Nested Loops : Iterating through 2D structures
Conditional Logic : Checking for winning conditions across rows, columns, and diagonals
Functions : Organizing code into reusable components
Game State : Tracking board position and whose turn it is
Algorithm Design : Implementing basic computer AI (win-block-random strategy)
User Input Validation : Ensuring valid move selection
📥 Download Project Files
Click below to download all the files you need to get started:
⬇ Download All Files (ZIP)
🎯 Learning Objectives
By completing this assignment, you'll be able to:
Create and manipulate 2D lists to represent game state
Implement nested loops to process multi-dimensional data
Check complex winning conditions (rows, columns, diagonals)
Design and implement a game intelligence algorithm
Structure a complete game with proper turn-taking and state management
Validate user input and handle edge cases
Debug complex logic and test thoroughly
📋 Core Features (Required)
Your TicTacToe game must:
Display the board clearly with a 3x3 grid showing positions 1-9:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
Allow player to make moves by entering 1-9 for an available position
Check for valid moves — position must be 1-9 and unoccupied
Have computer AI that:
Tries to win — if computer can win next move, take it
Blocks player — if player can win next move, block them
Picks randomly — otherwise, choose a random empty cell
Detect winning conditions:
Any row with three of the same symbol
Any column with three of the same symbol
Main diagonal (top-left to bottom-right)
Anti-diagonal (top-right to bottom-left)
Detect draws — when board is full but no winner
Display game outcome clearly: Player Won / Computer Won / Draw
📋 Implementation Details
Board Representation:
Use a 3x3 2D list: board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
Empty cells contain ' ' (space), player uses 'X', computer uses 'O'
Position 1-9 maps to row/column indices:
Positions 1-3 are row 0, 4-6 are row 1, 7-9 are row 2
To convert position to indices: row = (position - 1) // 3, col = (position - 1) % 3
🚀 Suggested Function Structure
The scaffolding file includes 15 TODO items covering these functions:
initialize_board() — create empty 3x3 board
display_board(board) — print formatted board
get_player_move(board) — prompt and validate player input
is_valid_move(board, row, col) — check if position is empty
check_winner(board, player) — check all rows, columns, diagonals
is_board_full(board) — check for draw condition
get_computer_move(board) — AI strategy (win/block/random)
play_single_game() — main game loop
play_again() — prompt to play another round
main() — overall program flow
📁 Files in Your Assignment
tictactoe.py — Your main game file
with TODO markers to guide you
💡 Tips for Success
Start with display: Get the board displaying correctly first
Build incrementally: Complete one function at a time and test it
Use the helper functions: Break AI logic into separate functions for win/block/random
Test thoroughly: Play multiple games to verify:
Player wins work correctly
Computer blocks your winning moves
Computer tries to win
Draw detection works
Debug systematically: Add print statements to track board state during game
🚀 Getting Started
Download the files using the button above
Open tictactoe.py in VS Code
Read through the TODO comments to understand the game structure
Implement each function one at a time, following the TODO order
If you get stuck on a specific concept, check cheat_sheet.py
Test your game: python tictactoe.py
Play several games to verify all features work correctly
🎮 Example Game Flow
Welcome to TicTacToe! You are X, Computer is O.
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
Your move (1-9): 5
1 | 2 | 3
-----------
4 | X | 6
-----------
7 | 8 | 9
Computer chooses: 1
O | 2 | 3
-----------
4 | X | 6
-----------
7 | 8 | 9