2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
NJF
  • 21
  • 2
  • 1
    Welcome to SO! Interesting question. I'm curious, what is the purpose of `char setA[size + 1][31], setB[size + 1][31], setC[size + 1][31];`? Your current "solver" has essentially hard-coded the solution algorithm but doesn't really know how many disks are on each pole, which is an important step towards making an ASCII representation of each puzzle state. – ggorlen Dec 17 '18 at 06:03
  • Welcome to Stack Overflow. Please note that you can edit your own question, and you should normally do that rather than add a comment below, especially a comment which is not a response to someone else's comment/question. (Responses to comments might well be better handled by an edit than a comment, too.) I've done it for you this time. Please do it yourself in future. You could remove your comment; if you don't, the clean-up squad will likely do it, but it is better if they aren't troubled by it. – Jonathan Leffler Dec 17 '18 at 06:16
  • How are you planning to do the display? Is the curses library a sensible option? Or do you need to use some of the Windows API functions? I have code dating back to 1987 which uses the curses API and has slightly different format requirements — the gap between the poles varies with the number of disks, rather than being fixed; I use `=` instead of `-` (as claimed, or `_` as shown) to mark the disks, and the move information is above, not below the display, and the poles aren't numbered (I find people can count from 1 to 3 quite easily). But it requires careful tracking of which disk is where. – Jonathan Leffler Dec 17 '18 at 06:26

0 Answers0