Week 3 Assignment

TicTacToe Game with Unbeatable Computer AI

📝 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:

Expected Time: 4–6 hours
Difficulty: Medium
Recommended: Week 3 (Nested Loops & 2D Lists)

📥 Download Project Files

Click below to download all the files you need to get started:

🎯 Learning Objectives

By completing this assignment, you'll be able to:

  1. Create and manipulate 2D lists to represent game state
  2. Implement nested loops to process multi-dimensional data
  3. Check complex winning conditions (rows, columns, diagonals)
  4. Design and implement a game intelligence algorithm
  5. Structure a complete game with proper turn-taking and state management
  6. Validate user input and handle edge cases
  7. Debug complex logic and test thoroughly

📋 Core Features (Required)

Your TicTacToe game must:

  1. Display the board clearly with a 3x3 grid showing positions 1-9:
     1 | 2 | 3
    -----------
     4 | 5 | 6
    -----------
     7 | 8 | 9 
  2. Allow player to make moves by entering 1-9 for an available position
  3. Check for valid moves — position must be 1-9 and unoccupied
  4. 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
  5. 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)
  6. Detect draws — when board is full but no winner
  7. Display game outcome clearly: Player Won / Computer Won / Draw

📋 Implementation Details

Board Representation:

🚀 Suggested Function Structure

The scaffolding file includes 15 TODO items covering these functions:

📁 Files in Your Assignment

💡 Tips for Success

  1. Start with display: Get the board displaying correctly first
  2. Build incrementally: Complete one function at a time and test it
  3. Use the helper functions: Break AI logic into separate functions for win/block/random
  4. Test thoroughly: Play multiple games to verify:
    • Player wins work correctly
    • Computer blocks your winning moves
    • Computer tries to win
    • Draw detection works
  5. Debug systematically: Add print statements to track board state during game

🚀 Getting Started

  1. Download the files using the button above
  2. Open tictactoe.py in VS Code
  3. Read through the TODO comments to understand the game structure
  4. Implement each function one at a time, following the TODO order
  5. If you get stuck on a specific concept, check cheat_sheet.py
  6. Test your game: python tictactoe.py
  7. 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