I have written a code to solve the Towers of Hanoi puzzle, but need help formatting an output to print a 2d representation of each move. I know I need to use stacks to solve this problem, but I am not sure how to do so. Can anyone help me come up with a solution to this problem?
Project Info:
"In this project, you will pass the number of disks (no more than 15) as argument, then display the step by step moves strictly following the specification below.
Each rod should be display with 31 spaces in width, then with 5 spaces after each rod. The rod should be displayed using | in the middle, the height should be same as the number of disks. Each disk should be placed on the rod, with correct width to reflect the size of the disk. The smallest disk should be shown as two dashes (one on each side of the rod), the second smallest disk shall be displayed as 4 dashes (2 on each side of the rod), etc. The possible largest disks should be show as 15 dashes on each side of the rod. When a disk is moved from one rod to another rod, the size shall stay same.
For example, if there are 5 disks, they should be shown as this on the rod:
@@@@@@@@@@@@@@_|_@@@@@@@@@@@@@@&&&&&
@@@@@@@@@@@@@__|__@@@@@@@@@@@@@&&&&&
@@@@@@@@@@@@___|___@@@@@@@@@@@@&&&&&
@@@@@@@@@@@____|____@@@@@@@@@@@&&&&&
@@@@@@@@@@_____|_____@@@@@@@@@@&&&&&
1
- 5 bars (|) represent the rod _ represents the disk.
- @ and & represent the spaces.
- @ represent the space before and after the disk. If the disk is the largest one, there will be no @ on either end.
- & represents the space after the rods.
- The number under the rod is the rod number: 1, 2 or 3."
My Code:
#include <stdio.h>
#include <stdlib.h>
int step = 1; //Global variable
void solver(int size, char fromRod, char toRod, char extraRod); //Prototyping functionx
int main(int argc, char *argv[]) { //Main function
int size, step = 1, i, j, k, width = 1; //Declaring variables
if(argc <= 1) { //Displays error if there is a missing line on the command
printf("Missing command line argument.\n");
printf("Usage: ./hanoi numberOfDisks\n");
exit(1);
}
size = atoi(argv[1]); //Finding size of puzzle from user input
if (size > 15 || size < 2) { //If the size is too big or too small displays an error
printf("Invalid input. Size should be between 1 and 15.\n");
return -1;
}
char setA[size + 1][31], setB[size + 1][31], setC[size + 1][31]; //Tower number only on last row
solver(size, '1', '3', '2'); //Calls solver function
return 0;
}
void solver(int size, char fromRod, char toRod, char extraRod) { //Solver function
if (size == 1) { //If size is 1
printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod); //Prints step number and instructions
return;
}
solver(size - 1, fromRod, extraRod, toRod); //Recursive statement
step++; //Increments the number of steps
printf("%d: Move top disk from %c --> %c\n", step, fromRod, toRod);
step++;
solver(size - 1, extraRod, toRod, fromRod);
}
Output Example Using a Size of 3:
| | |
__|__ | |
___|___ _|_ |
1 2 3
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2: move top disk from 1 --> 3
Also, I have the step by step instructions properly outputting via the solver function. That is all set. The formatted output is the big issue here.