Pages

Subscribe:

About Template

Random Posts

C++ Program of The Tic-tac-toe Game


Name:  Tictactoe
Description: This is a program of tictactoe game. A game played by two players who alternately mark squares in a grid with O's or X's. The winner will be the first to get three marks in a row, column or diagonal. In this program, the player should enter number 1 up to 9.

Picture below is the first screenshot wherein you will enter the names of the player. I entered juan for player1 and pedro for player2.

The second screenshot below is juan's turn, wherein, he needs to enter one number from the remaining number 4,6,7,8,and 9.

As we can see in the third screenshot below, juan wins! Because he is the one who first get three marks (X mark) in a diagonal form.

//Below are the Codes of the Program:
#include <iostream.h>
#include <conio.h>


int main()
{
char tik_tak[3][3]={'1','2','3','4','5','6','7','8','9'},XOX,player1[20],player2[20];   //declaration of array with defaults value
int value,get[100]={1000},identify=0,stoper=0;


cout<<"Enter Player 1 Name : ";
  cin>>player1;
cout<<"Enter Player 2 Name : ";
  cin>>player2;

for(int genloop=0;genloop<100;genloop++)                                                                     //general looping
{
clrscr();                                                                                                         //clear each loop


   cout<<"\n\n\n\n\n\n";
    for(int xloop=0;xloop<3;xloop++)                                                                       //output the defaults value first
    { cout<<"\t\t\t";
        for(int yloop=0;yloop<3;yloop++)
        {
            cout<<tik_tak[xloop][yloop]<<"\t";
        }
    cout<<"\n\n";
    }


     if(identify%2==0)            {      cout<<player1<<",it's your turn....";              }                // if s/2=0 the that turn is on a player while s/2!=0 then its on the others turn
else if(identify%2!=0)            {      cout<<player2<<",it's your turn....";           }




cout<<"\n\n\n\n\n\n";
cin>>value;                                                                                                   //enter value from 1-9






int counter=0;
                                                                                                                        //each loop qa resets its value to 0
for(int yaloop=0;yaloop<100;yaloop++)                                                                                      //checking if array d is being return
    if(value==get[yaloop])
        counter=1;




if(counter==0)                                                                                                     // if not returned then
    {
   identify++;                                                                                               //s is a counter of each loop
if(identify%2==0)        {    XOX='O'; }                                                                            // if s/2=0 the that turn is on a player while s/2!=0 then its on the others turn
else if(identify%2!=0){    XOX='X'; }
                                                                                                                        //replace the array value
      if(value==1) tik_tak[0][0]=XOX;
else if(value==2) tik_tak[0][1]=XOX;
else if(value==3) tik_tak[0][2]=XOX;
else if(value==4) tik_tak[1][0]=XOX;
else if(value==5) tik_tak[1][1]=XOX;
else if(value==6) tik_tak[1][2]=XOX;
else if(value==7) tik_tak[2][0]=XOX;
else if(value==8) tik_tak[2][1]=XOX;
else if(value==9) tik_tak[2][2]=XOX;
    }


                                           //stored the entered value
                                  //checking the array if one already won
         if(tik_tak[0][1]==tik_tak[0][0]&&tik_tak[0][2]==tik_tak[0][0])  {genloop=100; stoper=1;}      //b if a player has already w0n
 else if(tik_tak[1][0]==tik_tak[1][1]&&tik_tak[1][1]==tik_tak[1][2])  {genloop=100; stoper=1;}
 else if(tik_tak[2][0]==tik_tak[2][1]&&tik_tak[2][1]==tik_tak[2][2])  {genloop=100; stoper=1;}
 else if(tik_tak[0][0]==tik_tak[1][0]&&tik_tak[0][0]==tik_tak[2][0])  {genloop=100; stoper=1;}
 else if(tik_tak[0][1]==tik_tak[1][1]&&tik_tak[1][1]==tik_tak[2][1])  {genloop=100; stoper=1;}
 else if(tik_tak[0][2]==tik_tak[1][2]&&tik_tak[1][2]==tik_tak[2][2])  {genloop=100; stoper=1;}
 else if(tik_tak[0][0]==tik_tak[1][1]&&tik_tak[1][1]==tik_tak[2][2])  {genloop=100; stoper=1;}
 else if(tik_tak[0][2]==tik_tak[1][1]&&tik_tak[1][1]==tik_tak[2][0])  {genloop=100; stoper=1;}


 get[genloop]=value;


 for(int yamloop=0;yamloop<9;yamloop++)                                                                //checking if nobody wins for draw only
 if(get[yamloop]==1)
 for(int yaaloop=0;yaaloop<9;yaaloop++)
 if(get[yaaloop]==2)
 for(int yasloop=0;yasloop<9;yasloop++)
 if(get[yasloop]==3)
 for(int yadloop=0;yadloop<9;yadloop++)
 if(get[yadloop]==4)
 for(int yafloop=0;yafloop<9;yafloop++)
 if(get[yafloop]==5)
 for(int yagloop=0;yagloop<9;yagloop++)
 if(get[yagloop]==6)
 for(int yahloop=0;yahloop<9;yahloop++)
 if(get[yahloop]==7)
 for(int yajloop=0;yajloop<9;yajloop++)
 if(get[yajloop]==8)
 for(int yakloop=0;yakloop<9;yakloop++)
 if(get[yakloop]==9)
     {genloop=100; stoper=0;}
}






clrscr();                                                                                           //clear the screen


 cout<<"\n\n\n\n\n\n";                                                                              //output the defaults value first
    for(int xloop=0;xloop<3;xloop++)
    { cout<<"\t\t\t";
        for(int yloop=0;yloop<3;yloop++)
        {
            cout<<tik_tak[xloop][yloop]<<"\t";
        }
    cout<<"\n\n";
    }


if(stoper==1)
{if(identify%2!=0)        {      cout<<player1<<",you win!!!!!!";          }                    // if s/2=0 the that turn is on a player while s/2!=0 then its on the others turn
else if(identify%2==0){      cout<<player2<<",you win!!!!!!";       }
}                                                                                                  //b if a player has win
else if (stoper==0)
cout<<"Sorry ,,, it's draw!!\n Nobody wins!!!";
getch();
}