Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************
- * Tower of Hanoi Project
- * Main.c
- *
- * Verifies a list of moves for the Tower of Hanoi game.
- *
- * Written by Michael Moore
- */
- #include <stdio.h>
- // definitions
- #define NUM_PEGS 3
- #define NUM_DISKS 4
- #define STARTING_PEG 0
- #define NUM_MOVES 15
- // values of "size" of disks on each peg. Left is the bottom, right is the top
- int pegs[NUM_PEGS][NUM_DISKS];
- // number of disks on each peg
- int pegNumDisks[NUM_PEGS] =
- {
- 4,
- 0,
- 0
- };
- // moves the player makes. The disk moves from column 1 peg to column 2 peg
- int moves[NUM_MOVES][2] =
- {
- {0, 1}, // move disk from peg 0 to peg 1
- {0, 2},
- {1, 2},
- {0, 1},
- {2, 0},
- {2, 1},
- {0, 1},
- {0, 2},
- {1, 2},
- {1, 0},
- {2, 0},
- {1, 2},
- {0, 1},
- {0, 2},
- {1, 2}
- };
- // method definitions
- int GetPegDiskValue(int peg);
- void MoveDisk(int peg1, int peg2);
- void PrintPegs();
- void PrintMove(int move);
- // program entrance
- int main()
- {
- int i, peg1DiskValue, peg2DiskValue, solutionPeg;
- // make sure the starting peg is valid
- if (STARTING_PEG < 0 || STARTING_PEG >= NUM_PEGS)
- {
- printf("Invalid starting peg! The starting peg must be one the pegs.");
- getch();
- return 1;
- }
- // setup the initial layout of the disks, putting all the disks on the starting peg
- for (i = 0; i < NUM_DISKS; i++)
- pegs[STARTING_PEG][i] = NUM_DISKS - i;
- // print out the initial layout of the disks
- PrintPegs();
- // loop through all the moves
- for (i = 0; i < NUM_MOVES; i++)
- {
- // print out what the player does
- PrintMove(i);
- // check if the player is trying to move a disk to or from a non-existant peg
- if (moves[i][0] < 0 || moves[i][0] >= NUM_PEGS || moves[i][1] < 0 || moves[i][1] >= NUM_PEGS)
- {
- printf("Invalid move! Disks must stay on the pegs.");
- getch();
- return 1;
- }
- // check if the player is trying to move a disk to from a page to the same peg
- if (moves[i][0] == moves[i][1])
- {
- printf("Invalid move! Cannot move a disk to the same peg it is already on.");
- getch();
- return 1;
- }
- // get the "value" or size of the top disk on the two pegs the player is trying to move between
- peg1DiskValue = GetPegDiskValue(moves[i][0]);
- peg2DiskValue = GetPegDiskValue(moves[i][1]);
- // check if the player is trying to move a disk that is not there
- if (peg1DiskValue == 0)
- {
- printf("Invalid move! There is no disk on that peg.");
- getch();
- return 1;
- }
- // check if the player moved a larger disk onto a smaller one
- if (peg2DiskValue > 0 && peg1DiskValue > peg2DiskValue)
- {
- printf("Invalid move! Cannot move a larger disk on onto a smaller one.");
- getch();
- return 1;
- }
- // if the move is valid, apply changes to the peg array
- MoveDisk(moves[i][0], moves[i][1]);
- // print out the layout of the disks
- PrintPegs();
- }
- // if no errors there thrown, the list of moves was valid!
- printf("\nAll moves are valid!");
- // check if the puzzle was solved
- solutionPeg = CheckSolution();
- if (solutionPeg == -1)
- printf("\nPuzzle is not solved.");
- else
- {
- if (solutionPeg == STARTING_PEG)
- printf("\nPuzzle is not solved. You cannot end on the same peg that you started on.");
- else
- printf("\nPuzzle is solved!");
- }
- getch();
- return 0;
- }
- // returns the "value" or size of the top disk on a peg
- int GetPegDiskValue(int peg)
- {
- // if there are no disks, return 0
- if (pegNumDisks[peg] == 0)
- return 0;
- // otherwise, return the top disk on the peg
- else
- return pegs[peg][pegNumDisks[peg] - 1];
- }
- // moves a disk from peg1 to peg2
- void MoveDisk(int peg1, int peg2)
- {
- // add the top disk from the first peg, to second peg
- pegs[peg2][pegNumDisks[peg2]] = pegs[peg1][pegNumDisks[peg1] - 1];
- pegNumDisks[peg2] ++;
- // take the top disk off the first peg
- pegs[peg1][pegNumDisks[peg1] - 1] = 0;
- pegNumDisks[peg1] --;
- }
- // checks if a solution is found and returns the peg that the solutions is on. Retruns -1 if no solution is found
- int CheckSolution()
- {
- int i;
- // loop through all the pegs
- for (i = 0; i < NUM_PEGS; i++)
- {
- // if all the disks are on this peg, return this peg number
- if (pegNumDisks[i] == NUM_DISKS)
- return i;
- // otherwise, if there is a disk, it is impossible for all the disks to be on one peg
- else if (pegNumDisks[i] > 0)
- return -1;
- }
- // no solution was found
- return -1;
- }
- // prints out the layout of the pegs
- void PrintPegs()
- {
- // loop through the pegs and print out the disks for each peg
- int i, j;
- for (i = 0; i < NUM_PEGS; i++)
- {
- printf("\n}");
- for (j = 0; j < NUM_DISKS; j++)
- {
- if (pegs[i][j] == 0)
- break;
- printf("%u", pegs[i][j]);
- }
- }
- printf("\n");
- }
- // prints out a move the player takes
- void PrintMove(int move)
- {
- printf("\n%u -> %u\n", moves[move][0], moves[move][1]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement