TIC TAK TOE game's Programming in C++

Posted on at


Today i want to tell you about a game which i have programed in C++.In which I have also used Visual Studio compailor..

Game name is tic tak toe which is most favoriate game in childrens. I have also played many time this game.....here is the basic program behind this game...so, here we go.... 

#include "iostream"

#include "conio.h"

#include "stdio.h"

using namespace std;

void main()

{

                int turn[9],Row,Col;

                const int row=3,col=3;

                char player1[20]={},choice;

                char player2[20]={};

                char arr[row][col]={'v','v','v','v','v','v','v','v','v'};

                do{

                                system("cls");

                cout<<"\t LETS PLAY TIC TAC TOE..\7\7\7\n\n\n";

                cout<<"player 1 enter your name :";

                gets(player1);

                cout<<"\n\nplayer 2 enter your name :";

                gets(player2);

                cout<<endl<<endl;

                cout<<player1<<"'s symbol is 'O'\n\n";

                cout<<player2<<"'s symbol is 'X'\n";

                cout<<endl<<endl;

                cout<<"press enter to get started...";

                getch();

                system("cls");

                for(int i=0;i<3;i++)

                                {

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

                                                {

                                                                cout<<arr[i][j];

                                                }

                                                cout<<endl;

                                }

                for(int k=1;k<=9;k++)

                {                             

turn :  cout<<endl;         

                                if(k%2!=0)

                                {

                                cout<<player1<<" will enter row and column :";

                                cin>>turn[k];

                                if(turn[k]<=33)

                                {

                                                Row=turn[k]/10-1;

                                                Col=turn[k]%10-1;

                                }

                                else

                                {

                                                cout<<"wrong input...";

                                                getch();

                                                goto turn;

                                }

                               

                                                arr[Row][Col]= 'O';

                               

 

                                for(int i=0;i<3;i++)

                                {

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

                                                {

                                                                cout<<arr[i][j];

                                                }

                                                cout<<endl;

                                }

                                if (arr[0][0]=='O' && arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[2][0]=='O' && arr[2][0]==arr[1][1] && arr[1][1]==arr[0][2]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[0][0]=='O' && arr[0][0]==arr[1][0] && arr[1][0]==arr[2][0]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[0][1]=='O' && arr[0][1]==arr[1][1] && arr[1][1]==arr[2][1]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[0][2]=='O' && arr[0][2]==arr[1][2] && arr[1][2]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[0][0]=='O' && arr[0][0]==arr[0][1] && arr[0][1]==arr[0][2]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[1][0]=='O' && arr[1][0]==arr[1][1] && arr[1][1]==arr[1][2]) {cout<<player1<<" wins"<<endl;break;}

                                if (arr[2][0]=='O' && arr[2][0]==arr[2][1] && arr[2][1]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}

 

                                if (k== 9){cout<<"The game is over and no one wins, hahaha, you both stink!!!"<<endl; break;} //sum=9 because there are only 9 boxes in the game

               

 

                                }

                //big     

                                else

                                {

turn2 :      cout<<endl;   

                                                cout<<player2<<" will enter row and column :";

                                                cin>>turn[k];

                                                if(turn[k]<=33)

                                                {

                                                                Row=turn[k]/10-1;

                                                                Col=turn[k]%10-1;

                                                }

                                                else

                                                {

                                                                cout<<"wrong input...";

                                                                getch();

                                                                goto turn2;

                                                }

                                               

                                               

                                                                arr[Row][Col]= 'X';

                                               

                                               

 

                                                for(int i=0;i<3;i++)

                                                {

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

                                                                {

                                                                                cout<<arr[i][j];

                                                                }

                                                                cout<<endl;

                                                }

                                }

                                                if (arr[0][0]=='X' && arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[2][0]=='X' && arr[2][0]==arr[1][1] && arr[1][1]==arr[0][2]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[0][0]=='X' && arr[0][0]==arr[1][0] && arr[1][0]==arr[2][0]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[0][1]=='X' && arr[0][1]==arr[1][1] && arr[1][1]==arr[2][1]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[0][2]=='X' && arr[0][2]==arr[1][2] && arr[1][2]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[0][0]=='X' && arr[0][0]==arr[0][1] && arr[0][1]==arr[0][2]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[1][0]=='X' && arr[1][0]==arr[1][1] && arr[1][1]==arr[1][2]) {cout<<player2<<" wins"<<endl;break;}

                                                if (arr[2][0]=='X' && arr[2][0]==arr[2][1] && arr[2][1]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}

 

                                                if (k== 9){cout<<"The game is over and no one wins..."<<endl; break;} //sum=9 because there are only 9 boxes in the game

               

               

                               

                }

 

                               

 

 

               

ask:        cout<<"do you want to play again...??(Y/N)\n\n";

                                cin>>choice;

                }while(choice=='y' || choice =='Y');

               

                getch();

 

}

Friends do share and buzz.Thanks!!

You can also follow me on twitter: https://twitter.com/waleedannex64

If you want to read mine previous blogs then just go through here: http://www.bitlanders.com/waleed-altaf

 


TAGS:


About the author

waleed-altaf

hi..who i am....i dn't know :P

Subscribe 0
160