Wizualizacja wieży hanoi

0

Witam,

Sprawa wygląda tak, że muszę zrobić Wizualizację wieży hanoi, mam kod, ale coś jest nie tak. Możecie rzucić okiem?
Przepraszam za komentarze po angielsku, ale studiuję na kierunku Makro, więc wszystko muszę mieć po angielsku...

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>

/* Comment out to disable */
#if defined(WIN32) || defined(WIN64)
#undef COLORED
#else
#define COLORED
#endif
#define ANIMATED


/* N will hold the number of disks */
int N, delay;

/* declare a structure for each column */
/* each column is actually a LIFO stack */
typedef struct column
{
  int top;                      /* top holds the stack's top index */
  int *stack;                   /* stack is a dynamically allocated array which
                                   is used as the stack */
} column_t;

/* This function prints the board 
 * want work well for N>9 */
void
printBoard (column_t board[3])
{

  int i, j, k, n, num_of_digits, width, disk, mod, div;

  /* calculate the width of the largest disk and add some extra space */
  int maxwidth = (2 * (N - 1)) + 4;

  /* print the first empty line for all three columns */
  for (i = 0; i < 3; ++i)
    {
      for (k = 0; k < (maxwidth / 2); ++k)
        printf (" ");

      printf ("|");

      for (k = 0; k < (maxwidth / 2); ++k)
        printf (" ");
    }
  printf ("\n");


  /* now print the actual columns */
  for (i = N - 1; i >= 0; --i)
    {                           /* for each stack level */
      for (j = 0; j < 3; ++j)
        {                       /* for each column/stack */
          disk = board[j].stack[i];
          /* calculate the disk's width */
          width = (2 * (disk - 1)) + 1;

          if (disk)
            {                   /* if there is a disk at this level of the stack */
              for (k = 0; k < (((maxwidth - width) + 1) / 2); ++k)
                printf (" ");

              /* calculate the numbers width */
              n = disk;
              num_of_digits = 0;
              while (n > 0)
                {
                  n /= 10;
                  ++num_of_digits;
                }

#ifdef COLORED
              /* change color */
              printf ("\033[1;%dm", 31 + disk % 6);
#endif /* COLORED */
              div = width / num_of_digits;
              for (k = 0; k < div; ++k)
                printf ("%d", disk);

              mod = width % num_of_digits;
              /* if there are more digits to print */
              if (mod)
                printf ("%d", disk / (int) pow (10, num_of_digits - mod));

#ifdef COLORED
              /* reset color */
              printf ("\033[0m");
#endif /* COLORED */

              for (k = 0; k < (((maxwidth - width) + 1) / 2); ++k)
                printf (" ");
            }
          else
            {                   /* if there is no disk at this level of the stack */
              for (k = 0; k < (maxwidth / 2); ++k)
                printf (" ");

              printf ("|");

              for (k = 0; k < (maxwidth / 2); ++k)
                printf (" ");

            }

        }
      printf ("\n");
    }

  /* print the bottom line */
  for (i = 0; i < (maxwidth + 1) * 3; ++i)
    printf ("-");

  printf ("\n");

#ifdef ANIMATED
  /* set the delay between the printings */
  usleep (delay);
#endif /* ANIMATED */
}

/* moves 'disk' from 'from' to 'to' in 'board' */
void
move (column_t * board, int disk, int from, int to)
{

  /* remove the disk from the 'from' column */
  board[from].stack[--board[from].top] = 0;
  assert (board[from].top >= 0);

  /* move the disk to the 'to' column */
  board[to].stack[board[to].top++] = disk;

#ifdef ANIMATED
  /* clear the terminal for animated solving */
#if defined(WIN32) || defined(WIN64)
  system ("CLS");
#else 
   system("clear");  
  printf ("\033[%dA", N + 4);

#endif /* WIN32 || WIN64 */
#endif /* ANIMATED */

  /* print the move and then the board */
  printf ("\t%c-->%c\n\n", 'A' + from, 'A' + to);
  printBoard (board);

  return;
}

void
hanoi (column_t * board, int disk, int from, int to)
{
  int tmp;


  if (--disk == 0)
    return;
  
  tmp = 3 - (to + from);

  hanoi (board, disk, from, tmp);

  move (board, disk, from, to);

  hanoi (board, disk, tmp, to);

  return;
}

int
main (int argc, char **argv)
{

  int i;
  column_t board[3];

  if (argc != 3)
    {
      printf ("\n\Usage:%s <number of disks> <delay in milliseconds>\n\
        Example:%s 5 100\n", argv[0], argv[0]);
      return 1;
    }

  N = atoi (argv[1]);
  delay = atoi (argv[2]) * 1000;

  if ( (N*delay) < 0 ) {
	printf("Please enter positive numbers!");
	return 1;
  }

  board[0].top = N;
  board[0].stack = (int *) malloc (N * sizeof (int));

  /* initially place all disks at first column */
  for (i = 0; i < N; ++i)
    board[0].stack[i] = N - i;

  for (i = 1; i < 3; ++i)
    {
      board[i].top = 0;
      board[i].stack = (int *) malloc (N * sizeof (int));
    }

  printf ("\n\n");
  printBoard (board);
  hanoi (board, N + 1, 0, 2);
 
  for (i = 0; i < 3; ++i)
    free (board[i].stack);

  return 0;
}
 

Dzięki wielkie z góry! :)

1

ale coś jest nie tak

Za każdym razem, gdy mówisz, że coś jest nie tak, jeden programista na świecie umiera.
Co jest nie tak?
Nie kompiluje się, źle działa?

0

Wybacz, źle się wyraziłem. Nie kompiluję się.

0

@up?

1 użytkowników online, w tym zalogowanych: 0, gości: 1