Jeu dame c++ (tc)

Contenu du snippet

UN jeu de dame realiser par un amateur
a+

Source / Exemple :


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>

#define   debx   120

 
 int h=20;
 int x, y, i, j, i3, j3, c, e, k=1;
 int x1, y1, x2, y2, x3, y3, ch;
 int indice, ind=1;
 char msg[10];
 typedef struct { int c1,c2,c3;
		 } tab;
 tab g[65];

 

 void pion(int i2,int j2,int c) //{*************** fonction ****************}
 {
  int d;
  if(c==12 || c==15) d=1;
    else if(c==13) { c=15; d=2; }
	   else if(c==16) { c=12; d=2; }
  setcolor(c);
  setfillstyle(d,c);
  setlinestyle(0,1,1);
  j3=debx-50+i2*50; i3=40+j2*50;
  circle(j3+25,i3-50+24,17); floodfill(j3+24,i3-50+15,c);}

 void Table() //{*********************procedure************************}
  {
   for(i=1;i<=64;i++) { g[i].c1=BROWN; g[i].c2=0; g[i].c3=6; }
   for(j=2;j<=8;j+=2) for(i=2;i<=8;i+=2) g[i+(j-1)*8].c1=CYAN;
   for(j=1;j<=8;j+=2) for(i=1;i<=8;i+=2) g[i+(j-1)*8].c1=CYAN;

   cleardevice();
   setlinestyle(0,2,3);
   setfillstyle(1,GREEN);
   bar(0,0,getmaxx(),getmaxy()-5);
   setcolor(LIGHTRED);
   rectangle(0,0,getmaxx(),getmaxy()-5);

   x=debx; y=40;
   setfillstyle(0,BLACK);
   bar(x+20,y+20,x+420,y+420);
   rectangle(x,y,x+400,y+400);

   for (i=1;i<=8;i++) { x = debx;
   for (j=1;j<=8;j++) { setcolor(YELLOW);
			rectangle(x,y,x+50,y+50);
			setfillstyle(1,g[i+(j-1)*8].c1);
			bar(x+2,y+2,x+48,y+48);
			x+=50;
		      } y+=50;
		      }
  }

 void Initialit() //{*********************procedure************************}
  {
   for(i=1;i<=8;i+=2)
   { pion(i,1,WHITE); g[i+(1-1)*8].c2=1; g[i+(1-1)*8].c3=WHITE; }
   for(i=2;i<=8;i+=2)
   { pion(i,2,WHITE); g[i+(2-1)*8].c2=1; g[i+(2-1)*8].c3=WHITE; }
   for(i=1;i<=8;i+=2)
   { pion(i,3,WHITE); g[i+(3-1)*8].c2=1; g[i+(3-1)*8].c3=WHITE; }

   for(i=2;i<=8;i+=2)
   { pion(i,6,LIGHTRED); g[i+(6-1)*8].c2=1; g[i+(6-1)*8].c3=LIGHTRED; }
   for(i=1;i<=8;i+=2)
   { pion(i,7,LIGHTRED); g[i+(7-1)*8].c2=1; g[i+(7-1)*8].c3=LIGHTRED; }
   for(i=2;i<=8;i+=2)
   { pion(i,8,LIGHTRED); g[i+(8-1)*8].c2=1; g[i+(8-1)*8].c3=LIGHTRED; }

   settextstyle(1,HORIZ_DIR,3); // 1 ou 4 ou 7
   setcolor(WHITE);
   for(i=0;i<=7;i++) { sprintf(msg,"%c",i+65); outtextxy(debx+20+i*50,10,msg); }
   for(i=0;i<=7;i++) { sprintf(msg,"%d",i+1 ); outtextxy(debx-20,50+i*50,msg); }

  }

 void Select(int i,int j) //{*************** fonction ****************}
  {
   setfillstyle(1,LIGHTMAGENTA);
   bar(debx-48+i*50,j*50-8,debx-2+i*50,j*50+38);
   if(g[i+(j-1)*8].c2) pion(i,j,g[i+(j-1)*8].c3);
  }

 void Bare(int i,int j) //{*************** fonction ****************}
  {
   setfillstyle(1,g[i+(j-1)*8].c1);
   bar(debx-48+i*50,j*50-8,debx-2+i*50,j*50+38);
   if(g[i+(j-1)*8].c2) pion(i,j,g[i+(j-1)*8].c3);
  }

 int Eguale(int x3,int y3,int i,int j) //{*************** fonction ***************}
 {
   if(g[x3+(y3-1)*8].c3==13 && ( g[i+(j-1)*8].c3==13 || g[i+(j-1)*8].c3==15 ) ) return(1);
    else if(g[x3+(y3-1)*8].c3==16 && ( g[i+(j-1)*8].c3==16 || g[i+(j-1)*8].c3==12 ) ) return(1);
	  else return(0);
 }

 void deplacer() //{*********************procedure************************}
  {
   if( g[x2+(y2-1)*8].c2==1  || g[x2+(y2-1)*8].c1==BROWN  || abs(x3-x2)==0 || abs(y3-y2)==0)
      { sound(310); delay(300); nosound(); goto ET; }

   if( (g[x3+(y3-1)*8].c3==12 && y2-y3>0) || (g[x3+(y3-1)*8].c3==15 && y2-y3<0) )
      { sound(310); delay(300); nosound(); goto ET; }

   if(g[x3+(y3-1)*8].c3!=13 && g[x3+(y3-1)*8].c3!=16)
   if(abs(x3-x2)>2||abs(y3-y2)>2) { sound(310); delay(300); nosound(); goto ET; }

  int pax, pay;
  if(g[x3+(y3-1)*8].c3==13 || g[x3+(y3-1)*8].c3==16)
  { if(abs(x3-x2)!=abs(y3-y2)) { sound(310); delay(300); nosound(); goto ET; }
    pax=(x2-x3)/abs(x2-x3); pay=(y2-y3)/abs(y2-y3); i=x3+pax; j=y3+pay;

    while(abs(i-x2)!=0) { if( g[i+(j-1)*8].c2==1 && (Eguale(x3,y3,i,j) || g[i+pax+(j-1+pay)*8].c2!=0) ) { sound(310); delay(300); nosound(); goto ET; }
			  else { i+=pax; j+=pay; }
			}
    i=x3+pax; j=y3+pay;
    while(abs(i-x2)!=0) { if(g[i+(j-1)*8].c2==1) { g[i+(j-1)*8].c2=0; Bare(i,j); }
			  i+=pax; j+=pay;
			}
  }

  else if((abs(x3-x2)==2||abs(y3-y2)==2))
   {
    if(x3-2==x2 && y3-2==y2) e=1;
    else if(x3+2==x2 && y3-2==y2) e=2;
    else if(x3-2==x2 && y3+2==y2) e=3;
    else if(x3+2==x2 && y3+2==y2) e=4;

   switch(e)
   {
   case 1 : { if(g[x2+1+y2*8].c2==1 && g[x2+1+y2*8].c3!=g[x3+(y3-1)*8].c3) { g[x2+1+y2*8].c2=0; Bare(x2+1,y2+1); } else { sound(310); delay(300); nosound(); goto ET; } } break;
   case 2 : { if(g[x2-1+y2*8].c2==1 && g[x2-1+y2*8].c3!=g[x3+(y3-1)*8].c3) { g[x2-1+y2*8].c2=0; Bare(x2-1,y2+1); } else { sound(310); delay(300); nosound(); goto ET; } } break;
   case 3 : { if(g[x2+1+(y2-2)*8].c2==1 && g[x2+1+(y2-2)*8].c3!=g[x3+(y3-1)*8].c3) { g[x2+1+(y2-2)*8].c2=0; Bare(x2+1,y2-1); } else { sound(310); delay(300); nosound(); goto ET; } } break;
   case 4 : { if(g[x2-1+(y2-2)*8].c2==1 && g[x2-1+(y2-2)*8].c3!=g[x3+(y3-1)*8].c3) { g[x2-1+(y2-2)*8].c2=0; Bare(x2-1,y2-1); } else { sound(310); delay(300); nosound(); goto ET; } } break;
   }
   }
   g[x2+(y2-1)*8].c2 = 1;
   g[x3+(y3-1)*8].c2 = 0;

   g[x2+(y2-1)*8].c3 = g[x3+(y3-1)*8].c3;
   g[x3+(y3-1)*8].c3 = 6;

   if(y2==8 && g[x2+(y2-1)*8].c3==15) g[x2+(y2-1)*8].c3=13;
    else if(y2==1 && g[x2+(y2-1)*8].c3==12) g[x2+(y2-1)*8].c3=16;

   Bare(x3,y3);
   Bare(x2,y2);
 ET:  ind = 1;

  }

 void Damma()  //{*********************procedure************************}
  {
   x1=x2=1; y1=y2=1;
   Select(x1,y1);

 while (ch!=27) {

   x2=x1; y2=y1;

TQ:ch=getch();

   if(ch==13) if(ind) if(g[x2+(y2-1)*8].c2==0) goto TQ;
		       else { x3=x2; y3=y2; indice=1; ind=0; ch=getch(); }
	       else  deplacer();

   switch(ch)
   {
   case 53 :{ x1--; y1++; } break;
   case 54 :{ x1++; y1++; } break;
   case 56 :{ x1--; y1--; } break;
   case 57 :{ x1++; y1--; } break;
   }
    if(x1==0 && ch==56) { x1++; y1++; }
    else if(x1==0 && ch==53) { x1++; y1--; }

    else if(x1==9 && ch==57) { x1--; y1++; }
    else if(x1==9 && ch==54) { x1--; y1--; }
    else if(y1==0 && ch==56) { x1++; y1++; }
    else if(y1==9 && ch==53) { x1++; y1--; }
    else if(y1==0 && ch==57) { x1--; y1++; }
    else if(y1==9 && ch==54) { x1--; y1--; }

   if(!indice) if(x2!=x3 || y2!=y3 || ind==1) Bare(x2,y2);
   indice=0;
   Select(x1,y1);
		  }
  }

int main(void)        /*****      PROGRAMME PRINCIPALE      88888*****/

{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   

   Table();

   Initialit();

   Damma();

   /* clean up */
   closegraph();
   return 0;

}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.