by abeera » January 21st, 2009, 1:11 pm
Asslam alaikum maine yahan aik code paste ker rehi ho yeah code maine banaya tou hai but is k graphics ki problem hai aur mujhe graphics k points n location set kerna nahi a rahay dats our project of dis semester so plzzzzzzzzzzzzzzzzz is pe nazar kijiyeh ga yeah code tic tac toe ka hai aur plzzzzzzzzzzzzzz plzzzzzzzzzzzz is ko improve ker dain bcoz mujhse graphics nahi theek ho rahay hain............................
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>
//*******decleration of functions******************
void IntArray(void);
void Player1(void);
void Player2(void);
void fillrectangle(int,int,int,int);
void box(void);
void movement(int,int);
void MoveTick(int x,int y);
void TwoPlayer(void);
void MoveCross(int x,int y);
void intro();
void Tick();
void Cross();
int Check();
void *p;
void *q;
int arr[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int x=0,y=0;
//**************main function*****************
void main(void)
{
int a;
int size;
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "g:/tc/bgi");
Tick();
Cross();
cleardevice();
G:
intro();
IntArray();
getch();
a=getche();
cleardevice();
if(a=='p' || a=='P')
TwoPlayer();
if(a=='e' || a=='E')
exit(0);
if(a!='e' && a!='E' && a!='p' && a!='P')goto G;
setcolor(4);
settextstyle(4,0,2);
outtextxy(210,445,"To Play Press Any Key");
a=getch();
cleardevice();
goto G;
}
void intro()
{
setcolor(8);
rectangle(0,0,639,470);
rectangle(0+2,0+2,639-2,470-2);
settextstyle(8,0,7);
setcolor(WHITE);
outtextxy(50,26,"TICTACTOE GAME");
setcolor(BLUE);
outtextxy(51+1,25,"TICTACTOE GAME");
settextstyle(2,0,22);
setcolor(GREEN);
outtextxy(186,120,"P:TWO PLAYERS");
outtextxy(187,121,"P:TWO PLAYERS");
setcolor(RED);
outtextxy(186,173,"E:EXIT");
outtextxy(187,174,"E:EXIT");
settextstyle(6,0,5);
setcolor(WHITE);
outtextxy( 40,243,"PRESENTED BY:");
setcolor(BLUE);
settextstyle(7,0,6);
outtextxy(99,310,"A");
setcolor(BLUE);
outtextxy(129,310,"B");
setcolor(BLUE);
outtextxy(162,310,"E");
setcolor(BLUE);
outtextxy(198,310,"E");
setcolor(BLUE);
outtextxy(233,310,"R");
setcolor(BLUE);
outtextxy(266,310,"A");
setcolor(YELLOW);
outtextxy(325,310,"T");
setcolor(YELLOW);
outtextxy(362,310,"A");
setcolor(YELLOW);
outtextxy(395,310,"R");
setcolor(YELLOW);
outtextxy(433,310,"I");
setcolor(YELLOW);
outtextxy(469,310,"Q");
setcolor(YELLOW);
settextstyle(7,0,6);
outtextxy(100,370,"A");
setcolor(YELLOW);
outtextxy(135,370,"Y");
setcolor(YELLOW);
outtextxy(160,370,"E");
setcolor(YELLOW);
outtextxy(190,370,"S");
setcolor(YELLOW);
outtextxy(220,370,"H");
setcolor(YELLOW);
outtextxy(250,370,"A");
setcolor(BLUE);
outtextxy(350,370,"S");
setcolor(BLUE);
outtextxy(380,370,"I");
setcolor(BLUE);
outtextxy(410,370,"D");
setcolor(BLUE);
outtextxy(440,370,"D");
setcolor(BLUE);
outtextxy(470,370,"I");
setcolor(BLUE);
outtextxy(500,370,"Q");
setcolor(BLUE);
outtextxy(530,370,"U");
setcolor(BLUE);
outtextxy(560,370,"I");
}
//***********function of main rectangles***********
void box(void)
{
//********main rectangles************//
setcolor(4);
rectangle(130,70,470,464);
rectangle(132,72,468,462);
//**************small rectangles************//
//**************first row rectangles***********//
setcolor(7);
rectangle(141,80,218,150);//1
rectangle(149,88,210,142);//1
rectangle(222,80,299,150);//2
rectangle(230,88,291,142);//2
rectangle(303,80,380,150);//3
rectangle(311,88,372,142);//3
rectangle(384,80,461,150); //4
rectangle(392,88,453,142); //4
//second row rectangles*************//
rectangle(141,154,218,227);//4
rectangle(149,162,210,219);//4
rectangle(222,154,299,227);//5
rectangle(230,162,291,219);//5
rectangle(303,154,380,227);//6
rectangle(311,162,372,219);//6
rectangle(384,154,461,227); //7
rectangle(392,162,453,219); //7
//third row rectangles*************//
rectangle(141,231,218,304);//7
rectangle(149,239,210,296);//7
rectangle(222,231,299,304);//8
rectangle(230,239,291,296);//8
rectangle(303,231,380,304);//9
rectangle(311,239,372,296);//9
rectangle(384,231,461,304); //
rectangle(392,239,453,296); //
//Fourth row rectangle**********//
setcolor(7);
rectangle(141,308,218,381);
rectangle(149,316,210,373);
rectangle(222,308,299,381);
rectangle(230,316,291,373);
rectangle(303,308,380,381);
rectangle(311,316,372,373);
rectangle(384,308,461,381);
rectangle(392,316,453,373);
//****************************** INternal Rectangles*********************
rectangle(140,312+77,461,385+58);
rectangle(144,391,457,441);
rectangle(148,393,453,439);
rectangle(152,395,449,437);
rectangle(156,397,445,435);
rectangle(160,399,441,433);
rectangle(164,401,437,431);
}
//**************function of coloring rectangles*************//
void fillrectangle(int x,int y,int col1,int col2)
{
setcolor(col1);
setfillstyle(1,col2);
floodfill(x,y,col1);
}
//**********************function of player 1***********
void Player1(void)
{
char a;
while(a!=13)
{
a=getch();
if(a==77) x++;
if(a==75) x--;
if(a==80) y++;
if(a==72) y--;
if(x<=-1)
x=3;
if(x>=4)
x=0;
if(y<=-1)
y=3;
if(y>=4)
y=0;
movement(x,y);
if(a==13&&arr[x][y]==0)
{
MoveTick(x,y);
arr[x][y]=1;
}
else a=1;
}
}
//////////////////////// Defination of Player 2 ///////////////////////////
void Player2(void)
{
char a;
while(a!=32)
{
a=getch();
if(a==68||a==100) x++;
if(a==65||a==97) x--;
if(a==87||a==119) y--;
if(a==88||a==120) y++;
if(x<0)
x=3;
if(x>3)
x=0;
if(y<0)
y=3;
if(y>3)
y=0;
movement(x,y);
if(a==32&&arr[x][y]==0)
{
MoveCross(x,y);
arr[x][y]=2;
break;
}
else
a=1;
}
}
//******************** two players
void TwoPlayer(void)
{
int ch;
box();
movement(0,0);
while(1)
{
Player1();
ch=Check();
if(ch==1)
{
setcolor(2);
settextstyle(4,0,3);
outtextxy(350,419,"PLAYER 1 WINS");
break;
}
if(ch==3)
{
setcolor(2);
settextstyle(4,0,4);
outtextxy(350,419,"GAME DRAW");
break;
}
Player2();
ch=Check();
if(ch==2)
{
setcolor(4);
settextstyle(4,0,3);
outtextxy(350,419,"PLAYER 2 WINS");
break;
}
if(ch==3)
{
setcolor(2);
settextstyle(4,0,4);
outtextxy(350,419,"GAME DRAW");
break;
}
}
}
//******************movements done*********//
void movement(int x,int y)
{
// first rectangle
if(x==0 && y==0)fillrectangle(143,83,6,1);
else
fillrectangle(143,83,6,0);
//second rectangle
if(x==1 && y==0)fillrectangle(225,83,6,1);
else
fillrectangle(225,83,6,0);
//third rectangle
if(x==2 && y==0)fillrectangle(305,83,6,1);
else
fillrectangle(305,83,6,0);
//fourth rectangle
if(x==3 && y==0)fillrectangle(386,83,6,1);
else
fillrectangle(386,83,6,0);
//fifth rectangle
if(x==0 && y==1)fillrectangle(143,156,6,1);
else
fillrectangle(143,156,6,0);
//six rectangle
if(x==1 && y==1)fillrectangle(224,156,6,1);
else
fillrectangle(224,156,6,0);
// seventh rectangle
if(x==2 && y==1)fillrectangle(305,156,6,1);
else
fillrectangle(305,156,6,0);
//eighth rectangle
if(x==3 && y==1)fillrectangle(386,156,6,1);
else
fillrectangle(386,156,6,0);
//ninth rectangle
if(x==0 && y==2)fillrectangle(143,233,6,1);
else
fillrectangle(143,233,6,0);
//10th rectangle
if(x==1 && y==2)fillrectangle(224,233,6,1);
else
fillrectangle(224,233,6,0);
//11th rectangle
if(x==2 && y==2)fillrectangle(305,233,6,1);
else
fillrectangle(305,233,6,0);
//12th rectangle
if(x==3 && y==2)fillrectangle(386,233,6,1);
else
fillrectangle(386,233,6,0);
//13 rectangle
if(x==0 && y==3)fillrectangle(143,310,6,1);
else
fillrectangle(143,310,6,0);
// 14th rectangle
if(x==1 && y==3)fillrectangle(224,310,6,1);
else
fillrectangle(224,310,6,0);
//15th rectangle
if(x==2 && y==3)fillrectangle(305,310,6,1);
else
fillrectangle(305,310,6,0);
//16 rectangle
if(x==3 && y==3)fillrectangle(386,310,6,1);
else
fillrectangle(386,310,6,0);
}
void Tick()
{
int size;
//**********tick;
setcolor(8);
line(202,129,214,145);
line(214,145,249,105);
line(202,129,212,133);
line(210,133,248,106);
setfillstyle(1,2);
floodfill(205,132,8);
size=imagesize(202-2,100,249+2,145+2);
p=malloc(size);
getimage(202-2,100,249+2,145+2,p);
}
void Cross()
{ int size;
//*********cross;
setcolor(8);
line(206,183,243,220);
line(202,189,238,225);
line(206,183,202,189);
line(243,220,238,225);
setfillstyle(1,4);
floodfill(206,184,8);
//************
line(233,183,205,220);
line(238,188,210,225);
line(233,183,238,188);
line(205,220,210,225);
setfillstyle(1,4);
floodfill(233,184,8);
setfillstyle(1,4);
floodfill(208,219,8);
size=imagesize(202-2,183-2,243+2,225+2);
q=malloc(size);
getimage(202-2,183-2,243+2,225+2,q);
}
void MoveTick(int x,int y)
{
if(x==0 &&y==0)putimage(200,100,p,1);
if(x==1 &&y==0)putimage(280,100,p,1);
if(x==2 &&y==0)putimage(360,100,p,1);
if(x==3 &&y==0)putimage(420,100,p,1);
if(x==0 &&y==1)putimage(200,180,p,1);
if(x==1 &&y==1)putimage(280,180,p,1);
if(x==2 &&y==1)putimage(360,180,p,1);
if(x==3 &&y==1)putimage(420,180,p,1);
if(x==0 &&y==2)putimage(200,255,p,1);
if(x==1 &&y==2)putimage(280,255,p,1);
if(x==2 &&y==2)putimage(360,255,p,1);
if(x==3 &&y==2)putimage(420,255,p,1);
if(x==0 &&y==3)putimage(200,320,p,1);
if(x==1 &&y==3)putimage(280,320,p,1);
if(x==2 &&y==3)putimage(360,320,p,1);
if(x==3 &&y==3)putimage(420,320,p,1);
}
void MoveCross(int x,int y)
{
if(x==0 &&y==0)putimage(200,100,q,1);
if(x==1 &&y==0)putimage(280,100,q,1);
if(x==2 &&y==0)putimage(360,100,q,1);
if(x==3 &&y==0)putimage(420,100,q,1);
if(x==0 &&y==1)putimage(200,180,q,1);
if(x==1 &&y==1)putimage(280,180,q,1);
if(x==2 &&y==1)putimage(360,180,q,1);
if(x==3 &&y==1)putimage(420,180,q,1);
if(x==0 &&y==2)putimage(200,255,q,1);
if(x==1 &&y==2)putimage(280,255,q,1);
if(x==2 &&y==2)putimage(360,255,q,1);
if(x==3 &&y==2)putimage(420,255,q,1);
if(x==0 &&y==3)putimage(200,320,q,1);
if(x==1 &&y==3)putimage(280,320,q,1);
if(x==2 &&y==3)putimage(360,320,q,1);
if(x==3 &&y==3)putimage(420,320,q,1);
}
//*************checks for players and computer***************
int Check(void)
{
{
int i,j;
int no=0;
//************<Def Check Player1>*****************
// First Horizantal row
if(arr[0][0]==1 && arr[1][0]==1 && arr[2][0]==1 && arr[3][0]==1)
return 1;
// Second Horizantal row
if(arr[0][1]==1 && arr[1][1]==1 && arr[2][1]==1 && arr[3][1]==1)
return 1;
// Third Horizantal row
if(arr[0][2]==1 && arr[1][2]==1 && arr[2][2]==1&& arr[3][2]==1)
return 1;
//Fourth horizontal row
if(arr[0][3]==1 && arr[1][3]==1 && arr[2][3]==1&& arr[3][3]==1)
return 1;
// First Vertical row
if(arr[0][0]==1 && arr[0][1]==1 && arr[0][2]==1 && arr[0][3]==1)
return 1;
// Second Vertical row
if(arr[1][0]==1 && arr[1][1]==1 && arr[1][2]==1 && arr[1][3]==1)
return 1;
// Third Vertical row
if(arr[2][0]==1 && arr[2][1]==1 && arr[2][2]==1 && arr[2][3]==1)
return 1;
if(arr[3][0]==1 && arr[3][1]==1 && arr[3][2]==1&& arr[3][3]==1)
return 1;
// Diagonal from left to right
if(arr[0][0]==1 && arr[1][1]==1 && arr[2][2]==1 && arr[3][3]==1)
return 1;
// Diagonal from right to left
if(arr[3][0]==1 && arr[2][1]==1 && arr[1][2]==1 && arr[0][3]==1)
return 1;
//************<Def Check Player2>*****************
if(arr[0][0]==2 && arr[1][0]==2 && arr[2][0]==2&& arr[3][0]==2)
return 2;
if(arr[0][1]==2 && arr[1][1]==2 && arr[2][1]==2&& arr[3][1]==2)
return 2;
if(arr[0][2]==2 && arr[1][2]==2 && arr[2][2]==2&& arr[3][2]==2)
return 2;
if(arr[0][3]==2 && arr[1][3]==2 && arr[2][3]==2&& arr[3][3]==2)
return 2;
if(arr[0][0]==2 && arr[0][1]==2 && arr[0][2]==2&& arr[0][3]==2)
return 2;
if(arr[1][0]==2 && arr[1][1]==2 && arr[1][2]==2&& arr[1][3]==2)
return 2;
if(arr[2][0]==2 && arr[2][1]==2 && arr[2][2]==2&& arr[2][3]==2)
return 2;
if(arr[3][0]==2 && arr[3][1]==2 && arr[3][2]==2&& arr[3][3]==2)
return 2;
if(arr[0][0]==2 && arr[1][1]==2 && arr[2][2]==2&& arr[3][3]==2)
return 2;
if(arr[3][0]==2 && arr[2][1]==2 && arr[1][2]==2&& arr[0][3]==2)
return 2;
// end player 2 //
if(arr[0][0]==4 && arr[1][0]==4 && arr[2][0]==4&& arr[3][0]==4)
return 4;
if(arr[0][1]==4 && arr[1][1]==4 && arr[2][1]==4&& arr[3][1]==4)
return 4;
if(arr[0][2]==4 && arr[1][2]==4 && arr[2][2]==4&& arr[3][2]==4)
return 4;
if(arr[0][3]==4 && arr[1][3]==4 && arr[2][3]==4& arr[3][3]==4)
return 4;
if(arr[0][0]==4 && arr[0][1]==4 && arr[0][2]==4&& arr[0][3]==4)
return 4;
if(arr[1][0]==4 && arr[1][1]==4 && arr[1][2]==4&& arr[1][3]==4)
return 4;
if(arr[2][0]==4 && arr[2][1]==4 && arr[2][2]==4&& arr[2][3]==4)
return 4;
if(arr[3][0]==4 && arr[3][1]==4 && arr[3][2]==4&& arr[3][3]==4)
return 4;
if(arr[0][0]==4 && arr[1][1]==4 && arr[2][2]==4&& arr[3][3]==4)
return 4;
if(arr[3][0]==4 && arr[2][1]==4 && arr[1][2]==4&& arr[0][3]==4)
return 4;
//***************<Continue>**********************
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
{
if(arr[j][i]!=0)no++;
}
if(no>=16)return 3;
else
return 0;
}
}
//**************************//
void IntArray(void)
{
int x;
int y;
for(y=0;y<=4;y++)
for(x=0;x<=4;x++)
arr[x][y]=0;
}