Skip to content
Laviyx Learn

Try

Mini project

Unbeatable Tic-Tac-Toe with Minimax

A Python tic-tac-toe game with a computer player that uses the minimax algorithm and alpha-beta pruning, so it never loses, plus easier levels for beginners.

Intermediate 1–2 weeks 1 studentUpdated

PythonTkinter or the terminalpytest

The problem

Game AI starts with one idea: look ahead at the possible moves and choose the best one, assuming the opponent also plays their best. Tic-tac-toe is small enough to search completely, so it is the perfect place to learn the minimax algorithm that, with improvements, also powered early chess programs.

Features to build

  • Play human versus computer and human versus human
  • Store the board as a list of 9 cells and check all 8 winning lines
  • Validate input: only empty cells, numbers 1 to 9, and clear messages for mistakes
  • A hard level using minimax that never loses
  • Easy and medium levels: random moves, or minimax that sometimes picks a random move
  • Alpha-beta pruning, with a counter showing how many positions were searched with and without it
  • Choose who goes first, and keep a running score of wins, losses and draws

Build plan

  1. 1

    Build the game

    Write functions to print the board, list empty cells, make a move and find a winner or draw. Test them before adding any AI.

  2. 2

    Write minimax

    Recursively try every empty cell. Score a finished game +1 for a computer win, −1 for a loss and 0 for a draw; the computer maximises the score and the human minimises it.

  3. 3

    Prefer quicker wins

    Subtract the depth from winning scores (and add it to losing ones), so the computer wins as fast as possible and delays a loss.

  4. 4

    Add alpha-beta pruning

    Pass the best scores found so far for each side, and stop exploring a branch when it cannot change the decision. Count positions searched to show the saving.

  5. 5

    Test it

    Write a test that plays the hard computer against every possible sequence of human moves and checks it never loses.

Learn these first

Each part of the project is taught in one of these free lessons.

Make it stand out

  • A Tkinter window with clickable cells
  • Cache positions already scored, using a dictionary keyed by the board
  • Extend the game to a 4×4 board and add a depth limit with a scoring function

Viva and interview questions

Try to answer each one aloud before you open it.

  • How does the minimax algorithm work?

    It builds the tree of possible moves. At the computer's turns it picks the move with the highest score; at the opponent's turns it assumes they pick the lowest. Scores come from finished games and are passed back up the tree.

  • What does alpha-beta pruning change?

    It skips branches that cannot affect the final choice, because the player above already has a better option. It always returns the same move as plain minimax, just faster.

  • How big is the tic-tac-toe game tree?

    There are 255,168 possible games and 5,478 different legal board positions, so the whole tree can be searched quickly. Chess is far too large for that, which is why chess programs limit the depth and estimate positions.

  • What happens when two perfect players play?

    The game is always a draw. That is why the hard level never loses: the best a human can do against it is draw.

What to put in your project report

  1. Abstract: the problem, your solution and the result in one paragraph.
  2. Introduction and problem statement: who has the problem and why it matters.
  3. Requirements: the features (functional) and qualities such as speed and security (non-functional).
  4. Design: architecture diagram, database tables or data flow, and screen layouts.
  5. Implementation: the main modules and the most interesting code, explained.
  6. Testing: the test cases you ran, with inputs, expected and actual results.
  7. Results: screenshots or numbers that show the project working.
  8. Conclusion and future scope: what you achieved and what you would add next.

Your college may give its own format. Follow that first, and use this list to check nothing is missing.

All project ideas →