Console Tic-Tac-Toe with Minimax AI
Build tic-tac-toe in C for the terminal, playable by two people or against the computer, where the computer uses the minimax algorithm: it looks ahead at every possible sequence of moves and always picks the best one, so it never loses. The board is a small 2D array, moves are typed as a number from 1 to 9, and the game checks for wins and draws after each move. It's a classic first C project with a taste of AI: you'll learn arrays, functions, reading and validating input safely, loops and conditions, and recursion, the key idea behind minimax.
- Estimate
- ~7h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
stdio.h
Project roadmap
- 01
Represent and draw the board
~1hRepresent the board as a 3×3 char array (with ' ', 'X' and 'O'), and write a function that prints it with grid lines and cell numbers 1 to 9 in empty cells, so players know what to type. Set up a Makefile with -Wall -Wextra. Done when: the program prints an empty numbered board.
- 02
Read and validate moves
~1hRead a line with fgets, convert it with strtol, and accept only numbers from 1 to 9 pointing to an empty cell, re-asking with a clear message otherwise. Convert the number to a row and column, and place the current player's mark. Done when: invalid input (letters, 0, 10 or a taken cell) is refused without crashing.
- 03
Detect wins and draws
~1hWrite a function that checks all 3 rows, 3 columns and 2 diagonals for three identical marks and returns the winner, and another that detects a full board with no winner. Done when: every winning line and a draw are recognized in small test functions.
- 04
Implement minimax
~2hWrite a recursive minimax function: if the game is over, return +10 for a computer win (minus the depth, so faster wins score higher), -10 for a player win (plus the depth), and 0 for a draw; otherwise try every empty cell for the side to move, call minimax for the other side, undo the move, and return the best score. A best_move function uses it to choose the computer's move. Done when: the computer never loses, and it wins whenever you make a mistake.
- 05
Build the game loop and modes
~1hShow a menu to choose two players or against the computer (and who goes first), run the game loop alternating turns, announce the result, and ask whether to play again. Keep a running score of wins and draws for the session. Done when: both modes work, and games can be replayed.
- 06
Test it
~1hWrite a test program with assert: win detection for each line, draw detection, and minimax choosing the winning move when one exists and blocking the opponent's winning move. Also check that the computer never loses by playing it against every possible sequence of human moves (a small recursive test). Done when: all tests pass.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack