Torres de hanoi

0

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);
}
    
asked by Jesus Hernandez Godinez 11.04.2016 в 00:11
source

1 answer

1

You can use this, you have to fix it because I did it in 10 minutes and there are errors but I think it's easier for you to change it.

#include <stdio.h>
#include <stdlib.h>
#define MAX_TOWERS 3
#define MAX_TOWERS_SIZE 5

struct torre
{
    char name;
    int disco[MAX_TOWERS_SIZE];
    int count;
} typedef torre;

torre *list_towers[MAX_TOWERS];

void head();
void imprime();


void init_towers()
{ 
    int i=0, j = 0;
    char identificador = 'a';

    for(int i=0;i<MAX_TOWERS ; i++)
    {
     list_towers[i]= (torre *)malloc(sizeof(torre));
     list_towers[i]->name = identificador++; //a > b > c > d ...

       int k = MAX_TOWERS_SIZE;
       for(j=0; j<MAX_TOWERS_SIZE; j++)
        {
            if(list_towers[i]->name=='a')
            {           
                list_towers[i]->disco[j]=k--;
                list_towers[i]->count = MAX_TOWERS_SIZE;
            }
            else
            {
                list_towers[i]->disco[j]=0;
                list_towers[i]->count = 0;
            }
        }

        /*printf("%c [0]%i [1]%i [2]%i [3]%i [4]%i qtd[%i]\n"  , list_towers[i]->name
                                        ,list_towers[i]->disco[0]
                                        ,list_towers[i]->disco[1]
                                        ,list_towers[i]->disco[2]
                                        ,list_towers[i]->disco[3]
                                        ,list_towers[i]->disco[4]
                                        ,list_towers[i]->count );*/

    }   
}

void move(char from, char to)
{
    torre *p_to,*p_from;
    printf("\nMoving from %c to %c\n", from, to);  

    for(int i=0;i<MAX_TOWERS ; i++)
    {
       if(list_towers[i]->name==to)
       {
         p_to = list_towers[i];
       }

       if(list_towers[i]->name==from)
       {
         p_from = list_towers[i];
       }       
    }    

       if(p_from->count==0)
       {
         printf("\nImpossible porque no existe elementos\n");   
         return;
       }

       if(p_to->count==MAX_TOWERS_SIZE)
       {
         printf("\nImpossible porque no esta lleno\n");   
         return;
       }

       //cambiando se menor o valor "to" igual a cero (vacia)
       if((p_to->disco[p_to->count] < p_from->disco[p_from->count]) || (p_to->count == 0))
       {    
           p_to->disco[p_to->count] = p_from->disco[(p_from->count)-1];
           p_from->disco[(p_from->count)-1] = 0;
           p_from->count--;
           p_to->count++;        
       }
       else
       {
         printf("\nImpossible porque el elemento necesita ser menor\n");   
         return;
       }

       imprime();   

} 

void 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: T1->T3\n6: Salir\n");
    imprime();
}

void imprime()
{
    int i;
    for(i=(MAX_TOWERS_SIZE-1); i>=0; i--)
    {
        printf("  |%d|\t  |%d|\t  |%d|\n",list_towers[0]->disco[i],list_towers[1]->disco[i],list_towers[2]->disco[i]);
    }
    printf("Torre 1\tTorre 2\tTorre 3\n");
}

int main()
{
    int i,j,k=0,ban=0;

    init_towers();

    //system("clear");
    imprime();

    move('a','b');
    move('a','b');
    move('a','c');
    move('b','c');
    move('c','a');

   // imprime();
}
    
answered by 11.04.2016 в 10:56