Tic Tac Toe project solved in codeblocks-source code with explanation


In this blog post, you will learn to do a programming project of C++ by making a tic tac toe in C++.

#include <cstdlib>
#include<iostream>
using namespace std;
int choice;
int row,column;
char turn='X';
bool draw=true;
char board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};


void display_board()
{
system("cls");
    cout<<"..............................................Tick Cross Game by Pakistani coder........................................"<<endl;
    cout<<"\t\t\t\t\tPlayer1[X]\n\t\t\t\t\tPlayer2[0]\n";
    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t__"<<board[0][0]<<"___ __"<<board[0][1]<<"___ __"<<board[0][2]<<"\n";
    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t__"<<board[1][0]<<"___ __"<<board[1][1]<<"___ __"<<board[1][2]<<"\n";

    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t     |     |     \n";
    cout<<"\t\t\t\t\t___"<<board[2][0]<<"__ __"<<board[2][1]<<"___ __"<<board[2][2]<<"\n";
    cout<<"\t\t\t\t\t     |     |     \n";
   /// cout<<"\t\t\t\t\t     |     |     \n";

}
void player_turn()
{
if(turn=='X')
        cout<<"\n\tPlayer1[X] turn:";
    if(turn=='0')
        cout<<"player 2[0] turn";
    cin>>choice;

/*
    switch(choice)
    {

    case 1:
        board[0][0]='X';
        break;

    case 2:
        board[0][1]='X';
        break;

    case 3:
        board[0][2]='X';
        break;

    case 4:
        board[1][0]='X';
        break;

           case 5:
        board[1][1]='X';
        break;

           case 6:
        board[1][2]='X';
        break;

           case 7:
        board[2][0]='X';
        break;

           case 8:
        board[2][1]='X';
        break;

           case 9:
        board[2][2]='X';
        break;
        default:

        cout<<"invalid choice";
        break;
        ///value will go there but need to display the board also

    }*/
//    display_board();
    //checkwin();
    //cout<<"player 2 turn\n";
  //  cin>>choice;
    switch(choice)
    {


    case 1:
row=0;column=0;
        break;

    case  2:
row=0;column=1;
        break;

            case 3:
row=0;column=2;
        break;

            case 4:
row=1;column=0;
        break;

            case 5:
row=1;column=1;
        break;


            case 6:
row=1;column=2;
        break;
            case 7:
                row=2;column=0;break;

            case 8:
                row=2;column=1;break;


            case 9:
row=2;column=2;
        break;

        default:

        cout<<"invalid choice";
        break;
    }
    ///if x turn, then board row col=X and next turn=0 and if turn==0 continue
    if(turn=='X'&&board[row][column]!='X'&&board[row][column]!='0')
    {

        board[row][column]='X';
        turn='0';
    }
    else if(turn=='0'&&board[row][column]!='X'&&board[row][column]!='0')

    {

        board[row][column]='0';
        turn='X';
    }
    else
    {

        cout<<"box already filled"<<endl;
        player_turn();
    }
    display_board();
}
bool gameover()
{
///check win
for(int i=0;i<3;i++)
{
    if(board[i][0]==board[i][1]&&board[i][0]==board[i][2]||board[0][i]==board[1][i]&&board[0][i]==board[2][i])
return false;
}
if(board[0][0]==board[1][1]&&board[0][0]==board[2][2]||board[0][2]==board[1][1]&&board[0][2]==board[2][0])
    return false;///win=false
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
{
    if(board[i][j]!='X'&& board[i][j]!='0')
            {
                return true;
            }
}
}
bool draw=true;
return false;

}

int main()
{
   /*for(int i=0;i<3;i++)
   {

       for(int j=0;j<3;j++)
       {

           cout<<board[i][j];
       }
   }*/
   while(gameover())
   {

    display_board();
player_turn();
gameover();


    ///display_board();
    }
    if(turn=='X'&&draw==true)
        cout<<"Player2[0] won the game"<<endl;

else    if(turn=='0'&&draw==true)
        cout<<"player1[X] won the game"<<endl;
else
 cout<<"game draw\n\n"<<endl;

}

Leave a Comment

Your email address will not be published. Required fields are marked *