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 :
Now, in the main( ), look at the following code-segment :
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 :
' 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 :
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 :
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 :
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
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);
}
Code: C
for(i=1,k+=y;m<8;i+=y,m++)
for(j=1,l=0;l<8;j+=x,l++)
Now, inside the for loops :
Code: C
if((l%2==1 && m%2==0) || (l%2==0 && m%2==1))
' 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))
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);
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);
}
(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


