A simple command-line Tic Tac Toe game written in C++ with multiple game modes and computer AI strategies.
-
4 Game Modes:
- Human vs Human
- Human vs Nonsense Computer (random moves)
- Human vs Sensible Computer (basic strategy)
- Nonsense Computer vs Sensible Computer
-
Two AI Players:
- NonsenseComputerPlayer: chooses random empty cells.
- SensibleComputerPlayer: tries to win, block the opponent, or create future winning opportunities.
-
Board Display: Clean console-based 3x3 board.
-
Input Validation: Ensures valid mark and box choices.
-
Replay Option: Play again without restarting the program.
The game uses object-oriented design:
Player
is an abstract base class.HPlayer
(human),NonSenseComputerPlayer
, andSensibleComputerPlayer
are derived classes.Board
manages board state and drawing.Game
class coordinates the entire gameplay loop.
The SensibleComputerPlayer evaluates the board in this order:
- Win: If it can win in this move, it does.
- Block: If the opponent can win next, it blocks.
- Opportunity: If it can create a future win (1 in a line), it tries.
- Fallback: Otherwise, it picks a random empty cell.
- A C++ compiler (e.g.,
g++
,clang++
, or MSVC)
Open a terminal and compile with:
g++ TicTacToeTask.cpp -o TicTacToe
Run the game:
./TicTacToe # on Linux or Mac
TicTacToe.exe # on Windows
==== TIC TAC TOE ====
Select game mode:
1: Human vs Human
2: Human vs Nonsense Computer
3: Human vs Sensible Computer
4: Nonsense Computer vs Sensible Computer
Enter your choice (1-4): 3
Choose mark for Player 1 (O/X): X
Game Mode: Human vs Sensible Computer
___ ___ ___
| | | |
| 1 | 2 | 3 |
|___|___|___|
| | | |
| 4 | 5 | 6 |
|___|___|___|
| | | |
| 7 | 8 | 9 |
|___|___|___|
TicTacToeTask.cpp # Main source file containing all game classes
README.md # This documentation file
- Created as part of an OOP lab project using C++.
- Developed by Zohaib Khan.
This project is open-source and free to use. Attribution appreciated!