Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. /*****************************************************
  2.  * Tower of Hanoi Project
  3.  * Main.c
  4.  *
  5.  * Verifies a list of moves for the Tower of Hanoi game.
  6.  *
  7.  * Written by Michael Moore
  8.  */
  9.  
  10. #include <stdio.h>
  11.  
  12. // definitions
  13. #define NUM_PEGS        3
  14. #define NUM_DISKS       4
  15. #define STARTING_PEG    0
  16. #define NUM_MOVES       15
  17.  
  18. // values of "size" of disks on each peg. Left is the bottom, right is the top
  19. int pegs[NUM_PEGS][NUM_DISKS];
  20.  
  21. // number of disks on each peg
  22. int pegNumDisks[NUM_PEGS] =  
  23. {
  24.     4,
  25.     0,
  26.     0
  27. };
  28.  
  29. // moves the player makes. The disk moves from column 1 peg to column 2 peg
  30. int moves[NUM_MOVES][2] =
  31. {
  32.     {0, 1}, // move disk from peg 0 to peg 1
  33.     {0, 2},
  34.     {1, 2},
  35.     {0, 1},
  36.     {2, 0},
  37.     {2, 1},
  38.     {0, 1},
  39.     {0, 2},
  40.     {1, 2},
  41.     {1, 0},
  42.     {2, 0},
  43.     {1, 2},
  44.     {0, 1},
  45.     {0, 2},
  46.     {1, 2}
  47. };
  48.  
  49. // method definitions
  50. int GetPegDiskValue(int peg);
  51. void MoveDisk(int peg1, int peg2);
  52. void PrintPegs();
  53. void PrintMove(int move);
  54.  
  55. // program entrance
  56. int main()
  57. {
  58.     int i, peg1DiskValue, peg2DiskValue, solutionPeg;
  59.  
  60.     // make sure the starting peg is valid
  61.     if (STARTING_PEG < 0 || STARTING_PEG >= NUM_PEGS)
  62.     {
  63.         printf("Invalid starting peg! The starting peg must be one the pegs.");
  64.         getch();
  65.         return 1;
  66.     }
  67.  
  68.     // setup the initial layout of the disks, putting all the disks on the starting peg
  69.     for (i = 0; i < NUM_DISKS; i++)
  70.         pegs[STARTING_PEG][i] = NUM_DISKS - i;
  71.  
  72.     // print out the initial layout of the disks
  73.     PrintPegs();
  74.  
  75.     // loop through all the moves
  76.     for (i = 0; i < NUM_MOVES; i++)
  77.     {
  78.         // print out what the player does
  79.         PrintMove(i);
  80.  
  81.         // check if the player is trying to move a disk to or from a non-existant peg
  82.         if (moves[i][0] < 0 || moves[i][0] >= NUM_PEGS || moves[i][1] < 0 || moves[i][1] >= NUM_PEGS)
  83.         {
  84.             printf("Invalid move! Disks must stay on the pegs.");
  85.             getch();
  86.             return 1;
  87.         }
  88.  
  89.         // check if the player is trying to move a disk to from a page to the same peg
  90.         if (moves[i][0] == moves[i][1])
  91.         {
  92.             printf("Invalid move! Cannot move a disk to the same peg it is already on.");
  93.             getch();
  94.             return 1;
  95.         }
  96.  
  97.         // get the "value" or size of the top disk on the two pegs the player is trying to move between
  98.         peg1DiskValue = GetPegDiskValue(moves[i][0]);
  99.         peg2DiskValue = GetPegDiskValue(moves[i][1]);
  100.  
  101.         // check if the player is trying to move a disk that is not there
  102.         if (peg1DiskValue == 0)
  103.         {
  104.             printf("Invalid move! There is no disk on that peg.");
  105.             getch();
  106.             return 1;
  107.         }
  108.  
  109.         // check if the player moved a larger disk onto a smaller one
  110.         if (peg2DiskValue > 0 && peg1DiskValue > peg2DiskValue)
  111.         {
  112.             printf("Invalid move! Cannot move a larger disk on onto a smaller one.");
  113.             getch();
  114.             return 1;
  115.         }
  116.  
  117.         // if the move is valid, apply changes to the peg array
  118.         MoveDisk(moves[i][0], moves[i][1]);
  119.  
  120.         // print out the layout of the disks
  121.         PrintPegs();
  122.     }
  123.  
  124.     // if no errors there thrown, the list of moves was valid!
  125.     printf("\nAll moves are valid!");
  126.  
  127.     // check if the puzzle was solved
  128.     solutionPeg = CheckSolution();
  129.     if (solutionPeg == -1)
  130.         printf("\nPuzzle is not solved.");
  131.     else
  132.     {
  133.         if (solutionPeg == STARTING_PEG)
  134.             printf("\nPuzzle is not solved. You cannot end on the same peg that you started on.");
  135.         else
  136.             printf("\nPuzzle is solved!");
  137.     }
  138.  
  139.     getch();
  140.     return 0;
  141. }
  142.  
  143. // returns the "value" or size of the top disk on a peg
  144. int GetPegDiskValue(int peg)
  145. {
  146.     // if there are no disks, return 0
  147.     if (pegNumDisks[peg] == 0)
  148.         return 0;
  149.     // otherwise, return the top disk on the peg
  150.     else
  151.         return pegs[peg][pegNumDisks[peg] - 1];
  152. }
  153.  
  154. // moves a disk from peg1 to peg2
  155. void MoveDisk(int peg1, int peg2)
  156. {
  157.     // add the top disk from the first peg, to second peg
  158.     pegs[peg2][pegNumDisks[peg2]] = pegs[peg1][pegNumDisks[peg1] - 1];
  159.     pegNumDisks[peg2] ++;
  160.  
  161.     // take the top disk off the first peg
  162.     pegs[peg1][pegNumDisks[peg1] - 1] = 0;
  163.     pegNumDisks[peg1] --;
  164. }
  165.  
  166. // checks if a solution is found and returns the peg that the solutions is on. Retruns -1 if no solution is found
  167. int CheckSolution()
  168. {
  169.     int i;
  170.  
  171.     // loop through all the pegs
  172.     for (i = 0; i < NUM_PEGS; i++)
  173.     {
  174.         // if all the disks are on this peg, return this peg number
  175.         if (pegNumDisks[i] == NUM_DISKS)
  176.             return i;
  177.         // otherwise, if there is a disk, it is impossible for all the disks to be on one peg
  178.         else if (pegNumDisks[i] > 0)
  179.             return -1;
  180.     }
  181.  
  182.     // no solution was found
  183.     return -1;
  184. }
  185.  
  186. // prints out the layout of the pegs
  187. void PrintPegs()
  188. {
  189.     // loop through the pegs and print out the disks for each peg
  190.     int i, j;
  191.     for (i = 0; i < NUM_PEGS; i++)
  192.     {
  193.         printf("\n}");
  194.  
  195.         for (j = 0; j < NUM_DISKS; j++)
  196.         {
  197.             if (pegs[i][j] == 0)
  198.                 break;
  199.  
  200.             printf("%u", pegs[i][j]);
  201.         }
  202.     }
  203.  
  204.     printf("\n");
  205. }
  206.  
  207. // prints out a move the player takes
  208. void PrintMove(int move)
  209. {
  210.     printf("\n%u -> %u\n", moves[move][0], moves[move][1]);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement