I have been trying to make my hanoi towers program, which allows the user to select the movement that will do, in principle it works well, but as it progresses, it does not allow to make the changes, I think not I am correctly using the counters of each tower, I am trying to do with pointers, but it is the first time I work with them, so I am very new, I do not know if I am implementing them correctly.
#include <stdio.h>
#include <stdlib.h>
struct torre
{
int disco[5];
} typedef torre;
torre *t1,*t2,*t3;
int *at1, *at2, *at3;
int main()
{
int i,j,k=0,ban=0;
system("clear");
t1=(torre *)malloc(sizeof(torre));
t2=(torre *)malloc(sizeof(torre));
t3=(torre *)malloc(sizeof(torre));
for(i=0; i<5; i++)
{
t1->disco[i]=k+1;
t2->disco[i]=0;
t3->disco[i]=0;
k++;
}
while (ban==0)
{
head();
scanf("%d",&j);
switch (j)
{
case 1:
head();
t1a2();
break;
case 2:
head();
t2a1();
break;
case 3:
head();
t2a3();
break;
case 4:
head();
t3a2();
break;
case 5:
ban=1;
printf("Adios\n");
break;
default:
printf("\E[1;31mSelecciona una opcion valida\E[00m");
}
}
}
head()
{
system("clear");
printf("\n\E[1;34m Bienvenido al juego\n TORRES DE HANOI\n 5 Discos\E[00m\n\n\E[1;33mReglas\n*Sólo se puede mover un disco cada vez\n*Un disco de mayor tamaño no puede descansar sobre uno más pequeño\n*Sólo puedes desplazar el disco que se encuentre arriba\E[00m\n\n");
printf("Selecciona un opcion: \n1:T1->T2\n2:T2->T1\n3:T2->T3\n4:T3->T2\n5: Salir\n");
imprime();
}
nope()
{
system("clear");
printf("\E[1;31mMovimiento invalido..Lee bien las reglas\E[00m");
}
imprime()
{
int i;
for(i=0; i<5; i++)
{
printf(" |%d|\t |%d|\t |%d|\n",t1->disco[i],t2->disco[i],t3->disco[i]);
}
printf("Torre 1\tTorre 2\tTorre 3\n");
}
int d1=0,d2=4,d3=4;
t1a2()
{
at1=&t1->disco[d1];
at2=&t2->disco[d2];
if(*at2==0)
{
*at2=*at1;
*at1=0;
d1++;
at1=&t1->disco[d1];
}
if (*at2!=0 && *at1!=0 && *at2>*at1)
{
at2=&t2->disco[d2];
*at2=*at1;
*at1=0;
}
else
{
nope();
}
printf("d1 %d,d2 %d",d1,d2);
}
t2a1()
{
at1=&t1->disco[d1];
at2=&t2->disco[d2];
if(*at1==0)
{
*at1=*at2;
*at2=0;
d2++;
}
if (*at1!=0 && *at2!=0 && *at2<*at1)
{
d1--;
at1=&t1->disco[d1];
*at1=*at2;
*at2=0;
}
else
{
nope();
}
printf("d1 %d,d2 %d",d1,d2);
}
t2a3()
{
at2=&t2->disco[d2];
at3=&t3->disco[d3];
if(*at3==0)
{
*at3=*at2;
*at2=0;
}
if (*at3>*at2 && *at2!=0 && *at3>*at2)
{
d2--;
at2=&t2->disco[d2];
*at3=*at2;
*at2=0;
}
else
{
nope();
}
printf("d2 %d,d3 %d",d2,d3);
}
t3a2()
{
at2=&t2->disco[d2];
at3=&t3->disco[d3];
if(*at2==0)
{
*at2=*at3;
*at3=0;
}
if (*at2!=0 && *at3!=0 && *at3<*at2)
{
d2--;
at2=&t2->disco[d2];
*at2=*at3;
*at3=0;
}
else
{
nope();
}
printf("d2 %d,d3 %d",d2,d3);
}