Coders Packet

Simple Tic Tac Toe Game In C++

By Deepak Nagar

We have created a simple Simple Tic Tac Toe game in C++. We are going to display the game on the terminal. We can use this code for the backend for the Tic Tac Toe game.

Description:- We have created a simple Tic Tac Toe game in C++. We use to include vectors and iostream library files. In this program, we define 3 functions. 

1)checking if any player wins the game or we have to continue or it is a draw for this we define the checking_win function.

Checking_win function use to return 3 values 1,0,and -1 . If it is a draw it returns 0. For a winner, it returns 1, and for continuing the game uses -1.

We use if-else if statements and for all possible winning conditions return 1.

We write condition for a draw and return 0;

And atlast in else block we return -1 if any of the conditions don't match.

2)We use the display_board function to display the Game board on the terminal. In this function, we simply use the cout function and .at() method in vector to display the content of the board.

3)To mark the choice made by a user we use the mark_board function. This function uses if-else if statements with the proper condition and assigns the value of mark variable X or O to the vector element used to represent an entry in the board.

Pseudo Code:-

Global Declarations: A character vector to display the entries in the board(square). Two integer type variables,1) choice variable to select the position which user want to select to mark the board.2)player variable to know the turn of the player.

main function:-Declare an integer variable to check the status of the game (game_status). And a character variable to store and mark the value of O and X at the proper position.

Initialize player =1 to give player one first chance. And now we use a do-while loop until the game_status is not equal to -1. In do block we use the display_board function and after that, we use conditional operator to know player turn. Then enter the choice which player wants to make and Depending on a player we use to mark X or O.Then we call the mark board function. And after that, we use to know the game_status by calling the checking_win function. At last, we use to print the result.

Function for checking win

int checking_win()
{
   if(square.at(1)==square.at(2)&&square.at(2)==square.at(3))
        {
            return(1);
        }
    else if(square.at(4)==square.at(5)&&square.at(5)==square.at(6))
        {
            return(1);
        }
    else if(square.at(7)==square.at(8)&&square.at(8)==square.at(9))
        {
            return(1);
        }
    else if(square.at(1)==square.at(4)&&square.at(4)==square.at(7))
        {
            return(1);
        }
    else if(square.at(2)==square.at(5)&&square.at(5)==square.at(8))
        {
            return(1);
        }
    else if(square.at(3)==square.at(6)&&square.at(6)==square.at(9))
        {
            return(1);
        }
    else if(square.at(1)==square.at(5)&&square.at(5)==square.at(9))
       {
         return(1);
       }
    else if(square.at(3)==square.at(5)&&square.at(5)==square.at(7))
      {
          return(1);
      }
    else if(square.at(1)!='1'&&square.at(2)!='2'&&square.at(3)!='3'&&square.at(4)!='4'&&square.at(5)!='5'&&square.at(6)!='6'&&square.at(7)!='7'
    &&square.at(8)!='8' &&square.at(9)!='9')
            {
                return(0);
            }
    else
        {
          return(-1);
        }
}

Function for displaying a board

void display_board()
{
    cout<<"\n\n TIC TAC TOE GAME\n\n";
    cout<<"PLAYER1(X)-PLAYER2 (O)\n\n\n";
    cout<<"    |   |    \n";
    cout<<square.at(1)<<"   |"<<square.at(2)<<"  |"<<square.at(3)<<"\n";
    cout<<"    |   |    \n";
    cout<<square.at(4)<<"   |"<<square.at(5)<<"  |"<<square.at(6)<<"\n";
    cout<<"    |   |    \n";
    cout<<square.at(7)<<"   |"<<square.at(8)<<"  |"<<square.at(9)<<"\n";
}
void mark_board(char mark)
{
      if(choice==1&&square.at(1)=='1')
        square.at(1)=mark;
    else if(choice==2&&square.at(2)=='2')
        square.at(2)=mark;
    else if(choice==3&&square.at(3)=='3')
        square.at(3)=mark;
    else if(choice==4&&square.at(4)=='4')
        square.at(4)=mark;
    else if(choice==5&&square.at(5)=='5')
        square.at(5)=mark;
    else if(choice==6&&square.at(6)=='6')
        square.at(6)=mark;
    else if(choice==7&&square.at(7)=='7')
        square.at(7)=mark;
    else if(choice==8&&square.at(8)=='8')
        square.at(8)=mark;
    else if(choice==9&&square.at(9)=='9')
        square.at(9)=mark;
    else
    {
        cout<<"enter a valid no"<<endl;
        player --;
    }

}

Output is provided in the image below.

Program_5

you can download the source code provided as a zip file.

Download project

Reviews Report

Submitted by Deepak Nagar (Deeip)

Download packets of source code on Coders Packet