Advertisement
semkname

Untitled

Mar 2nd, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h> // POW(); CALCULAR O VALOR DE MOVIMENTOS EXATOS DA TORRE DE HANOI;
  4. #define POSICOES 5
  5.  
  6. int torre1[5]= {1,2,3,4,5}, torre2[5]={0,0,0,0,0}, torre3[5] ={0,0,0,0,0}; // ARRAYS;
  7.  
  8. int cont=0; // VARIAVEL PARA CONTAR O NUMERO DE JOGADAS;
  9.  
  10. void mostrarTorres(); // PROTOTIPO DA FUNÇÃO PARA MOSTRA O ESTADO ATUAL DA TORRE;
  11.  
  12. int valorTorre(int torre[5]); // PROTOTIPO DA FUNÇÃO PARA PEGAR O MENOR VALOR;
  13.  
  14. int destinoTorre(int torre[5]); // PROTOTIPO DA FUNÇÃO PARA PEGAR O MENOR VALOR;
  15.  
  16. void moverDiscos(int orig, int dest); // PROTOTIPO DA FUNÇÃO PARA MOVER DISCOS;
  17.  
  18. void mostrarMenu(); // PROTOTIPO DA FUNÇÃO PARA MOSTRAR O MENU;
  19.  
  20. int verificarFinal(); // PROTOTIPO DA FUNÇÃO QUE VERIFICA SE O JOGADOR FOI CAMPEÃO;
  21.  
  22. void regras(); // PROTOTIPO DA FUNÇÃO QUE MOSTRA AS REGRAS DO JOGO;
  23.  
  24. int main(void){
  25. int op=1;
  26. while(op!=0){
  27. printf(" - TORRE DE HANOI \n 2 - JOGAR\n 1 - REGRAS\n 0 - SAIR\n - DIGITE: ");
  28. scanf("%d", &op);
  29. switch(op){
  30. case 2:
  31. mostrarTorres();
  32. mostrarMenu();
  33. break;
  34. case 1:
  35. regras();
  36. break;
  37. default:
  38. if(op != 0){
  39. printf(" - OPÇÃO INVALIDA INFORME NOVAMENTE\n - ");
  40. system("PAUSE");
  41. system("CLS");
  42. }else{
  43. system("CLS");
  44. printf("\n\n\t\t\t ENCERRADO COM SUCESSO.\n");
  45. }
  46. break;
  47. }
  48. }
  49. return 0;
  50. }
  51.  
  52.  
  53. int valorTorre(int torre[]){ // FUNÇÃO PARA ULTIMO VALOR DO ARRAY
  54. int i, a=0;
  55. for(i=POSICOES-1; i>=0; i--){
  56. if(torre[i] != 0){
  57. a = i;
  58. }
  59. if(torre[4] == 0){
  60. a = 0;
  61. }
  62. }
  63. return(a);
  64. }
  65.  
  66.  
  67. int destinoTorre(int torre[]){ // FUNÇÃO PARA RETORNA PROXIMO VALOR DE DESTINO
  68. int i, a=0;
  69. for(i=0; i<POSICOES; i++){
  70. if(torre[i] == 0){
  71. a = i;
  72. }
  73. }
  74. return(a);
  75. }
  76.  
  77.  
  78. void moverDiscos(int orig, int dest){ // FUNÇÃO PARA MOVER OS DISCOS
  79. int aux1, aux2, aux3;
  80. // 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);
  81. // ORIGEM 1 - TORRE 1
  82. if(orig == 1){
  83. // DESTINO 2 - TORRE 2
  84. if(dest == 2){
  85. aux1 = torre1[valorTorre(torre1)];
  86. aux2 = torre2[destinoTorre(torre2)+1] ;
  87. aux3 = torre2[valorTorre(torre2)];
  88. if(aux3 < aux1 && aux3 != 0){
  89. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  90. system("PAUSE");
  91. }else{
  92. if(aux1 == 0){
  93. printf("Sem discos na torre de origem.\n");
  94. system("PAUSE");
  95. }else{
  96. torre1[valorTorre(torre1)] = 0;
  97. torre2[destinoTorre(torre2)] = aux1;
  98. cont++;
  99. }
  100. }
  101. // DESTINO 3 - TORRE 3
  102. }else{
  103. aux1 = torre1[valorTorre(torre1)];
  104. aux2 = torre3[destinoTorre(torre3)+1] ;
  105. aux3 = torre3[valorTorre(torre3)];
  106. if(aux3 < aux1 && aux3 != 0){
  107. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  108. system("PAUSE");
  109. }else{
  110. if(aux1 == 0){
  111. printf("Sem discos na torre de origem.\n");
  112. system("PAUSE");
  113. }else{
  114. torre1[valorTorre(torre1)] = 0;
  115. torre3[destinoTorre(torre3)] = aux1;
  116. cont++;
  117. }
  118. }
  119. }
  120. }
  121.  
  122. // ORIGEM 2 - TORRE 2
  123. if(orig == 2){
  124. // DESTINO 1 - TORRE 1
  125. if(dest == 1){
  126. aux1 = torre2[valorTorre(torre2)];
  127. aux2 = torre1[destinoTorre(torre1)+1] ;
  128. aux3 = torre1[valorTorre(torre1)];
  129. if(aux3 < aux1 && aux3 != 0){
  130. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  131. system("PAUSE");
  132. }else{
  133. if(aux1 == 0){
  134. printf("Sem discos na torre de origem.\n");
  135. system("PAUSE");
  136. }else{
  137. torre2[valorTorre(torre2)] = 0;
  138. torre1[destinoTorre(torre1)] = aux1;
  139. cont++;
  140. }
  141. }
  142. // DESTINO 3 - TORRE 3
  143. }else{
  144. aux1 = torre2[valorTorre(torre2)];
  145. aux2 = torre3[destinoTorre(torre3)+1] ;
  146. aux3 = torre3[valorTorre(torre3)];
  147. if(aux3 < aux1 && aux3 != 0){
  148. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  149. system("PAUSE");
  150. }else{
  151. if(aux1 == 0){
  152. printf("Sem discos na torre de origem.\n");
  153. system("PAUSE");
  154. }else{
  155. torre2[valorTorre(torre2)] = 0;
  156. torre3[destinoTorre(torre3)] = aux1;
  157. cont++;
  158. }
  159. }
  160. }
  161. }
  162.  
  163. // ORIGEM 3 - TORRE 3
  164. if(orig == 3){
  165. // DESTINO 1 - TORRE 1
  166. if(dest == 1){
  167. aux1 = torre3[valorTorre(torre3)];
  168. aux2 = torre1[destinoTorre(torre1)+1] ;
  169. aux3 = torre1[valorTorre(torre1)];
  170. if(aux3 < aux1 && aux3 != 0){
  171. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  172. system("PAUSE");
  173. }else{
  174. if(aux1 == 0){
  175. printf("Sem discos na torre de origem.\n");
  176. system("PAUSE");
  177. }else{
  178. torre3[valorTorre(torre3)] = 0;
  179. torre1[destinoTorre(torre1)] = aux1;
  180. cont++;
  181. }
  182. }
  183. // DESTINO 2 - TORRE 2
  184. }else{
  185. aux1 = torre3[valorTorre(torre3)];
  186. aux2 = torre2[destinoTorre(torre2)+1] ;
  187. aux3 = torre2[valorTorre(torre2)];
  188. if(aux3 < aux1 && aux3 != 0){
  189. printf("Disco da origem [%d] é maior que o disco de destino [%d]\n", aux1, aux3);
  190. system("PAUSE");
  191. }else{
  192. if(aux1 == 0){
  193. printf("Sem discos na torre de origem.\n");
  194. system("PAUSE");
  195. }else{
  196. torre3[valorTorre(torre3)] = 0;
  197. torre2[destinoTorre(torre2)] = aux1;
  198. cont++;
  199. }
  200. }
  201. }
  202. }
  203. mostrarTorres();
  204. }
  205.  
  206.  
  207. void mostrarMenu(){
  208. int orig, dest, test=0, aux;
  209. do{
  210. // VERIFICA SE GANHOU!
  211. if(verificarFinal() == 1){
  212. aux = pow(2, POSICOES)-1;
  213. if(cont == aux){
  214. printf("\nParabéns, você ganhou e sua pontuação foi excelente %d de %d.\n", cont, aux);
  215. }else if(cont > aux && cont <aux+5){
  216. printf("\nParabéns, você ganhou mas sua pontuação foi abaixo da média %d de%d.\n", cont, aux);
  217. }else{
  218. printf("\nParabéns, você ganhou mas sua pontuação foi ruim: %d de %d.\n", cont, aux);
  219. }
  220. system("PAUSE");
  221. system("CLS");
  222. printf("\n\n\t\t\t ENCERRADO COM SUCESSO.\n");
  223. exit(0); // FINALIZA O PROGRAMA;
  224. }
  225. printf("Informe Torre Origem (1 a 3): ");
  226. scanf("%d", &orig);
  227. do{
  228. if(orig < 1 || orig > 3){
  229. printf(" - Torre de Origem não corresponde, informe corretamente.\nInforme Torre Origem (1 a 3): ");
  230. scanf("%d", &orig);
  231. }
  232. }while(orig < 1 || orig > 3);
  233. mostrarTorres();
  234. printf("Informe Torre Destino (1 a 3): \t");
  235. scanf("%d", &dest);
  236. do{
  237. if(dest < 1 || dest > 3){
  238. printf(" - Torre de Destino não corresponde, informe corretamente.\nInforme Torre Destino (1 a 3): ");
  239. scanf("%d", &dest);
  240. }
  241. if(dest == orig){
  242. printf(" - O destino não pode ser igual a torre de origem.\nInforme Torre Destino (1 a 3): ");
  243. scanf("%d", &dest);
  244. }
  245. }while(dest < 1 || dest > 3 || dest == orig);
  246. moverDiscos(orig, dest);
  247. }while(test != 1);
  248. }
  249.  
  250.  
  251. void mostrarTorres(){
  252. system("CLS");
  253. printf("\t TORRE DE HANOI \n");
  254. for(int i=0;i<POSICOES;i++){
  255. printf(" \t |%d| |%d| |%d| \n", torre1[i], torre2[i], torre3[i]);
  256. //printf(" \t%d|%d| %d|%d| %d|%d| \n", i, torre1[i], i, torre2[i], i, torre3[i]);// PARA TESTES
  257. }
  258. printf("\t Nº de jogadas: ");
  259. if(cont < 10){
  260. printf("0%d\n", cont);
  261. }else{
  262. printf("%d\n", cont);
  263. }
  264. }
  265.  
  266.  
  267. int verificarFinal(){
  268. int opt=0;
  269. if(torre3[0] == 1){
  270. opt = 1;
  271. }
  272. return(opt);
  273. }
  274.  
  275.  
  276. void regras(){
  277. system("CLS");
  278. printf(" - Torre de Hanói:\n");
  279. printf(" O objectivo deste jogo consiste em deslocar todos os discos da haste\n");
  280. printf(" Onde se encontram para uma haste diferente, \n Respeitando as seguintes regras:\n\n");
  281. printf(" 1 - deslocar um disco de cada vez, o qual deverá\n ser o do topo de uma das três hastes.\n\n");
  282. printf(" 2 - cada disco nunca poderá ser colocado sobre\n outro de diâmetro mais pequeno.\n\n ");
  283. system("pause");
  284. system("CLS");
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement