// Towers of Hanoi 
// Written by FL/Raisonance

#include <stdio.h>


// #define NOPRINT //defined to speed measurement, not defined for demo
#define SIZE 4                  // number of disks to move

void DrawPosition(void);        // for demo purpose
void ComplexMove(int dest, int src, int size) reentrant;
void HardMove(int dest, int src);       // move a top disk to dest location from src

int PosArray[3][SIZE];          // = { { 4, 3, 2, 1 },{0, 0, 0, 0},{0, 0, 0, 0}};
        // 3 maste and the four disks are all set on the left stack (number 0)

void ComplexMove(int dest, int src, int size) reentrant
{
   int ThirdLocation = ~(dest + src) & 3;
   if(size > 1)
      ComplexMove(ThirdLocation, src, size - 1);
   HardMove(dest, src);
   if(size > 1)
      ComplexMove(dest, ThirdLocation, size - 1);
}

void HardMove(int dest, int src)
{
   int disk;
   int i;

   // first find the disk at the top of the src stack
   for(i = 0; i < SIZE; i++)
   {
      disk = PosArray[src][(SIZE - 1) - i];
      if(disk)
      {
         PosArray[src][(SIZE - 1) - i] = 0;     // remove the disk
         break;
      }
   }
   // then put it onto the dest stack
   for(i = SIZE - 1; i >= 0; i--)
   {
      if(PosArray[dest][i - 1] || (i == 0))     // a floor exists
      {
         PosArray[dest][i] = disk;      // put the new over it
         break;
      }
   }
#ifndef NOPRINT
   DrawPosition();
#endif
}

void Repeat(char c, unsigned char n)
{
   char k;
   for(k = 0; k < n; k++)
      putchar(c);
}

void DrawPosition(void)
{
   int j, size, n, i;

   // draw first the top of the towers
   putchar('\n');
   for(i = 0; i < 3; i++)
   {
      Repeat(' ', SIZE + 1);
      putchar('|');
      Repeat(' ', SIZE);
   }
   putchar('\n');
   for(i = SIZE - 1; i >= 0; i--)
   {
      putchar(' ');
      for(j = 0; j < 3; j++)
      {
         size = PosArray[j][i];
         n = SIZE - size;
         Repeat(' ', n);        // Space filling for small disks
         Repeat('#', size);     // Left side of the disk
         putchar('|');          // Central maste
         Repeat('#', size);     // Right side of the disk
         Repeat(' ', n);        // Space filling for small disks
         putchar(' ');          // Space interval between disks
      }
      putchar('\n');
   }
   for(i = 0; i < 3; i++)
   {
      putchar(' ');
      Repeat('=', 2 * SIZE + 1);
   }
}

void main(void)
{
   int i;

   // Stacks initialization
   for(i = 0; i < SIZE; i++)
   {
      PosArray[0][i] = (SIZE - i);      // Left column contains all the disks
      PosArray[1][i] =          // Central column does not contain any disk
         PosArray[2][i] = 0;    // Right column does not contain any disk
   }

   printf("\nNow running Hanoi towers algorithm");
   // First we initialize the stack on the left (0) maste
#ifndef NOPRINT
   DrawPosition();
#endif
   // move all 4 disks from left(0) to right(2)
   ComplexMove(2, 0, SIZE);     // move the SIZE disks from the maste 0 to the Maste 2
   printf("\nMove is done !");
   while(1);
}
