- /*******************************************************
- les tours de hanoi
- (c) 2002 md-soft
- ********************************************************/
-
- #include <stdio.h>
- #include <conio.h>
-
- int tours[3][8]={ {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0} };
- int compteur = 0;
-
- /******************************************************
- Deplacement des disques
- *******************************************************/
- void move(int depart,int arrivee){
- int i,j;
- compteur++;
- for(i=0;tours[depart][i]!=0;i++);
- i--;
- for(j=0;tours[arrivee][j]!=0;j++);
- tours[arrivee][j]=tours[depart][i];
- tours[depart][i]=0;
- aff_tours(tours);
- }
-
- /******************************************************
- choix de l'intermediaire
- *******************************************************/
- int autre(int tdeb,int tarr){
- return (3-tdeb-tarr);
- }
-
-
- /******************************************************
- fonction hanoi
- ******************************************************/
- void hanoi(int disk,int tdeb,int tarr){
- if(disk==1){
- move(tdeb,tarr);
- }
- else{
- int tinter=autre(tdeb,tarr);
- hanoi(disk-1,tdeb,tinter);
- move(tdeb,tarr);
- hanoi(disk-1,tinter,tarr);
- }
- }
-
- /******************************************************
- affichage des disques
- ******************************************************/
- aff_disk(int t_disk){
- int larg,nb_blanc,i;
- if(t_disk==0){
- printf("%21s"," ");
- }
- else{
- larg=(t_disk*2)-1;
- nb_blanc=(21-larg)/2;
- for(i=0;i<nb_blanc;i++){
- printf(" ");
- }
- for(i=0;i<larg;i++){
- printf("Û");
- }
- for(i=0;i<nb_blanc;i++){
- printf(" ");
- }
- }
- }
-
- /******************************************************
- affichage des tours
- ******************************************************/
- aff_tours(int table[3][8]){
- int n;int i;
- clrscr();
- printf("Coup nø%d\n", compteur);
- for(i=7;i>=0;i--)
- {
- for(n=0;n<3;n++)
- {
- aff_disk(table[n][i]);
- }
- printf("\n");
- }
- sleep(1);
- }
-
- /******************************************************
- mise en place des disques
- ******************************************************/
- int disque(int n){
- int i;
- for(i=0;i<n;i++){
- tours[0][i]=n-i;
- }
- }
- /******************************************************
- programme principal
- ******************************************************/
- main(){
- int n;
-
- clrscr();
- printf(" ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
- printf(" º Les tours de hanoi º\n");
- printf(" ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n\n");
- do{
- printf("Choisissez un nombre de disque entre 3 et 8: ");
- scanf("%d",&n);
- }while((n<3) || (n>8));
-
- disque(n);
- aff_tours(tours);
- printf("\n");
- hanoi(n,0,2);
- }
/*******************************************************
les tours de hanoi
(c) 2002 md-soft
********************************************************/
#include <stdio.h>
#include <conio.h>
int tours[3][8]={ {0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0} };
int compteur = 0;
/******************************************************
Deplacement des disques
*******************************************************/
void move(int depart,int arrivee){
int i,j;
compteur++;
for(i=0;tours[depart][i]!=0;i++);
i--;
for(j=0;tours[arrivee][j]!=0;j++);
tours[arrivee][j]=tours[depart][i];
tours[depart][i]=0;
aff_tours(tours);
}
/******************************************************
choix de l'intermediaire
*******************************************************/
int autre(int tdeb,int tarr){
return (3-tdeb-tarr);
}
/******************************************************
fonction hanoi
******************************************************/
void hanoi(int disk,int tdeb,int tarr){
if(disk==1){
move(tdeb,tarr);
}
else{
int tinter=autre(tdeb,tarr);
hanoi(disk-1,tdeb,tinter);
move(tdeb,tarr);
hanoi(disk-1,tinter,tarr);
}
}
/******************************************************
affichage des disques
******************************************************/
aff_disk(int t_disk){
int larg,nb_blanc,i;
if(t_disk==0){
printf("%21s"," ");
}
else{
larg=(t_disk*2)-1;
nb_blanc=(21-larg)/2;
for(i=0;i<nb_blanc;i++){
printf(" ");
}
for(i=0;i<larg;i++){
printf("Û");
}
for(i=0;i<nb_blanc;i++){
printf(" ");
}
}
}
/******************************************************
affichage des tours
******************************************************/
aff_tours(int table[3][8]){
int n;int i;
clrscr();
printf("Coup nø%d\n", compteur);
for(i=7;i>=0;i--)
{
for(n=0;n<3;n++)
{
aff_disk(table[n][i]);
}
printf("\n");
}
sleep(1);
}
/******************************************************
mise en place des disques
******************************************************/
int disque(int n){
int i;
for(i=0;i<n;i++){
tours[0][i]=n-i;
}
}
/******************************************************
programme principal
******************************************************/
main(){
int n;
clrscr();
printf(" ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
printf(" º Les tours de hanoi º\n");
printf(" ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n\n");
do{
printf("Choisissez un nombre de disque entre 3 et 8: ");
scanf("%d",&n);
}while((n<3) || (n>8));
disque(n);
aff_tours(tours);
printf("\n");
hanoi(n,0,2);
}