Tic Tac Toe is a recreation of the traditional Tic-Tac-Toe game based on terminal. This program provides two modes that not only allows user to play with each other but also with built-in virtual user(computer). In other words, users/players can choose between PvP or PvC when executing the program. The requirements include:
- Develop a Tic Tac Toe game based on terminal with the game board UI and detailed instructions
- Implement 2 modes, PvP and PvC
- Allows users to enter their name, regardless of the game mode
- Random draw for picking player orders between PvP and PvC in each game
- Keep track of number of steps left
![Screenshot 2024-05-20 at 11 11 20 PM](https://private-user-images.githubusercontent.com/70761188/332109994-818d3532-257f-4196-81a3-6360374cf3ef.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0NTU4OTEsIm5iZiI6MTcyMTQ1NTU5MSwicGF0aCI6Ii83MDc2MTE4OC8zMzIxMDk5OTQtODE4ZDM1MzItMjU3Zi00MTk2LTgxYTMtNjM2MDM3NGNmM2VmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIwVDA2MDYzMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNmZDY3MzE2MjA3ZTcxNDkyNjcwZGVkNGQyMDVhNmZkM2ExNDhjZTRhYjk4ZmEwYzRhNzQ3YTQ0NmVlZjM1YjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.85IKMJgxDNOQvGJVu-ryxMiFGUAXAwOHW7mQmlhV1Gg)
![Screenshot 2024-05-20 at 11 11 36 PM](https://private-user-images.githubusercontent.com/70761188/332110055-644e6323-4f3a-47ff-9dcf-87c70d0cf2a3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0NTU4OTEsIm5iZiI6MTcyMTQ1NTU5MSwicGF0aCI6Ii83MDc2MTE4OC8zMzIxMTAwNTUtNjQ0ZTYzMjMtNGYzYS00N2ZmLTlkY2YtODdjNzBkMGNmMmEzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIwVDA2MDYzMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI1NTg2OWIwNjllZmI3YWViYzk0N2VkOWNkNWViMDU1NmU1MDhkYWE4NWIzOGY3ZWQyZDRkZTljYTY2NzFlYzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.EEkkKXr33I89Bc3X7-C2Vr2TpktbUUEyErWi8atzJVU)
This program is implemented solely in C++ and should be executable on all platforms and OS. Before execution, please make sure to have proper g++ compiler installed by opening the terminal and entering the command
g++ --version
once confirmed, navigate to the project folder and compile the program with the following command
g++ -o game tic-tac-toe.cpp
and start the game with, example output is provided in below
./game
![Screenshot 2024-05-20 at 11 16 39 PM](https://private-user-images.githubusercontent.com/70761188/332111478-9facc042-238f-494d-a063-1b3edb15045d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE0NTU4OTEsIm5iZiI6MTcyMTQ1NTU5MSwicGF0aCI6Ii83MDc2MTE4OC8zMzIxMTE0NzgtOWZhY2MwNDItMjM4Zi00OTRkLWEwNjMtMWIzZWRiMTUwNDVkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIwVDA2MDYzMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk1YmU5ZTU5NDY5OGY2OTI4YmZhMzE2ODg2NDc3Yjg0NzAyZjRiY2ZlYmJhY2ZhOGY3YWU3ZWFhODMyNTU5YTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.kT0B6cToGjbryJ-w2uVLCMZaTCjEUdCylKXCy91YbS8)
The program is developed written in C++. To provide a better user experience, the main workflow and be roughly divided into five stages. The first three stages are game set up and user mode selection.
// Required Execution Flow of the Ordering System
// (1) Welcome message
System_welcome_message = "Welcome to the Tic-Tac-Toe game.\nThis system allows you to play with another player or a built-in computer player.";
cout << System_welcome_message << endl;
cout << endl;
After welcome messages, the program simply explains the rules and ask for mode input
// (2) Explain and provide the system commands to the user
cout << "Type 0 to exit the system\nType 1 to begin a new game: ";
cin >> SystemCommand;
cout << endl;
while(SystemCommand != 0 && SystemCommand != 1){
cout << "Type 0 to exit the system\nType 1 to begin a new game: ";
cin >> SystemCommand;
}
if(SystemCommand == 0){
cout << "Goodbye!" << "\n";
return -1;
}
else{
cout << "---------------------------------------------------" << endl;
cout << "Simply enter the cell position and try to link one line to win the game." << endl;
// Draw the game board
cout << "Here is what the game board looks like with the IDs of each cell: " << endl;
cout << " 1 | 2 | 3 \n-----------\n 4 | 5 | 6 \n-----------\n 7 | 8 | 9 \n";
}
// (3) Ask the user to select the game mode.
cout << "Type 1 to play against computer player\nType 2 to play against another human player: ";
cin >> Game_Mode_Command;
cout << endl;
while(Game_Mode_Command != 1 && Game_Mode_Command != 2){
cout << "Type 1 or 2 to choose your game mode: ";
cin >> Game_Mode_Command;
}
// Processing phase
if(Game_Mode_Command == 1){
build_in_player(gameBoard, store_value, usedCell, dice);
}
else if(Game_Mode_Command == 2){
human_player(gameBoard, store_value, usedCell, dice);
}
The two game modes are separated into two functions, human_player
and build_in_player
(This isn't the best structure design as there are quite some duplicate codes that can be organized into a new function, and the program is not implemented in OOP. Yet, this is an old project built before I learned OOP, currently do not consider redesigning the program, any pull requests will be greatly appreciated). Both functions loop the game for 9 steps and checks for winning cases after every step calling the bool winCase(string gameBoard)
function.
while (currentSteps >= 1 && currentSteps < 9){
switch(value){
case 'O':
...
gameBoard = gameBoard.replace(corresponding_position, 1, "O");
currentSteps = currentSteps + 1;
cout << gameBoard;
cout << "This is step " << currentSteps ;
cout << ", there are " << 9-currentSteps << " more steps." << "\n";
if(winCase(gameBoard)){
currentSteps = 9;
cout << "Computer wins." << endl;
}
if (currentSteps == 9){
cout << "The end." << "\n";
break;
}
value = 'X';
case 'X':
...
gameBoard = gameBoard.replace(corresponding_position, 1, "X");
currentSteps = currentSteps + 1;
cout << gameBoard;
cout << "This is step " << currentSteps ;
cout << ", there are " << 9-currentSteps << " more steps." << "\n";
if(winCase(gameBoard)){
currentSteps = 9;
cout << "Player wins." << endl;
}
if (currentSteps == 9){
cout << "The end." << "\n";
break;
}
value = 'O';
}
}
Welcome to the Tic-Tac-Toe game.
This system allows you to play with another player or a built-in computer player.
Type 0 to exit the system
Type 1 to begin a new game: 1
---------------------------------------------------
Simply enter the cell position and try to link one line to win the game.
Here is what the game board looks like with the IDs of each cell:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
Type 1 to play against computer player
Type 2 to play against another human player: 1
Now let's start. The computer will determine your order by rolling a dice.
Please enter your name WITH NO SPACE(< 40 characters)
player: Tommy
Determining the player order...
Please wait for 3 seconds...
Tommy go first.
You are the 'X' side:
Which cell do you want to place:
1
---------------------------------------------------
X | |
-----------
| |
-----------
| |
This is step 1, there are 8 more steps.
Let me think...
X | |
-----------
O | |
-----------
| |
This is step 2, there are 7 more steps.
Please enter X:
X
Which cell do you want to place:
8
X | |
-----------
O | |
-----------
| X |
This is step 3, there are 6 more steps.
Let me think...
X | |
-----------
O | | O
-----------
| X |
This is step 4, there are 5 more steps.
Please enter X:
X
Which cell do you want to place:
5
X | |
-----------
O | X | O
-----------
| X |
This is step 5, there are 4 more steps.
Let me think...
X | |
-----------
O | X | O
-----------
| X | O
This is step 6, there are 3 more steps.
Please enter X:
X
Which cell do you want to place:
2
X | X |
-----------
O | X | O
-----------
| X | O
This is step 7, there are 2 more steps.
Player wins.
The end.
---------------------------------------------------
Welcome to the Tic-Tac-Toe game.
This system allows you to play with another player or a built-in computer player.
Type 0 to exit the system
Type 1 to begin a new game: 0
Goodbye!
- The application is built long ago and before I knew OOP, thus, despite the current game works fine, the structure and program architecture is poorly designed. This could affect the maintenance and the difficulties when adding new features
- The UI is rather simple, for better user experience, it is also recommended to redesign the UI and flush the screen after every step