Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct {
  5. int* pegA;
  6. int* pegB;
  7. int* pegC;
  8. unsigned int blocks;
  9. } state;
  10.  
  11. void initState();
  12. void printState();
  13. int hasWon();
  14. void play();
  15.  
  16. int main() {
  17. printf("Towers of Hanoi\t~By Arc\n\n");
  18. printf("Enter number of layers to play with:\t");
  19. scanf("%d", &state.blocks);
  20. initState();
  21. while(!hasWon()) {
  22. system("clear||cls");
  23. printf("Towers of Hanoi\t~By Arc\n\n");
  24. printState();
  25. play();
  26. }
  27. system("clear||cls");
  28. printf("Towers of Hanoi\t~By Arc\n\n");
  29. printState();
  30. printf("Congratulations! You solved the puzzle!");
  31. //clean
  32. free(state.pegA);
  33. free(state.pegB);
  34. free(state.pegC);
  35. return 0;
  36. }
  37.  
  38. void initState() {
  39. int i;
  40. state.pegA = (int*)calloc(state.blocks, sizeof(int));
  41. state.pegB = (int*)calloc(state.blocks, sizeof(int));
  42. state.pegC = (int*)calloc(state.blocks, sizeof(int));
  43. //populate pegA
  44. for(i = 0; (unsigned int)i < state.blocks; i++) {
  45. *(state.pegA + i) = i + 1;
  46. }
  47. }
  48. void printState() {
  49. int i;
  50. for(i = 0; (unsigned int)i < state.blocks; i++) {
  51. printf("%d\t%d\t%d\n", *(state.pegA + i), *(state.pegB + i), *(state.pegC + i));
  52. }
  53. printf("Peg A Peg B Peg C\n\n");
  54. }
  55. int hasWon() {
  56. int i;
  57. //check in pegB
  58. for(i = 0; (unsigned int)i < state.blocks; i++) {
  59. if (*(state.pegB + i) != i + 1) break;
  60. }
  61. if ((unsigned int)i == state.blocks) {
  62. return 1;
  63. }
  64. //check in pegC
  65. for(i = 0; (unsigned int)i < state.blocks; i++) {
  66. if (*(state.pegC + i) != i + 1) break;
  67. }
  68. if ((unsigned int)i == state.blocks) {
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. void play() {
  74. int ch;
  75. int source, target;
  76. int *sourcePeg, *targetPeg;
  77. int i;
  78. int block = 0;
  79. int blockIndex = 0;
  80. //clean input stream
  81. while((ch = getchar()) != '\n' && ch != EOF);
  82. printf("Select a peg to move a block from (A/B/C):\nPeg ");
  83. source = getchar();
  84. if(source != 'A' && source != 'B' && source != 'C' && source != 'a' && source != 'b' && source != 'c') {
  85. printf("Invalid input. Please try again.\n");
  86. play();
  87. }
  88. //clean input stream
  89. while((ch = getchar()) != '\n' && ch != EOF);
  90. printf("Select a peg to move the block to (A/B/C):\nPeg ");
  91. target = getchar();
  92. if(target != 'A' && target != 'B' && target != 'C' && target != 'a' && target != 'b' && target != 'c') {
  93. printf("Invalid input. Please try again.\n");
  94. play();
  95. }
  96. else if (source == target) {
  97. printf("You cannot move to the same peg!\n");
  98. play();
  99. }
  100. if (source == 'A' || source == 'a') sourcePeg = state.pegA;
  101. else if (source == 'B' || source == 'b') sourcePeg = state.pegB;
  102. else sourcePeg = state.pegC;
  103. if (target == 'A' || target == 'a') targetPeg = state.pegA;
  104. else if (target == 'B' || target == 'b') targetPeg = state.pegB;
  105. else targetPeg = state.pegC;
  106.  
  107. //remove from source
  108. for(i = 0; (unsigned int)i < state.blocks; i++) {
  109. if (*(sourcePeg + i) != 0) {
  110. block = *(sourcePeg + i);
  111. //*(sourcePeg + i) = 0;
  112. blockIndex = i;
  113. break;
  114. }
  115. }
  116. if (block == 0) {
  117. printf("Peg %c has no blocks!\n", source);
  118. play();
  119. }
  120. //stack on target
  121. if (*(targetPeg + state.blocks - 1) == 0) {
  122. *(targetPeg + state.blocks - 1) = block;
  123. *(sourcePeg + blockIndex) = 0;
  124. }
  125. else for(i = 0; (unsigned int)i < state.blocks; i++) {
  126. if(*(targetPeg + i) != 0 && block < *(targetPeg + i)) {
  127. *(targetPeg + i - 1) = block;
  128. //remove from source
  129. *(sourcePeg + blockIndex) = 0;
  130. break;
  131. }
  132. else if (*(targetPeg + i) != 0 && block > *(targetPeg + i)) {
  133. printf("Cannot place a bigger block on top of a smaller block!\n");
  134. play();
  135. }
  136. }
  137. //clean
  138. sourcePeg = NULL;
  139. targetPeg = NULL;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement