Tours de hanoi avec turbo c ( graphique )

Description

resolution graphique ; tours de hanoi
pour toutes informations ou suggestions :
yassou3@fr.fm
merci pour votre interet
et bonne chance.

Source / Exemple :


#include <graphics.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <conio.h>
 #include <dos.h>
 struct rect{                    /*declaration de la structure globale */
	 int left1;              /*qui contient les positions du rectangle*/
	 int top1;
	 int right1;
	 int bottom1;
	 int col;
	 }p1;

 int x,p;
 int z=0;
 int j=0;
 int y=0;
 int max[8][5],left,right,top,bottom;
void lignes();
struct rect tab[3][20];
int nc,x1=0,y1=0,z1=0,co,pos;
char a1='a',b1='b',c1='c';

			       // procedure de designe les lignes
void lignes()
{
   setcolor(getmaxcolor());
   int xmax=getmaxx(),ymax=getmaxy();
   line(xmax-800,ymax,xmax,ymax);
   line(xmax-540,ymax-350,xmax-540,ymax);	//1er ligne
   line(xmax-300,ymax-350,xmax-300,ymax);       //2eme ligne
   line(xmax-80,ymax-350,xmax-80,ymax);         //3eme ligne
}
void deplacer1(char a1, char b1)
{

if(a1=='a') { pos=x1; if(x1>0) x1--; co=0; }
if(a1=='b') { pos=y1; if(y1>0) y1--; co=1; }
if(a1=='c') { pos=z1; if(z1>0) z1--; co=2; }

p1=tab[co][pos];

while(p1.bottom1>=getmaxy()-350)     //du bas vers le haut
{
setcolor(getmaxcolor()-15);
rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1-=10;
       p1.bottom1-=10;
setcolor(getmaxcolor()-p1.col);
rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

//delay(50);

}
if((a1=='a')&&(b1=='b'))              //de a vers b
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-300)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1+=1;
    p1.right1+=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(y1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}
if((a1=='a')&&(b1=='c'))               //de a vers c
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-80)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1+=1;
    p1.right1+=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(z1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}

if((a1=='b')&&(b1=='a'))                     //de b vers a
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-540)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1-=1;
    p1.right1-=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(x1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}

if((a1=='b')&&(b1=='c'))                          //de b vers c
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-80)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1+=1;
    p1.right1+=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(z1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}

if((a1=='c')&&(b1=='b'))                //de c vers b
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-300)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1-=1;
    p1.right1-=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(y1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}
if((a1=='c')&&(b1=='a'))                  //de c vers a
{
 while(p1.left1-(p1.left1-p1.right1)/2!=getmaxx()-540)
 {
 setcolor(getmaxcolor()-15);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);

    p1.left1-=1;
    p1.right1-=1;
 setcolor(getmaxcolor()-p1.col);
 rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
// delay(10);
  }
  while(p1.bottom1!=getmaxy()-(x1+2)*10)
  {
  setcolor(getmaxcolor()-15);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
       p1.top1+=1;
       p1.bottom1+=1;
  setcolor(getmaxcolor()-p1.col);
  rectangle(p1.left1,p1.top1,p1.right1,p1.bottom1);
  lignes();
//  delay(10);
 }
}
if(b1=='a') {   x1++;    tab[0][x1]=p1; }
if(b1=='b') {   y1++;    tab[1][y1]=p1; }
if(b1=='c') {   z1++;    tab[2][z1]=p1; }

lignes();

}
			      // procedure Hanoi recursive
void hanoi1(int nc,char a1,char b1,char c1)
{
if(nc==1)    deplacer1(a1,b1);
else
 {
  hanoi1((nc-1),a1,c1,b1);
  hanoi1(1,a1,b1,c1);
  hanoi1((nc-1),c1,b1,a1);
 }
}

 void deplacer(int a,int c)
{
  int i,n;
  if(a==1) p=x;
  if(a==4)
  {
  y--;
  p=y;
  }
  if(a==7)
  {
  z--;
  p=z;
  }
   max[c][j]-=40;
     for(i=0;i<(max[a][j]/20)-3;i++)
   {
    setcolor(getmaxcolor());
    ellipse(a*80, max[a][j]-i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80+1, max[a][j]-i*20,getmaxcolor());
    floodfill(a*80-1, max[a][j]-i*20,getmaxcolor());
       delay(10);
   ellipse(a*80,max[a][j]-i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-15);
    floodfill(a*80+1, max[a][j]-i*20,getmaxcolor());
    floodfill(a*80-1, max[a][j]-i*20,getmaxcolor());
    setcolor(getmaxcolor()-15);
    ellipse(a*80,max[a][j]-i*20,0,360,max[a+1][p],20);
   }
if(a<c)
{
     for(i=0;i<(c-a)*2;i++)
   {
    setcolor(getmaxcolor());
    ellipse(a*80+i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80+i*40,max[a][j]-((max[a][j]/20)-3)*20,getmaxcolor());
    delay(10);
    ellipse(a*80+i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-15);
    floodfill(a*80+i*40,max[a][j]-((max[a][j]/20)-3)*20,getmaxcolor());
    setcolor(getmaxcolor()-15);
    ellipse(a*80+i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
    }
  for(i=0;i<(max[c][j]/20)-3;i++)
   {
    setcolor(getmaxcolor());
    ellipse(a*80+(c-a)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80+(c-a)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80+(c-a)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
   delay(10);
    ellipse(a*80+(c-a)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-15);
    floodfill(a*80+(c-a)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80+(c-a)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    setcolor(getmaxcolor()-15);
    ellipse(a*80+(c-a)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);

   }
    setcolor(getmaxcolor());
    ellipse(a*80+(c-a)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80+(c-a)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80+(c-a)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
  }
if(a>c)
{
     for(i=0;i<(a-c)*2;i++)
   {
    setcolor(getmaxcolor());
    ellipse(a*80-i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80-i*40+1,max[a][j]-((max[a][j]/20)-3)*20,getmaxcolor());
    delay(10);
    ellipse(a*80-i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-15);
    floodfill(a*80-i*40+1,max[a][j]-((max[a][j]/20)-3)*20,getmaxcolor());
    setcolor(getmaxcolor()-15);
    ellipse(a*80-i*40,max[a][j]-((max[a][j]/20)-3)*20,0,360,max[a+1][p],20);
   }
    for(i=0;i<(max[c][j]/20)-3;i++)
   {
    setcolor(getmaxcolor());
    ellipse(a*80-(a-c)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80-(a-c)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80-(a-c)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    delay(10);
    ellipse(a*80-(a-c)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-15);
    floodfill(a*80-(a-c)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80-(a-c)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    setcolor(getmaxcolor()-15);
    ellipse(a*80-(a-c)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
   }
    setcolor(getmaxcolor());
    ellipse(a*80-(a-c)*80,max[a][j]-((max[a][j]/20)-3)*20+i*20,0,360,max[a+1][p],20);
    setfillstyle(SOLID_FILL, getmaxcolor()-8);
    floodfill(a*80-(a-c)*80+1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
    floodfill(a*80-(a-c)*80-1,max[a][j]-((max[a][j]/20)-3)*20+i*20,getmaxcolor());
 }
 max[a][j]+=40;
 if(c==4)
 {
 max[c+1][y]=max[a+1][p];
 y++;
 }
 if(c==7)
 {
 max[c+1][z]=max[a+1][p];
 z++;
 }
 if(c==1)
 {
 x++;
 max[c+1][x]=max[a+1][p];
 }
 if((a==1)&&(x>=0))
 {
  p--;
  x=p;
  }
  if(a==4)
  y=p;
  if(a==7)
  z=p;
}

void hanois(int a,int b,int c,int n)
{
if(n==1)  deplacer(a,c);
else
 {
  hanois(a,c,b,n-1);
  hanois(a,b,c,1);
  hanois(b,a,c,n-1);
 }
}

    int main(void)
 {
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    int i,n,m;

    initgraph(&gdriver, &gmode, "d:\\tc\\bgi");
    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)
    /* an error occurred */
    {
       printf("Graphics error: %s\n", grapherrormsg(errorcode));
       printf("Press any key to halt:");
       getch();
       exit(1); /* terminate with an error code */
    }
    printf("øøøøøøøøøøøøøøøøMENUøøøøøøøøøøøøøøø");
    printf("\n<1> travailler avec des Ellipses.");
    printf("\n<2> travailler avec des Barres 3D.");
    printf("\n<3> Quitter");
    printf("\n\nøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøø");
    printf("\n\nentrer votre choix  :  ");
    scanf("%d",&m);
   switch(m)
   {
   case 1:{
	  cleardevice();
	  printf("\n\t\t\tentrer le nombre de disques n : ");
	  scanf("%d",&n);
	  cleardevice();
	  line(80,getmaxy()-30,80,100);
	  line(320,getmaxy()-30,320,100);
	  line(560,getmaxy()-30,560,100);
	  line(0,getmaxy()-30,getmaxx(),getmaxy()-30);
	  outtextxy(80,getmaxy()-20,"A");
	  outtextxy(320,getmaxy()-20,"B");
	  outtextxy(560,getmaxy()-20,"C");
	  int a=1;
	  int b=4;
	  int c=7;
	  max[1][0]=400-(n-1)*40;
	  max[4][0]=440;
	  max[7][0]=440;
	  for(i=0,x=0;i<n;i++)
	   {
	  ellipse(80, 400-i*40,0,360,80-i*10,20);
	  setfillstyle(SOLID_FILL, getmaxcolor()-8);
	  floodfill(81, 400-i*40,getmaxcolor());
	  floodfill(79, 400-i*40,getmaxcolor());
	  max[2][x]=80-i*10  ;
	  x++;
	   }
	  x--;
	  sleep(1);
	  hanois(a,b,c,n);
	  //cleardevice();
	  sleep(1);
	  cleardevice();
	  outtextxy(200,160,"Merci pour votre visite");
	  outtextxy(200,180,"Ecole Superieure de Technologie de Meknes(MAROC)");
	  outtextxy(200,200,"Tours de Hanois ( Mini projet )");
	  outtextxy(200,220,"R?alis? par : ");
	  outtextxy(200,240,"HICHAM et YASSINE");
	  getch();

	  return 0 ;
	  }
case 2:   {
	  cleardevice();
	  printf("\n\t\t\tentrer le nombre de disques n : ");
	  scanf("%d",&nc);
	  getch();
	  cleardevice();
	  lignes();
	  gotoxy(11,3); printf("DEBUT");
	  gotoxy(42,3); printf("BUT");
	  gotoxy(65,3); printf("INTERMEDIAIRE");
	  left=getmaxx()-600,top=getmaxy()-17,right=getmaxx()-480,bottom=getmaxy()-10;
	  /* designe des rectangles */
	  for(i=0;i<nc;i++)
	    {
	    setcolor(getmaxcolor()-i);
	    rectangle(left, top, right, bottom);
	    p1.left1=left;    p1.top1=top;
	    p1.right1=right;  p1.bottom1=bottom;
	    p1.col=i;
	    tab[0][x1]=p1;                          // stockage des variables de struct rect ds tab
	    left+=5;         top-=10;
	    right-=5;        bottom-=10;
	    delay(1000);
	    x1++;
	     }
	    x1--;
	    hanoi1(nc,a1,b1,c1);
	   /* clean up */
	    getch();
	    cleardevice();
	    outtextxy(200,160,"Merci pour votre visite");
	    outtextxy(200,180,"Ecole Superieure de Technologie de Meknes(MAROC)");
	    outtextxy(200,200,"Tours de Hanois ( Mini projet )");
	    outtextxy(200,220,"R?alis? par : ");
	    outtextxy(200,240,"HICHAM et YASSINE");
	    getch();
	    closegraph();
	    return 0;
   break;
	  }
case 3:   {
	  cleardevice();
	  outtextxy(200,190,"-----------------------");
	  outtextxy(200,200,"MERCI POUR VOTRE VISITE");
	  outtextxy(200,210,"-----------------------");
	  getch();
	  exit(0);
	  }
default : {
	  printf("vous avez choisi de quitter");
	  getch();
	  exit(0);
	  }
}
getch();
closegraph();
}

Codes Sources

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.