#include <iostream>

void hanoi( int from, int to, int temp, int count)
{
   if(count>1) {

      // Move all disks smaller than this one over to the spare.
      // So if disk size is 5, we move 4 disks to the spare. 
      // This leaves us with 1 disk on the source peg.
      // Note the placement of the params.
      // We are now using the destination peg as the spare peg. 
      // This causes each recursion to ping-pong the spare and dest pegs.

      hanoi(from, temp, to, count-1);


      // Move the remaining disk to the destination peg.
      std::cout << "Move disk "  << count << " from peg " 
                << from << " to peg " << to << std::endl;


      // Move the disks we just moved to the spare back over to the destination peg
      hanoi(temp, to, from, count-1);
   }
   else
   {
      // This is our standard termination case. 
      // We get to here when we are operating on the smallest possible disk.

      std::cout << "Move disk " << count << " from peg " 
                << from << " to peg " << to << std::endl;
   }
}


int hanoi()
{
 
   // Move 5 disks from peg 0 to peg 1 using peg 2 as a temporary.
   int count  = 5;
   int source = 0;
   int dest   = 1; 
   int spare  = 2; 

   std::cout << "source peg = " << source 
             << ", destination peg = " << dest
             << ", spare peg = " << spare 
             << ", #disks = " << count << std::endl;
   hanoi( source, dest, spare, count);

   return 0;
}
