A fully functional two-player Tic-Tac-Toe game built in C++ demonstrating the practical use of 2D arrays for game development.
- ✅ Two-Player Mode: Play against a friend locally
- ✅ Interactive CLI: Clean, user-friendly console interface
- ✅ 2D Array Board: Uses a 3×3 character array to represent game state
- ✅ Input Validation: Prevents invalid moves and handles errors gracefully
- ✅ Win Detection: Automatically checks rows, columns, and diagonals
- ✅ Draw Detection: Identifies when the board is full with no winner
- ✅ Game Statistics: Tracks wins and draws across multiple games
- ✅ Multiple Rounds: Play as many games as you want
- C++ compiler (g++ or clang++) with C++17 support
- Make (optional, for easy building)
Using Make:
make
./tictactoeOr compile manually:
g++ -std=c++17 -I include src/*.cpp -o tictactoe
./tictactoe- Launch the game
- Enter names for Player 1 (X) and Player 2 (O)
- Optionally load sample data or start fresh
- Enter row number (0-2) when prompted
- Enter column number (0-2) when prompted
- The board shows position indices on the top and left edges
0 1 2
┌───┬───┬───┐
0 │ │ │ │ ← Row 0
├───┼───┼───┤
1 │ │ │ │ ← Row 1
├───┼───┼───┤
2 │ │ │ │ ← Row 2
└───┴───┴───┘
↑ ↑ ↑
Col 0 1 2
Get three of your symbols (X or O) in a row:
- Horizontal: Complete any row
- Vertical: Complete any column
- Diagonal: Top-left to bottom-right OR top-right to bottom-left
0 1 2
┌───┬───┬───┐
0 │ X │ O │ X │
├───┼───┼───┤
1 │ O │ X │ │
├───┼───┼───┤
2 │ │ │ │
└───┴───┴───┘
X wins! (Diagonal)
.
├── README.md # This file
├── LICENSE # MIT License
├── Makefile # Build configuration
├── .gitignore # Git ignore rules
├── include/
│ └── TicTacToe.h # Game class declaration
└── src/
├── TicTacToe.cpp # Game logic implementation
└── main.cpp # Main game loop and UI
char board[3][3]; // 2D array representing the game board| Operation | Complexity | Description |
|---|---|---|
| Place Move | O(1) | Direct array access |
| Check Win | O(1) | Fixed 8 checks (3 rows + 3 cols + 2 diagonals) |
| Check Draw | O(1) | Simple counter check |
| Display Board | O(1) | Fixed 9 cell traversal |
| Validate Move | O(1) | Bounds + empty cell check |
The game checks for winning patterns in constant time:
- Three rows (horizontal wins)
- Three columns (vertical wins)
- Two diagonals (diagonal wins)
Total: 8 checks regardless of board state.
This project demonstrates:
- 2D Arrays: Understanding multi-dimensional arrays and memory layout
- Game Logic: Implementing turn-based game mechanics
- Pattern Matching: Detecting winning combinations efficiently
- Input Validation: Handling user input safely
- State Management: Tracking game state using arrays
- Object-Oriented Design: Clean class structure and encapsulation
- Change board size (4×4, 5×5)
- Use different symbols instead of X and O
- Add colors using ANSI escape codes
- Modify win condition (require 4 in a row)
- Implement AI opponent (minimax algorithm)
- Add undo/redo functionality
- Create save/load game feature
- Network multiplayer support
- GUI version using SDL or SFML
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
This project is licensed under the MIT License - see the LICENSE file for details.
- Built as an educational project to demonstrate 2D arrays in C++
- Inspired by the classic Tic-Tac-Toe game
- Great for learning data structures and game development
Questions or suggestions? Feel free to open an issue!
Enjoy the game! 🎮
make && ./tictactoe