[CÓDIGO] Torre de Hanoi com 5 Discos
SOU INICIANTE NA LINGUAGEM C
E DESENVOLVI ESSE PROGRAMA SIMPLES - TORRE DE HANÓI COM 5 DISCOS
OBS: O CÓDIGO ESTÁ DISPONÍVEL PARA TODOS QUE QUISEREM MELHORÁ-LO
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // POW(); CALCULAR O VALOR DE MOVIMENTOS EXATOS DA TORRE DE HANOI;
#define POSICOES 5
int torre1[5]= {1,2,3,4,5}, torre2[5]={0,0,0,0,0}, torre3[5] ={0,0,0,0,0}; // ARRAYS;
int cont=0; // VARIAVEL PARA CONTAR O NUMERO DE JOGADAS;
void mostrarTorres(); // PROTOTIPO DA FUNÇÃO PARA MOSTRA O ESTADO ATUAL DA TORRE;
int valorTorre(int torre[5]); // PROTOTIPO DA FUNÇÃO PARA PEGAR O MENOR VALOR;
int destinoTorre(int torre[5]); // PROTOTIPO DA FUNÇÃO PARA PEGAR O MENOR VALOR;
void moverDiscos(int orig, int dest); // PROTOTIPO DA FUNÇÃO PARA MOVER DISCOS;
void mostrarMenu(); // PROTOTIPO DA FUNÇÃO PARA MOSTRAR O MENU;
int verificarFinal(); // PROTOTIPO DA FUNÇÃO QUE VERIFICA SE O JOGADOR FOI CAMPEÃO;
void regras(); // PROTOTIPO DA FUNÇÃO QUE MOSTRA AS REGRAS DO JOGO;
int main(void){
int op=1;
while(op!=0){
printf(" - TORRE DE HANOI \n 2 - JOGAR\n 1 - REGRAS\n 0 - SAIR\n - DIGITE: ");
scanf("%d", &op);
switch(op){
case 2:
mostrarTorres();
mostrarMenu();
break;
case 1:
regras();
break;
default:
if(op != 0){
printf(" - OPÇÃO INVALIDA INFORME NOVAMENTE\n - ");
system("PAUSE");
system("CLS");
}else{
system("CLS");
printf("\n\n\t\t\t ENCERRADO COM SUCESSO.\n");
}
break;
}
}
return 0;
}
int valorTorre(int torre[]){ // FUNÇÃO PARA ULTIMO VALOR DO ARRAY
int i, a=0;
for(i=POSICOES-1; i>=0; i--){
if(torre[i] != 0){
a = i;
}
if(torre[4] == 0){
a = 0;
}
}
return(a);
}
int destinoTorre(int torre[]){ // FUNÇÃO PARA RETORNA PROXIMO VALOR DE DESTINO
int i, a=0;
for(i=0; i<POSICOES; i++){
if(torre[i] == 0){
a = i;
}
}
return(a);
}
void moverDiscos(int orig, int dest){ // FUNÇÃO PARA MOVER OS DISCOS
int aux1, aux2, aux3;
// REGRAS (O DISCO DE ORIGEM NÃO PODE SER MAIOR QUE O DESTINO) E (VALOR DO DISCO DE DESTINO TEM QUE SER DIFERENTE DE 0);
// ORIGEM 1 - TORRE 1
if(orig == 1){
// DESTINO 2 - TORRE 2
if(dest == 2){
aux1 = torre1[valorTorre(torre1)];
aux2 = torre2[destinoTorre(torre2)+1] ;
aux3 = torre2[valorTorre(torre2)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre1[valorTorre(torre1)] = 0;
torre2[destinoTorre(torre2)] = aux1;
cont++;
}
}
// DESTINO 3 - TORRE 3
}else{
aux1 = torre1[valorTorre(torre1)];
aux2 = torre3[destinoTorre(torre3)+1] ;
aux3 = torre3[valorTorre(torre3)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre1[valorTorre(torre1)] = 0;
torre3[destinoTorre(torre3)] = aux1;
cont++;
}
}
}
}
// ORIGEM 2 - TORRE 2
if(orig == 2){
// DESTINO 1 - TORRE 1
if(dest == 1){
aux1 = torre2[valorTorre(torre2)];
aux2 = torre1[destinoTorre(torre1)+1] ;
aux3 = torre1[valorTorre(torre1)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre2[valorTorre(torre2)] = 0;
torre1[destinoTorre(torre1)] = aux1;
cont++;
}
}
// DESTINO 3 - TORRE 3
}else{
aux1 = torre2[valorTorre(torre2)];
aux2 = torre3[destinoTorre(torre3)+1] ;
aux3 = torre3[valorTorre(torre3)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre2[valorTorre(torre2)] = 0;
torre3[destinoTorre(torre3)] = aux1;
cont++;
}
}
}
}
// ORIGEM 3 - TORRE 3
if(orig == 3){
// DESTINO 1 - TORRE 1
if(dest == 1){
aux1 = torre3[valorTorre(torre3)];
aux2 = torre1[destinoTorre(torre1)+1] ;
aux3 = torre1[valorTorre(torre1)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre3[valorTorre(torre3)] = 0;
torre1[destinoTorre(torre1)] = aux1;
cont++;
}
}
// DESTINO 2 - TORRE 2
}else{
aux1 = torre3[valorTorre(torre3)];
aux2 = torre2[destinoTorre(torre2)+1] ;
aux3 = torre2[valorTorre(torre2)];
if(aux3 < aux1 && aux3 != 0){
printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
system("PAUSE");
}else{
if(aux1 == 0){
printf("Sem discos na torre de origem.\n");
system("PAUSE");
}else{
torre3[valorTorre(torre3)] = 0;
torre2[destinoTorre(torre2)] = aux1;
cont++;
}
}
}
}
mostrarTorres();
}
void mostrarMenu(){
int orig, dest, test=0, aux;
do{
// VERIFICA SE GANHOU!
if(verificarFinal() == 1){
aux = pow(2, POSICOES)-1;
if(cont == aux){
printf("\nParabéns, você ganhou e sua pontuação foi excelente %d de %d.\n", cont, aux);
}else if(cont > aux && cont <aux+5){
printf("\nParabéns, você ganhou mas sua pontuação foi abaixo da média %d de%d.\n", cont, aux);
}else{
printf("\nParabéns, você ganhou mas sua pontuação foi ruim: %d de %d.\n", cont, aux);
}
system("PAUSE");
system("CLS");
printf("\n\n\t\t\t ENCERRADO COM SUCESSO.\n");
exit(0); // FINALIZA O PROGRAMA;
}
printf("Informe Torre Origem (1 a 3): ");
scanf("%d", &orig);
do{
if(orig < 1 || orig > 3){
printf(" - Torre de Origem não corresponde, informe corretamente.\nInforme Torre Origem (1 a 3): ");
scanf("%d", &orig);
}
}while(orig < 1 || orig > 3);
mostrarTorres();
printf("Informe Torre Destino (1 a 3): \t");
scanf("%d", &dest);
do{
if(dest < 1 || dest > 3){
printf(" - Torre de Destino não corresponde, informe corretamente.\nInforme Torre Destino (1 a 3): ");
scanf("%d", &dest);
}
if(dest == orig){
printf(" - O destino não pode ser igual a torre de origem.\nInforme Torre Destino (1 a 3): ");
scanf("%d", &dest);
}
}while(dest < 1 || dest > 3 || dest == orig);
moverDiscos(orig, dest);
}while(test != 1);
}
void mostrarTorres(){
system("CLS");
printf("\t TORRE DE HANOI \n");
for(int i=0;i<POSICOES;i++){
printf(" \t |%d| |%d| |%d| \n", torre1[i], torre2[i], torre3[i]);
//printf(" \t%d|%d| %d|%d| %d|%d| \n", i, torre1[i], i, torre2[i], i, torre3[i]);// PARA TESTES
}
printf("\t Nº de jogadas: ");
if(cont < 10){
printf("0%d\n", cont);
}else{
printf("%d\n", cont);
}
}
int verificarFinal(){
int opt=0;
if(torre3[0] == 1){
opt = 1;
}
return(opt);
}
void regras(){
system("CLS");
printf(" - Torre de Hanói:\n");
printf(" O objectivo deste jogo consiste em deslocar todos os discos da haste\n");
printf(" Onde se encontram para uma haste diferente, \n Respeitando as seguintes regras:\n\n");
printf(" 1 - deslocar um disco de cada vez, o qual deverá\n ser o do topo de uma das três hastes.\n\n");
printf(" 2 - cada disco nunca poderá ser colocado sobre\n outro de diâmetro mais pequeno.\n\n ");
system("pause");
system("CLS");
}Discussão (0)
Carregando comentários...