Anonymous Anonymous - 4 days ago
52 0

No description

C++

A tic Tac based Game

#include<iostream>
#include<stdlib.h>
#include<graphics.h>
#include<string.h>
#include<stdio.h>
#include<time.h>
struct coordinates
{
    int x;
    int y;
};
struct coordinates1
{
    int x1;
    int x2;
    int y1;
    int y2;
};
int i=9;
struct coordinates circle1[9];
struct coordinates1 rectangle1[9];



using namespace std;
int a[3][3];
int b[10];
int c[9];
int d[9];
int z,x;
int top=0,top1=0,top2=0;
void player2();
void player1();
void loop1();
void loop2();
void gamewon1();
void gamewon2();
void intialize(int a)
{


circle1[0].x=a+20; circle1[0].y=120; rectangle1[0].x1=a+20; rectangle1[0].y1=100; rectangle1[0].x2=a+40; rectangle1[0].y2=140;
circle1[1].x=a+93; circle1[1].y=120;  rectangle1[1].x1=a+87; rectangle1[1].y1=100; rectangle1[1].x2=a+100; rectangle1[1].y2=140;
circle1[2].x=a+160; circle1[2].y=120;  rectangle1[2].x1=a+160; rectangle1[2].y1=100; rectangle1[2].x2=a+180; rectangle1[2].y2=140;
circle1[3].x=a+20; circle1[3].y=190;  rectangle1[3].x1=a+20; rectangle1[3].y1=170; rectangle1[3].x2=a+40; rectangle1[3].y2=210;
circle1[4].x=a+93; circle1[4].y=190;  rectangle1[4].x1=a+87; rectangle1[4].y1=170; rectangle1[4].x2=a+100; rectangle1[4].y2=210;
circle1[5].x=a+160; circle1[5].y=190;  rectangle1[5].x1=a+160; rectangle1[5].y1=170; rectangle1[5].x2=a+180; rectangle1[5].y2=210;
circle1[6].x=a+20; circle1[6].y=260;   rectangle1[6].x1=a+20; rectangle1[6].y1=240; rectangle1[6].x2=a+40; rectangle1[6].y2=280;
circle1[7].x=a+93; circle1[7].y=260;    rectangle1[7].x1=a+87; rectangle1[7].y1=240; rectangle1[7].x2=a+100; rectangle1[7].y2=280;
circle1[8].x=a+160; circle1[8].y=260;   rectangle1[8].x1=a+160; rectangle1[8].y1=240; rectangle1[8].x2=a+180; rectangle1[8].y2=280;
}
void draw1(int y)
{
    circle(circle1[y].x,circle1[y].y,20);
}
void draw2(int y)
{
    rectangle(rectangle1[y].x1,rectangle1[y].y1,rectangle1[y].x2,rectangle1[y].y2);

}
int main()
{

int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
int x;
int y;
int radius=80;
x=getmaxx()/3;
y=getmaxy();
intialize(x);
line(x+60,80,x+60,310);   //vertical 1
line(x+130,80,x+130,310);  //vertical 2
line(x+10,160,x+190,160);  //horizontal 1
line(x+10,220,x+190,220);   //horizontal 2
/*circle(x+20,120,20);
circle(x+93,120,20);
circle(x+160,120,20);
circle(x+20,190,20);
circle(x+20,260,20);
circle(x+93,190,20);
circle(x+160,260,20);
circle(x+160,190,20);
circle(x+93,260,20);*/
/*rectangle(x+20,120,x+40,140);
rectangle(x+87,120,x+107,140);
rectangle(x+160,120,x+180,140);
rectangle(x+20,190,x+40,210);
rectangle(x+87,190,x+107,210);
rectangle(x+160,190,x+180,210);
rectangle(x+20,260,x+40,280);
rectangle(x+87,260,x+107,280);
rectangle(x+160,260,x+180,280);*/


cout<<"                            Welcome To Tic Toe Game"<<endl;
cout<<"                              Yash Kumar Singhal"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"                             Rules And Regulations"<<endl;
cout<<"*Numbering of Matrix will be as Follows"<<endl;
cout<<"1  2  3 "<<endl<<"4  5  6"<<endl<<"7  8  9"<<endl;
cout<<"You can Choose which block To mark According to the numbered against the"<<endl<<"Corresponding Block"<<endl;
cout<<endl<<"*The matrix which Have not been still marked is shown as -1"<<endl;
cout<<"*Player 1 will take the turn First"<<endl;
cout<<"*The marking Of Player 1 Is shown as 1 while the same for Player 2"<<endl<<endl;
cout<<"*Following Program is Only the DOS Simulation of Org TicTacToe and thus"<<endl<<" Will follow all Original Rules of the game"<<endl<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;



for(z=0;z<=2;z++)
{
	for(x=0;x<=2;x++)
	{
		a[z][x]=-1;
	}
}
for(z=0;z<=5;z++)
{
	c[z]=-1;
    d[z]=-1;

}
for(z=0;z<=8;z++)
{
	b[z]=-1;
}
while(1)
{

player1();
if(top==9)
{
	cout<<"Match is Tied"<<endl;
	exit(1);

}
player2();
if(top==9)
{
	cout<<"Match is Tied"<<endl;
	exit(1);

}
}
}

void player1()
{
	int pusher,checker,roundabout,row,coloumn;
	A:
	roundabout=0;
	cout<<"enter the position to be marked (Player 1)"<<endl;
	cin>>pusher;
	for(checker=0;checker<=top;checker++)
	{

	  if(pusher==b[checker])
	  {
	  	cout<<"Invalid Move"<<endl;
	  	roundabout=1;

	  }

	}

	if(roundabout==1)
	{
		goto A;
	}
	else
	{
		pusher=pusher-1;
		if(pusher==0||pusher==1||pusher==2)
		{
			row=0;
			coloumn=pusher-(3*row);
		}
		if (pusher==3||pusher==4||pusher==5)
		{
			row=1;
			coloumn=pusher-(3*row);
		}
		if(pusher==6||pusher==7||pusher==8)
		{
			row=2;
			coloumn=pusher-(3*row);
		}


		a[row][coloumn]=1;
		b[top]=pusher+1;
		c[top1]=pusher+1;
		draw1(pusher);
		top++;
		top1++;

	}
	for(z=0;z<=2;z++)
	{
		for(x=0;x<=2;x++)
		{
			cout<<a[z][x]<<" ";
		}
		cout<<endl;
	}


	loop1();

}
void player2()
{
	int pusher,checker,roundabout,row,coloumn;
	A:
	   roundabout=0;
	cout<<"enter the position to be marked (Player 2)"<<endl;
	cin>>pusher;
	for(checker=0;checker<=top;checker++)
	{

	  if(pusher==b[checker])
	  {
	  	cout<<"Invalid Move"<<endl;
	  	roundabout=1;

	  }

	}

	if(roundabout==1)
	{
		goto A;
	}
	else
	{
		pusher=pusher-1;
		if(pusher==0||pusher==1||pusher==2)
		{
			row=0;
			coloumn=pusher-(3*row);
		}
		if (pusher==3||pusher==4||pusher==5)
		{
			row=1;
			coloumn=pusher-(3*row);
		}
		if(pusher==6||pusher==7||pusher==8)
		{
			row=2;
			coloumn=pusher-(3*row);
		}


		a[row][coloumn]=2;
		b[top]=pusher+1;
		d[top2]=pusher+1;
		draw2(pusher);
		top++;
		top2++;

	}
	for(z=0;z<=2;z++)
	{
		for(x=0;x<=2;x++)
		{
			cout<<a[z][x]<<" ";
		}
		cout<<endl;
	}


    loop2();
}

void gamewon1()
{
	cout<<"Congratulations Player1	has Won"<<endl;

        exit(1);




}
void gamewon2()
{
	cout<<"Congratulations Player 2 has Won"<<endl;


        exit(1);

}

void loop1()
{
	int i,m,n,j;
		m=0,n=0;


for(i=0;i<top1;i++)
	{
		if(c[i]==1)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==2)
				{
					m=1;
				}
				if(c[j]==3)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{

				gamewon1();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==1)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==4)
				{
					m=1;
				}
				if(c[j]==7)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==1)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==5)
				{
					m=1;
				}
				if(c[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==2)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==5)
				{
					m=1;
				}
				if(c[j]==8)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==3)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==5)
				{
					m=1;
				}
				if(c[j]==7)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==3)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==6)
				{
					m=1;
				}
				if(c[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
}

  m=0;n=0;


for(i=0;i<top1;i++)
	{
		if(c[i]==4)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==5)
				{
					m=1;
				}
				if(c[j]==6)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top1;i++)
	{
		if(c[i]==7)
		{
			for(j=0;j<top1;j++)
			{
				if(c[j]==8)
				{
					m=1;
				}
				if(c[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon1();
			}
		}
	}

}



void loop2()
{
	int i,m,n,j;
	m=0,n=0;



	for(i=0;i<top2;i++)
	{
		if(d[i]==1)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==2)
				{
					m=1;
				}
				if(d[j]==3)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{

				gamewon2();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==1)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==4)
				{
					m=1;
				}
				if(d[j]==7)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==1)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==5)
				{
					m=1;
				}
				if(d[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==2)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==5)
				{
					m=1;
				}
				if(d[j]==8)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==3)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==5)
				{
					m=1;
				}
				if(d[j]==7)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}

m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==3)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==6)
				{
					m=1;
				}
				if(d[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
}

  m=0;n=0;


for(i=0;i<top2;i++)
	{
		if(d[i]==4)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==5)
				{
					m=1;
				}
				if(d[j]==6)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}

	m=0,n=0;

for(i=0;i<top2;i++)
	{
		if(d[i]==7)
		{
			for(j=0;j<top2;j++)
			{
				if(d[j]==8)
				{
					m=1;
				}
				if(d[j]==9)
				{
					n=1;
				}
			}
			if(m==1&&n==1)
			{
				gamewon2();
			}
		}
	}
}
Comments