I was developing my program which consists of simulating the game of the towers of hanoi, although this is managed by the player instead of the program like all the ones I have seen. And I got stuck in this part that consists of asking the user what the disk is going to be extracted from and then asking him where he will place or stack the disk, I suppose it would be a problem of the conditions or conditioners but at the moment of selecting where he will place the disk despite After the case is over, it goes on to the next case, instead of starting the do while again. The problem should be in this section I think.
//Iimplement recursive tower of hanoi
void t_Hanoi(int n, TDA_PILA& A, TDA_PILA& B, TDA_PILA& C,
TDA_PILA* a, TDA_PILA* b, TDA_PILA* c) {
int e;
char aux, aux1;
//Identify which tower the discs are taken from
if (a == &A)
aux = 'A';
else if (b == &A)
aux = 'B';
else if (c == &A)
aux = 'C';
if (a == &C)
aux1 = 'A';
else if (b == &C)
aux1 = 'B';
else if (c == &C)
aux1 = 'C';
//recursion tower of hanoi
do{
int opcD;
int opcT;
printf("\t Escriba la torre que desee extraer un disco \n");
printf("\t 1. Torre 1 \n");
printf("\t 2. Torre 2 \n");
printf("\t 3. Torre 3 \n");
printf("\n");
mostrar(A);
mostrar2(B);
mostrar3(C);
scanf("%i", &opcD);
switch(opcD){
case 1:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 2. Torre 2 \n");
printf("\t 3. Torre 3 \n");
scanf("%i", &opcT);
if(opcT == 2){
pop(A, e);
push(B, e);
}
if(opcT == 3);{
pop(A, e);
push(C, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
case 2:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 1. Torre 1 \n");
printf("\t 3. Torre 3 \n");
scanf("%i", &opcT);
if(opcT == 1){
pop(B, e);
push(A, e);
}
else if(opcT == 3);{
pop(B, e);
push(C, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
case 3:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 1. Torre 1 \n");
printf("\t 2. Torre 2 \n");
scanf("%i", &opcT);
if(opcT == 1){
pop(C, e);
push(A, e);
}
else if(opcT == 2);{
pop(C, e);
push(B, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
//t_Hanoi(n - 1, A, C, B, a, b, c);
}
}while(empty(A) && empty(B));{
printf("Has Ganado. \n");
}
}
The same if you need it, I will leave you the complete code, I await your answers, thanks.
#include <stdio.h>
#include <stdlib.h>
//constante
#define TAMPILA 10
//iniziality stack
struct pila {
int item[TAMPILA];
int tope;
};
typedef struct pila TDA_PILA;
//prototype functions of the stack
int push(TDA_PILA& p, int e);
int pop(TDA_PILA& p, int& e);
int stacktop(TDA_PILA p, int& e);
int empty(TDA_PILA p);
void startpila(TDA_PILA& p);
void menu();
void reglas();
void Gameinit();
//prototypes functions of the hanoi
void t_Hanoi(int n, TDA_PILA& A, TDA_PILA& B, TDA_PILA& C,
TDA_PILA* a, TDA_PILA* b, TDA_PILA* c);
void mostrar3(TDA_PILA C);
void mostrar2(TDA_PILA B);
void mostrar(TDA_PILA A);
int main() {
menu();
}
void menu(){
int opc;
system("cls");
printf(" \t \t Menu Principal \n");
printf("\t Game Torres de hanoi \n");
printf("Elige la opcion deseeada \n");
printf("1. Jugar \n");
printf("2. Reglas \n");
printf("3. Salir \n");
scanf("%i", &opc);
switch(opc){
case 1: Gameinit(); break;
case 2: reglas(); break;
case 3:
break;
}
}
void Gameinit(){
int n;
//create and iniliazity the stacks
//La torre A sera usada como torre de origen
//La torre B sera usada como torre auxiliar
//La torre C sera usada como torre de destino
TDA_PILA A, B, C;
startpila(A);
startpila(B);
startpila(C);
//obtener las posiciones de las torres
TDA_PILA* a = &A;
TDA_PILA* b = &B;
TDA_PILA* c = &C;
printf("******\tTORRE DE HANOI\t******");
printf("\nIngrese la cantidad de disco(s): ");
scanf("%d", &n);
printf("\nInicializando la Torre A con disco(s) del 1 al %d", n);
for (int i = n; i > 0; i--)
push(A, i);
printf("\n\t La Torre esta llena");
printf("\nLos elementos de la Torre A-C son: \n");
mostrar(A);
mostrar2(B);
mostrar3(C);
printf("");
printf("\n\t");
system("pause");
t_Hanoi(n, A, B, C, a, b, c);
printf("\n\tDiscos movidos exitosamente");
printf("\nLos elementos de la Torre C son: \n ");
mostrar(C);
printf("\n\t");
system("pause");
menu();
}
//Enter the discs to the tower
int push(TDA_PILA& p, int e) {
if (p.tope == TAMPILA - 1)
return 0;
p.item[++p.tope] = e;
return 1;
}
//remove the disks from the tower
int pop(TDA_PILA& p, int& e) {
if (empty(p))
return 0;
e = p.item[p.tope--];
return 1;
}
//Function the stack
int stacktop(TDA_PILA p, int& e) {
if (empty(p))
return 0;
e = p.item[p.tope];
return 1;
}
//know if the tower is empty
int empty(TDA_PILA p){
if (p.tope == -1)
return 1;
return 0;
}
//iniziality stack of origin
void startpila(TDA_PILA& p){
p.tope = -1;
}
//Show the disks of the destination tower
void mostrar(TDA_PILA A) {
int e;
printf("Torre A:\n");
while (pop(A, e)){
printf("%d \n", e);
}
}
void mostrar2(TDA_PILA B){
int e;
printf("Torre B:\n");
while (pop(B, e)){
printf("%d \n", e);
}
printf("\n \n");
}
void mostrar3(TDA_PILA C){
int e;
printf("Torre C:\n");
while (pop(C, e)){
printf("%d \n", e);
}
printf("\n \n");
}
//Iimplement recursive tower of hanoi
void t_Hanoi(int n, TDA_PILA& A, TDA_PILA& B, TDA_PILA& C,
TDA_PILA* a, TDA_PILA* b, TDA_PILA* c) {
int e;
char aux, aux1;
//Identify which tower the discs are taken from
if (a == &A)
aux = 'A';
else if (b == &A)
aux = 'B';
else if (c == &A)
aux = 'C';
if (a == &C)
aux1 = 'A';
else if (b == &C)
aux1 = 'B';
else if (c == &C)
aux1 = 'C';
//recursion tower of hanoi
do{
int opcD;
int opcT;
printf("\t Escriba la torre que desee extraer un disco \n");
printf("\t 1. Torre 1 \n");
printf("\t 2. Torre 2 \n");
printf("\t 3. Torre 3 \n");
printf("\n");
mostrar(A);
mostrar2(B);
mostrar3(C);
scanf("%i", &opcD);
switch(opcD){
case 1:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 2. Torre 2 \n");
printf("\t 3. Torre 3 \n");
scanf("%i", &opcT);
if(opcT == 2){
pop(A, e);
push(B, e);
}
if(opcT == 3);{
pop(A, e);
push(C, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
case 2:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 1. Torre 1 \n");
printf("\t 3. Torre 3 \n");
scanf("%i", &opcT);
if(opcT == 1){
pop(B, e);
push(A, e);
}
else if(opcT == 3);{
pop(B, e);
push(C, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
case 3:
printf("\t Selecciona que torre deseas enpilar el disco\n");
printf("\t 1. Torre 1 \n");
printf("\t 2. Torre 2 \n");
scanf("%i", &opcT);
if(opcT == 1){
pop(C, e);
push(A, e);
}
else if(opcT == 2);{
pop(C, e);
push(B, e);
}
if(opcT>3);{
printf("Error, no se ha encontrado esa torre, volveras al menu \n");
printf("O no puedes apilar el disco a esa torre ya que es la de origen \n");
system("PAUSE");
menu();
}
//t_Hanoi(n - 1, A, C, B, a, b, c);
}
}while(empty(A) && empty(B));{
printf("Has Ganado. \n");
}
}
void reglas(){
printf("\t \t ----------REGLAS---------- \n");
printf("1. Siempre se mueve el disco mas pequeño de esa torre o el que esta mas arriba \n");
printf("2. No se puede devolver ningun disco a la torre de origen \n");
printf("3. No se puede colocar un disco mas grande sobre otro menor \n");
printf("4. Para ganar todos los discos se deben de encontrar en la torre destino de forma
ascendente. \n");
system("PAUSE");
menu();
}
Right after "I got stuck" comes the why and where I think the problem is originating, and I put the part of the code that is most likely generating the problem, which is that my conditions or conditionals are not working, Well, with the switch after performing case 1, jump to case 2.