Tic-Tac-Toe
by Amit Rawat · Published · Updated
Tic-Tac-Toe is a game which played between two players. The chances are given alternately to fill the board with your chosen symbol and if your symbol is matched horizontally or vertically or diagonally you win and if neither of the players is able to do so then the match is a tie. So, Let's see the code of how it is done!
Explaining the code:-
Firstly the global variables used in the program are:-
char xo[3][3]; //used to store the input of the player's symbol char playerA; //used to store the symbol of player 1 char playerB; //used to store the symbol of player 2
I have created some of the functions which are used to do a particular part in the program.
So let's start with the displayBoard() function. Which is used to print the board every time it is called in the code.
void displayBoard(){ cout << endl; cout<< "\t\t\t\t "<<" C "<< " O "<<" L "<< endl; cout<< "\t\t\t\t "<<"[0]"<< " [1]"<<" [2]"<< endl; cout<< "\t\t\t\t R [0] "<<xo[0][0] << " | " << xo[0][1] << " | " << xo[0][2] << endl; cout<< "\t\t\t\t ---"<< (char)193 << "---" << (char)193 << "---" << endl; cout<< "\t\t\t\t O [1] "<<xo[1][0] << " | " << xo[1][1] << " | " << xo[1][2] << endl; cout<< "\t\t\t\t ---"<< (char)193 << "---" << (char)193 << "---" << endl; cout<< "\t\t\t\t W [2] "<<xo[2][0] << " | " << xo[2][1] << " | " << xo[2][2] << endl; }
Next function used is check() function which is used to check every time whether one of the players have any matched rows or columns or diagonals. If it is then it returns true boolean value otherwise it returns false.
bool check(){ for(int i=0;i<3;i++){ if(xo[i][0] != ' ' && xo[i][0] == xo[i][1] && xo[i][1] == xo[i][2]) return true; } for(int i=0;i<3;i++){ if(xo[0][i] != ' ' && xo[0][i] == xo[1][i] && xo[1][i] == xo[2][i]) return true; } if(xo[0][0] != ' ' && xo[0][0] == xo[1][1] && xo[1][1] == xo[2][2]) return true; if(xo[0][2] != ' ' && xo[0][2] == xo[1][1] && xo[1][1] == xo[2][0]) return true; return false; }
Now we have a function which takes the input from the player about the symbol they want to choose and assign the respective symbols to the players' variables.
void choose(char& playerA, char& playerB){ char temp; A: cin.clear(); cin >> temp; if(temp != 'X' && temp != 'O' && temp != 'x' && temp != 'o'){ cout << "You have entered the wrong symbol!\nPlease choose from these two symbols ( X or O )" << endl; goto A; }else{ if(temp >= 97 ){ temp = temp -32; } playerA = temp; if(temp == 'X'){ playerB='O'; }else playerB='X'; } }
There is a function which assigns the initial values to all the boxes of the tic-tac-toe board. This function is handy at the start of the game and when the game is restarted.
void clearBoard(){ for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ xo[i][j] = ' '; } } }
Next function is void setValue(int c, int row, int col); which is used to set the value to the particular box of the tic-tac-toe board.
void setValue(int c,int row, int col){ if(c%2 == 1){ xo[row][col]=playerA; }else{ xo[row][col]=playerB; } }
There is a function which delays the execution of the commands and used to give the animated feel.
void delay(int milliseconds){ clock_t now= clock(); while(clock() < (now + milliseconds)); } There is a function which displays the name of the project in big letters. Its name is displayTicTacToe(). void displayTicTacToe(int milliseconds){ cout << "_______________________________________________________________________________"<< endl;delay(milliseconds); cout << "_______________________________________________________________________________"<< endl << endl;delay(milliseconds); cout << "-------- -------- ------- -------- ------- ------- -------- ------- -------"<< endl;delay(milliseconds); cout << "-------- -------- ||----- -------- |-----| ||----- -------- ||---|| || "<< endl;delay(milliseconds); cout << " || || || || || || || || || || ----- "<< endl;delay(milliseconds); cout << " || || || || ||---|| || || || || ----- "<< endl;delay(milliseconds); cout << " || -------- ||----- || ||---|| ||----- || ||---|| || "<< endl;delay(milliseconds); cout << " || -------- ------- || || || ------- || ------- -------"<< endl<< endl;delay(milliseconds); cout << "_______________________________________________________________________________"<< endl;delay(milliseconds); cout << "_______________________________________________________________________________"<< endl;delay(milliseconds); }
And lastly, we have the main function where we have used all the above functions to make the tic-tac-toe game.
int main(){ char more; int c; int row,col; bool winner=false; displayTicTacToe(300); cout << "\t\t\tWelcome to TicTacToe\n" ; cout << "There are only two symbols ( X and O ) from which you can choose." << endl; cout << "Choose your symbol." << endl; cout << "Player 1: "; choose(playerA,playerB); cout<< "Let's start the game!"<< endl; do{ system("cls"); clearBoard(); displayBoard(); cout << "We will start with player 1.\n"; delay(2000); c=9; winner = false; while(c > 0){ system("cls"); displayTicTacToe(0); displayBoard(); if(c%2 == 1){ cout<< endl; cout<< "Player 1: Enter the position at which you want to put the symbol!"<< endl; }else{ cout<< endl; cout<< "Player 2: Enter the position at which you want to put the symbol!" << endl; } cout<< "\t Enter the row: "; cin >> row; cout << "\t Enter the column: "; cin >> col; setValue(c,row,col); if(check()){ if(c%2 == 1){ system("cls"); displayTicTacToe(150); displayBoard(); cout<< "\nPlayer 1 win!\n"; winner=true; break; }else{ system("cls"); displayTicTacToe(150); displayBoard(); cout<< "\nPlayer 2 win!\n"; winner=true; break; } } c--; } if(c==0){ cout<< "The match was a tie!" << endl; cout<< "Do you wanna play again(Y/N): "; cin >> more; } if(winner){ cout<< "Do you wanna play again(Y/N): "; cin >> more; } }while(more == 'Y' || more =='y'); system("exit"); return 0; }
That was all about the tic-tac-toe game. If you have any problem in understanding any part of the code, you can comment below your problem.
You can learn about many other C++ Programs Here.
Amit Rawat
Latest posts by Amit Rawat (see all)
- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019