1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Chess using BGI Graphics in C/C++

Discussion in 'Game Programming' started by rai_gandalf, Feb 18, 2006.

?

How do u rate the Chess Board using BGI ??

  1. DAMN GOOD!! (Too Good to be True)

    21.9%
  2. Impressive!!

    65.6%
  3. More could have been done

    12.5%
  4. BAD!! Rotten Tomatoes!!

    0 vote(s)
    0.0%
  1. rai_gandalf

    rai_gandalf New Member

    Joined:
    Nov 4, 2005
    Messages:
    46
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Final Year Comp Engg
    Location:
    Mumbai
    Home Page:
    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:
    /* CHESS USING [URL=http://www.go4expert.com/articles/bgi-graphics-dev-cpp-t23095/]BGI GRAPHICS[/URL] 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:
    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:
    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:
    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:
    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:
    /* 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 :)
     

    Attached Files:

    Last edited: Feb 28, 2006
    1 person likes this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    I dont have the TC compiler but the code rally looked impressive.
     
  4. rai_gandalf

    rai_gandalf New Member

    Joined:
    Nov 4, 2005
    Messages:
    46
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Final Year Comp Engg
    Location:
    Mumbai
    Home Page:

    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
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    You can upload the image anywhere you wish to and then use the bbcode IMG to show the image in the forum.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Or can even mail me if you wish to.
     
  7. tjweb

    tjweb New Member

    Joined:
    Jul 19, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  8. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    can we please have the image or the screen shot
     
  9. lead.smart34

    lead.smart34 New Member

    Joined:
    Feb 14, 2008
    Messages:
    77
    Likes Received:
    0
    Trophy Points:
    0
  10. crazytolearn57

    crazytolearn57 New Member

    Joined:
    Feb 14, 2008
    Messages:
    48
    Likes Received:
    0
    Trophy Points:
    0
  11. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    Nice and impressive.
     
  12. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    817
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    very nice interface...
     
  13. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Really nice work buddy :D
     
  14. ZuDoRa

    ZuDoRa New Member

    Joined:
    Jan 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey itz really osm bt pweeej cn sm1 guide meh in hw ta make muvmentz in dis????
     
  15. saurabhsrk

    saurabhsrk New Member

    Joined:
    Apr 5, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Its compiling but after clicking run command nothing's happenning,Plaese help sir
     

Share This Page