Go4Expert (http://www.go4expert.com/)
-   Game Programming (http://www.go4expert.com/articles/game-programming/)
-   -   Chess using BGI Graphics in C/C++ (http://www.go4expert.com/articles/chess-using-bgi-graphics-c-cpp-t598/)

 rai_gandalf 18Feb2006 20:08

Chess using BGI Graphics in C/C++

1 Attachment(s)
Hello to one & all,

Myself & a friend of mine are involved in trying to make Chess using Graphics in Turbo C/C++. I am responsible for making the Game engine & he is responsible for the Graphics components. So far, we have (actually he has), completed the Chess Board creation using BGI. He has done a neat job of it. I just thought that you people might run it & give ur feedback so that we can go fwd & improve it.

Here's the entire code :
Code: C

`/* CHESS USING BGI GRAPHICS IN C/C++ */#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>void rook(int i,int j,int k);void knight(int i,int j,int k);void bishop(int i,int j,int k);void queen(int i,int j,int k);void king(int i,int j,int k);void pawn(int i,int j,int k);void main(){ int a=DETECT,b,c; int x,y,i,j,k=0,poly[8],l,m=0; clrscr(); initgraph(&a,&b,"\\tc\\bgi"); x=getmaxx()/8; y=getmaxy()/8; //setcolor(15); for(i=1,k+=y;m<8;i+=y,m++) for(j=1,l=0;l<8;j+=x,l++) {  if((l%2==1 && m%2==0) || (l%2==0 && m%2==1))  {   setcolor(0);                                /*IN BLACK CHEKS*/   setfillstyle(1,0);   poly[0]=j;   poly[1]=i;   poly[2]=j+x;   poly[3]=i;   poly[4]=j+x;   poly[5]=k+i;   poly[6]=j;   poly[7]=k+i;   fillpoly(4,poly);   /* ROOK IN BLACK CHEKS */   if((l==7 && m==0) || (l==0 && m==7))   {    setcolor(WHITE);    if(m==0 && l==7)    {     setfillstyle(SOLID_FILL,WHITE);    }    rook(i,j,k);   }   /*KNIGHT IN BLACK CHEKS*/  if((l==1 && m==0) || (l==6 && m==7))   {    setcolor(15);    if(m==0 && l==1)    {     setfillstyle(1,15);    }   knight(i,j,k);   }   /*BISHOP IN BLACK CHECK*/  if((l==5 && m==0) || (l==2 && m==7))   {    setcolor(15);    if(m==0 && l==5)    {     setfillstyle(1,15);    }   bishop(i,j,k);   }   /*QUEEN IN BLACK CHEK*/  if(m==7 && l==4)   {    setcolor(15);    queen(i,j,k);   }  /*KING IN THE BLACK CHEK*/  if(m==0 && l==3)   {    setcolor(15);    setfillstyle(1,15);    king(i,j,k);   }   /*PAWN IN BLACK CHEKS*/  if(m==6 && l%2==1  ||  m==1 && l%2==0)   {    setcolor(15);    if(m==1 && l%2==0)     setfillstyle(1,15);    pawn(i,j,k);   }  }  else  {   setcolor(15);                                 /*IN WHITE CHEKS*/   setfillstyle(1,15);   poly[0]=j;   poly[1]=i;   poly[2]=j+x;   poly[3]=i;   poly[4]=j+x;   poly[5]=k+i;   poly[6]=j;   poly[7]=k+i;   fillpoly(4,poly);   /*ROOK IN WHITE CHEKS*/   if((l==0 && m==0) || (l==7 && m==7))   {    setcolor(0);    if(m==7 && l==7)    {     setfillstyle(1,0);    }   rook(i,j,k);   }   /*KNIGHT IN WHITE CHEKS*/   if((l==6 && m==0) || (l==1 && m==7))   {    setcolor(0);    if(m==7 && l==1)    {     setfillstyle(1,0);    }   knight(i,j,k);   }   /*BISHOP IN WHITE CHEKS*/   if((l==2 && m==0) || (l==5 && m==7))   {    setcolor(0);    if(m==7 && l==5)    {     setfillstyle(1,0);    }   bishop(i,j,k);   }   /*QUEEN IN WHITE CHEK*/   if(m==0 && l==4)   {    setcolor(0);    queen(i,j,k);   }   /*KING IN THE WHITE CHEK*/   if(m==7 && l==3)   {    setcolor(0);    setfillstyle(1,0);    king(i,j,k);   }    /*PAWN IN WHITE CHEKS*/   if(m==6 && l%2==0  ||  m==1 && l%2==1)   {    setcolor(BLACK);    if(m==6 && l%2==0)     setfillstyle(SOLID_FILL,BLACK);    pawn(i,j,k);   }  } } setcolor(15); rectangle(0,0,(8*x)+1,(8*y)+1);  getch();  closegraph();}void rook(int i,int j,int k){ ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,17,17); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),17,17); setcolor(120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,60,14,14); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),90,150,14,14); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),180,240,14,14); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),270,330,14,14);}void knight(int i,int j,int k){ ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,17,17); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),17,17); setcolor(120); setfillstyle(1,120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,10,10); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),10,10);}void bishop(int i,int j,int k){ ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,17,17); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),17,17); setcolor(120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,13,13); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),13,13); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),90,180,10,10); setfillstyle(1,120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,3,3); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),3,3);}void queen(int i,int j,int k){ ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,17,17); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),20,20); setcolor(120); setfillstyle(1,120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,17,17); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,14,14); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,10,10); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,6,6); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,3,3); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),3,3);}void king(int i,int j,int k){ ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,20,20); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),20,20); setcolor(120); setfillstyle(1,120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,3,3); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),3,3); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,10,10); line((j+getmaxx()/16),(k+i)-(getmaxy()/16),(j+getmaxx()/16),((k+i)-(getmaxy()/16))+20); line((j+getmaxx()/16),(k+i)-(getmaxy()/16),(j+getmaxx()/16),((k+i)-(getmaxy()/16))-20); line((j+getmaxx()/16),(k+i)-(getmaxy()/16),(j+getmaxx()/16)-20,((k+i)-(getmaxy()/16))); line((j+getmaxx()/16),(k+i)-(getmaxy()/16),(j+getmaxx()/16)+20,((k+i)-(getmaxy()/16)));}void pawn(int i,int j,int k){ fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),10,10); setcolor(120); setfillstyle(1,120); ellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),0,360,3,3); fillellipse((j+getmaxx()/16),(k+i)-(getmaxy()/16),3,3);}`

Now, in the main( ), look at the following code-segment :
Code: C

`for(i=1,k+=y;m<8;i+=y,m++) for(j=1,l=0;l<8;j+=x,l++)`

The outer for-i-loop tracks horizontally (i.e. Column tracking), while inner for-j-loop tracks vertically (i.e. Row tracking).

Now, inside the for loops :
Code: C

`if((l%2==1 && m%2==0) || (l%2==0 && m%2==1))`

' l ' => takes values from ' 0 ' to ' 7 ', which stand for rows 1 through 8
' m ' => takes values from ' 0 ' to ' 7 ' which stand for columns 1 through 8

So, the ' if ' condition translates to :
Code: C

`if((Row==2,4,6 or 8   &&   Col==1,3,5 or 7)  || (Row==1,3,5 or 7   &&   Col==2,4,6 or 8))`

Assuming that the Chess Board has its Upper Left Corner Block as WHITE Check, then the above code segment basically takes care of printing all the BLACK Checks of the Board.
Obviously the 'else' part will do the job of printing all the WHITE Checks of the board.

Now, looking at the code segment :
Code: C

`setcolor(0);                             setfillstyle(1,0);poly[0]=j;poly[1]=i;poly[2]=j+x;poly[3]=i;poly[4]=j+x;poly[5]=k+i;poly[6]=j;poly[7]=k+i;fillpoly(4,poly);`

Now, what the above code-segment does is that it draws a rectangle defined by the 4 sets of (xco,yco) that is contained in the array 'poly'.
Looking at the Master for loops that encapsulates the above code, we can see how ' i ', ' j ', ' k ' have all been initialized/incremened.
This pattern of initialization/incrementation is basically done, so as to obtain the 4 corners of the Check that we are about to print.
For more info, please consult form Turbo C++ Help, functions : getmaxx( ) AND getmaxy ( )

Now, the positioning of the various members like Pawns, Rooks, Horses, etc. are achieved by the fact that at the beginning, their positioning is fixed & is easily predicted by looking at ' m ' & ' l ' values.

I will elaborate on the Rook positioning :
Code: C

`/* ROOK IN BLACK CHEKS */if((l==7 && m==0) || (l==0 && m==7)){    setcolor(WHITE);    if(m==0 && l==7)    {        setfillstyle(SOLID_FILL,WHITE);    }    rook(i,j,k);}`

Assuming that the Chess Board has its Upper Left Corner Block as WHITE Check,
(preferrably, please execute the program & u will find), then Rook in Black Checks occurs at two positions:
( i ) Row 1, Col 8 ==> corresponds to l==7,m==0
( ii ) Row 8, Col 1 ==> corresponds to l==0,m==7

That explains the 'if' condition. Now, in both cases, since the Background is BLACK in color, the outline of the Rooks must be WHITE. Also, in the ( i ) case, its a WHITE ROOK that must appear in a BLACK Check.

Other members are positioned in a similar manner.

As far as functions used like ellipse( ), getmaxx( ), getmaxy( ), I suggest u can refer the Turbo C/C++ Help.

Ciao,
Rajiv :)

 shabbir 18Feb2006 21:27

Re: Chess using BGI Graphics in C/C++

I voted as impressive. Its really a good interface for the start. Also I would advice to give a screenshot so that if someone does not have BGI Graphics they can also see the interface.

 coderzone 18Feb2006 21:41

Re: Chess using BGI Graphics in C/C++

Quote:
 Originally Posted by shabbir I voted as impressive. Its really a good interface for the start. Also I would advice to give a screenshot so that if someone does not have BGI Graphics they can also see the interface.
I dont have the TC compiler but the code rally looked impressive.

 rai_gandalf 19Feb2006 01:01

Re: Chess using BGI Graphics in C/C++

Quote:
 Originally Posted by shabbir I voted as impressive. Its really a good interface for the start. Also I would advice to give a screenshot so that if someone does not have BGI Graphics they can also see the interface.

THX A LOT for the appreciation. Also, could u tell me how to giv a screenshot.

Ciao,
Rajiv

"Always strive for perfection, but when you achieve it, what then will you strive for??" :p

 shabbir 19Feb2006 09:02

Re: Chess using BGI Graphics in C/C++

Quote:
 Originally Posted by rai_gandalf THX A LOT for the appreciation. Also, could u tell me how to giv a screenshot. Ciao, Rajiv "Always strive for perfection, but when you achieve it, what then will you strive for??" :p
You can upload the image anywhere you wish to and then use the bbcode IMG to show the image in the forum.

 shabbir 25Apr2006 21:15

Re: Chess using BGI Graphics in C/C++

Quote:
 Originally Posted by rai_gandalf THX A LOT for the appreciation. Also, could u tell me how to giv a screenshot. Ciao, Rajiv "Always strive for perfection, but when you achieve it, what then will you strive for??" :p
Or can even mail me if you wish to.

 tjweb 19Jul2006 11:12

Re: Chess using BGI Graphics in C/C++

Hope you don't mind a little critisism

-I can see some problems here. Have you checked to make sure the GFX will be compatable.
-And it is also in DOS. This will be a frown when you get done.
-For a game in C++ there should be a resolution
-No error handlers Read bottom

But i think you could easily make this a commercial grade game (if you make it windows or higher resolution DOS)

-TIP Try using error handlins such as if bishop1_spceclr="w"then err 2
THIS TIP IS NOT IN C++ AND MAY NOT BE COMATABLE WITH CURRENT VARS

 imrantechi 13Feb2008 14:22

Re: Chess using BGI Graphics in C/C++

can we please have the image or the screen shot

Re: Chess using BGI Graphics in C/C++

its good

 crazytolearn57 26Feb2008 18:34

Re: Chess using BGI Graphics in C/C++

good one

 skp819 6Jan2009 11:08

Re: Chess using BGI Graphics in C/C++

Nice and impressive.

 mayjune 14Aug2009 03:21

Re: Chess using BGI Graphics in C/C++

very nice interface...

 naimish 14Aug2009 07:35

Re: Chess using BGI Graphics in C/C++

Really nice work buddy :D

 ZuDoRa 23Jan2010 21:12

Re: Chess using BGI Graphics in C/C++

hey itz really osm bt pweeej cn sm1 guide meh in hw ta make muvmentz in dis????

 saurabhsrk 5Apr2016 07:53

Re: Chess using BGI Graphics in C/C++

Its compiling but after clicking run command nothing's happenning,Plaese help sir

 All times are GMT +5.5. The time now is 14:15.