A classic two-player Tic-Tac-Toe game implemented in C++ with a console-based interface. This project demonstrates fundamental programming concepts including game logic, input validation, and win condition checking.
This is a command-line Tic-Tac-Toe game where two players take turns marking spaces on a 3×3 grid. The first player to align three of their marks horizontally, vertically, or diagonally wins the game. The implementation includes automatic move validation, board state management, and replay functionality.
- Two-Player Gameplay: Local multiplayer experience for two players (X and O)
- Input Validation: Robust validation ensures only valid moves (rows/columns 1-3) are accepted
- Overwrite Protection: Prevents players from overwriting occupied spaces
- Win Detection: Automatically detects winning conditions across all rows, columns, and diagonals
- Draw Detection: Identifies when the board is full with no winner
- Replay Option: Allows players to start a new game immediately after completion
- Clear Display: Automatic screen clearing after each move for a clean gaming experience
The game is structured using procedural programming with the following key components:
- Global Board State: 3×3 character array representing the game board
- Move Functions: Separate functions for X and O player moves (
Xmove(),Omove()) - Validation System:
ValidMove()ensures input is within valid range (1-3) - Board Update Logic:
updateboard()handles placing marks and preventing overwrites - Win Checker:
winning()evaluates all possible winning combinations - End Game Handler:
endgame()detects draws and offers replay options
display()- Renders the current board state to the consoleXmove()/Omove()- Handle player input for X and O respectivelyupdateboard(row, column, player)- Updates board with player's moveValidMove(move)- Recursively validates row/column inputwinning()- Checks all 8 possible winning conditionsendgame()- Detects full board and offers game restartgame()- Main game loop coordinating all functions
- C++ compiler (GCC, Clang, MSVC, or similar)
- Windows OS (uses
system("cls")for screen clearing)
g++ TicTacToe.cpp -o TicTacToe.exe./TicTacToe.exeOr on Windows:
TicTacToe.exe- Launch the game - Run the executable to start
- Player X goes first - Enter row and column numbers (1-3):
X's turn - Enter row number: 2 Enter column number: 2 - Player O's turn - Same process for the second player
- Continue alternating until someone wins or the board fills up
- Game ends when:
- A player gets three in a row (horizontal, vertical, or diagonal)
- The board is full (draw) - players can choose to replay or exit
Column: 1 2 3
Row 1: | |
Row 2: | |
Row 3: | |
- Board positions are represented as a 3×3 array initialized with 'A' (empty)
- Players input row and column numbers (1-3)
- The game validates inputs and checks for occupied spaces
- After each move, the board is cleared and redisplayed
- Win conditions check 3 rows + 3 columns + 2 diagonals = 8 total combinations
- Draw condition checks if board is full with no winner
The implementation uses procedural programming with global state:
char board[3][3]; // Global game board
int row, column, factor; // Global game state variablesThe factor variable controls game flow:
0= Game in progress1= Game won5= Game exited
As noted in the code comments, potential enhancements include:
- Refactoring to use object-oriented programming (classes for Board, Player, Game)
- Cross-platform compatibility (replace
system("cls")) - AI opponent option for single-player mode
- Graphical user interface
- Move history tracking
- Undo functionality
- Better separation of concerns (input/output/logic)
This is an educational project. Feel free to use and modify as needed.
Thanks for reading! Enjoy the game!