Skip to content
C project #07Beginner

Tic-Tac-Toe

A two-player console Tic-Tac-Toe with move validation, win and draw detection, and a running score across games.

Estimated time
~3 hours to build
Files
5 files · 364 lines
Technologies
C11, <stdio.h>

Introduction

Tic-Tac-Toe is small enough to finish in an afternoon but has real game logic: a board, turns, invalid moves and win conditions. It is an ideal first exercise in modelling state with a 2D array.

Problem statement

Implement Tic-Tac-Toe for two players at one keyboard. Players enter cell numbers 1–9; the program rejects occupied or invalid cells, detects a win on any row, column or diagonal, detects a draw, and keeps score across several games.

Objectives

  • Represent game state in a 2D char array
  • Convert between cell numbers and row/column indexes
  • Check all eight winning lines with helper functions
  • Loop over turns and games

Features

  • Numbered board display
  • Validation of out-of-range and occupied cells
  • Win detection on rows, columns and diagonals; draw detection
  • Score kept across repeated games

Concepts demonstrated

  • 2D arrays
  • Functions
  • Game loop
  • Pointers to array elements

System architecture

board[3][3] holds '1'–'9' for empty cells or 'X'/'O'. play runs one game: draw, read a cell, convert with (cell−1)/3 and (cell−1)%3, validate, place, test wins(player), then switch players. wins checks three rows, three columns and two diagonals with the line helper. main repeats games and keeps the score.

Folder structure

TextProject tree
tic-tac-toe/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
Board reset and drawing, win checking, the turn loop in play, and the score loop in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Tic-Tac-Toe ===
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Player X, choose a cell (1-9):
1 | 2 | 3
---+---+---
4 | X | 6
---+---+---
7 | 8 | 9
Player O, choose a cell (1-9):
O | 2 | 3
---+---+---
4 | X | 6
---+---+---
7 | 8 | 9
Player X, choose a cell (1-9):
O | 2 | 3
---+---+---
4 | X | 6
---+---+---
7 | 8 | X
Player O, choose a cell (1-9):
Cell 5 is already taken.
O | 2 | 3
---+---+---
4 | X | 6
---+---+---
7 | 8 | X
Player O, choose a cell (1-9):
O | 2 | O
---+---+---
4 | X | 6
---+---+---
7 | 8 | X
Player X, choose a cell (1-9):
O | X | O
---+---+---
4 | X | 6
---+---+---
7 | 8 | X
Player O, choose a cell (1-9):
O | X | O
---+---+---
4 | X | 6
---+---+---
O | 8 | X
Player X, choose a cell (1-9):
O | X | O
---+---+---
X | X | 6
---+---+---
O | 8 | X
Player O, choose a cell (1-9):
O | X | O
---+---+---
X | X | O
---+---+---
O | 8 | X
Player X, choose a cell (1-9):
O | X | O
---+---+---
X | X | O
---+---+---
O | X | X
Player X wins!
Score - X: 1, O: 0, draws: 0
Play again? (y/n):
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Player X, choose a cell (1-9):
X | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Player O, choose a cell (1-9):
X | 2 | 3
---+---+---
O | 5 | 6
---+---+---
7 | 8 | 9
Player X, choose a cell (1-9):
X | X | 3
---+---+---
O | 5 | 6
---+---+---
7 | 8 | 9
Player O, choose a cell (1-9):
X | X | 3
---+---+---
O | O | 6
---+---+---
7 | 8 | 9
Player X, choose a cell (1-9):
X | X | X
---+---+---
O | O | 6
---+---+---
7 | 8 | 9
Player X wins!
Score - X: 2, O: 0, draws: 0
Play again? (y/n):
Thanks for playing!
Input
5
1
9
5
3
2
7
4
6
8
y
1
4
2
5
3
n

Testing

  • Compare with tests/expected-output.txt: game 1 includes an occupied-cell attempt and ends with X completing the middle column; game 2 ends with X winning on the top row.
  • Play a full draw and confirm “It's a draw.” after 9 moves.

Common errors

Moves land in the wrong cell

Row = (cell − 1) / 3 and column = (cell − 1) % 3 — don't forget the −1.

A win isn't detected on a diagonal

Both diagonals, (0,0)(1,1)(2,2) and (0,2)(1,1)(2,0), must be checked.

Possible improvements

  • Add a single-player mode with a simple AI that blocks wins
  • Let players enter names
  • Highlight the winning line

Additional challenges

  1. 1Implement an unbeatable computer opponent using the minimax algorithm.
  2. 2Generalise to an N×N board with K in a row to win.
  3. 3Save scores to a file between runs.

Built and tested it yourself? Mark it to update your dashboard.