Saturday, July 28, 2012

Tic Tac Toe Game C++

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<graphics.h>


char mat[3][3];
void table(void); //function to print the table
void welcome(void);     //function for welcome screen
void info(void);


void main()
{
welcome();
info();
     A: clrscr();
int i,j,m,n,sum=0;
char ch;

for(m=0;m<3;++m)
for(n=0;n<3;++n)
mat[m][n]='\0';
table();

while(sum<10)
{
//for player 1
cout<<"Player 1 is'x'\nChoose the position:";
cout<<"\nRow:";
cin>>i;
cout<<"Coloumn:";
cin>>j;

//if position is wrong
for(;i>3||i<1||j>3||j<1||('x'==mat[i-1][j-1]||'o'==mat[i-1][j-1]);)
{
   cout<<"\nSorry!!!! wrong position,Choose the position again";
   cout<<"\nRow:";
   cin>>i;
   cout<<"Coloumn:";
   cin>>j;
}
mat[i-1][j-1]='x';
sum++;

//to check if player 1 wins or not
if(mat[0][0]=='x'&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
{
table();
cout<<"\nPlayer 1 wins.......!!!" ;
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[1][0]=='x'&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[2][0]=='x'&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][0]=='x'&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][1]=='x'&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][2]=='x'&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][0]=='x'&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][2]=='x'&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
{
table();
cout<<"\nPlayer 1 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(sum==9) //to check the chances
{
table();
cout<<"\n\tThe game is over.......no one wins...HaHaHa.....!!!";
break;
}
//for player 2
cout<<"\n\nPlayer 2 is'o'\nChoose the position:";
cout<<"\nRow:";
cin>>i;
cout<<"Coloumn:";
cin>>j;

//if position is wrong
for(;i>3||i<1||j>3||j<1||('x'==mat[i-1][j-1]||'o'==mat[i-1][j-1]);)
{
   cout<<"\nSorry!!!! wrong position,Choose the position again";
   cout<<"\nRow:";
   cin>>i;
   cout<<"Coloumn:";
   cin>>j;
}
mat[i-1][j-1]='o';
sum++;
table();

//to check player 2 wins or not
if(mat[0][0]=='o'&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[1][0]=='o'&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[2][0]=='o'&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][0]=='o'&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][1]=='o'&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][2]=='o'&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][0]=='o'&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}

if(mat[0][2]=='o'&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
{
cout<<"\nPlayer 2 wins.......!!!";
cout<<"\nYou have played Great.....!!!";
sleep(5);
break;
}
}
cout<<"\n\n\tWould you like to play more....?(y/n):";
cin>>ch;
if(ch=='y'||ch=='Y')
goto A;
else
{
cout<<"\n\n\tThanks for Playing.......:)";
sleep(5);
exit(0);
}
}

void table()
{
clrscr();
cout<<"\n\n\t\t  1  2  3\n";
cout<<"\t\t1  "<<mat[0][0]<<"|"<<mat[0][1]<<"|"<<mat[0][2];
cout<<"\n\t\t  --|-|--";
cout<<"\n\t\t2  "<<mat[1][0]<<"|"<<mat[1][1]<<"|"<<mat[1][2];
cout<<"\n\t\t  --|-|--";
cout<<"\n\t\t3  "<<mat[2][0]<<"|"<<mat[2][1]<<"|"<<mat[2][2]<<"\n\n";
}

void welcome()
{
int gdriver=DETECT,gmode,b;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setbkcolor(1);
settextstyle(3,0,6);
setcolor(YELLOW);
outtextxy(160,100,"Welcome To");
outtextxy(145,170,"Tic-Tac-Toe");
outtextxy(230,240,"Game");
settextstyle(3,0,1);
setcolor(WHITE);
outtextxy(360,400,"Press any key to continue.....!!");
getch();

}

void info()
{
int gdriver=DETECT,gmode,b;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

setbkcolor(2);
settextstyle(3,0,5);
setcolor(BLUE);
outtextxy(150,80,"Submitted To:");
outtextxy(140,130,"Mrs. Rakhi Jain");
setcolor(RED);
outtextxy(220,200,"Submitted By:");
outtextxy(220,250,"Neeraj Mishra");
settextstyle(2,0,6);
setcolor(WHITE);
outtextxy(350,400,"Press any key to continue.....!!");
getch();
closegraph();
}

Program to Add Two Matrices


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m1[6][6],m2[6][6],res[6][6],r1,c1,r2,c2,i,j;
cout<<"Enter the no. of rows and columns of first matrix\n";
cin>>r1>>c1;
cout<<"Enter the elements of first matrix\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter the no. of rows and columns of second matrix\n";
cin>>r2>>c2;
cout<<"Enter the elements of second matrix\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>m2[i][j];
}
}
if((r1==r2)&&(c1==c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
res[i][j]=m1[i][j]+m2[i][j];
}
}
clrscr();
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<m1[i][j]<<"\t";
}
}
clrscr();
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<m2[i][j]<<"\t";
}
}
clrscr();
cout<<"\nThe sum of two matrix is:\n";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<res[i][j]<<"\t";
}
}
}
getch();
}