/* File: towers.cpp
 * Program to solve the Towers of Hanoi puzzle recursively.
 *
 * Input:  numDisks, the number of disks to be moved
 * Output: a sequence of moves that solve the puzzle
 ***********************************************************/

#include <iostream>
using namespace std;

void Hanoi(int n, char source, char destination, char spare);

int main()
{
   const char BEGIN_TOWER  = 'A',    // the three towers
              MIDDLE_TOWER = 'B',
              END_TOWER    = 'C';
   int numDisks;                     // the number of disks to be moved

   cout << "This program solves the Hanoi Towers puzzle.\n\n";

   cout << "Enter the number of disks: ";
   cin >> numDisks;
   cout << "The solution for n = " << numDisks << endl;
   Hanoi (numDisks, BEGIN_TOWER, END_TOWER, MIDDLE_TOWER);
}  // end main


/* Hanoi is a recursive function to solve the Towers of Hanoi puzzle.
 *
 *  Receive: n,           the number of disks to be moved;
 *           source,      tower containing disks to move
 *           destination, tower where to move disks
 *           spare,       tower to store disks temporarily,
 *  Output:  A message describing the move
 *******************************************************************/

void Hanoi (int n, char source, char destination, char spare)
{
   if (n <= 1)                                   // base case
      cout << "Move Disk 1 from " << source
           << " to " << destination << endl;
   else
   {                                             // inductive step
      Hanoi (n-1, source, spare, destination);
      cout << "Move Disk " << n << " from " << source
	   << " to " << destination << endl;
      Hanoi (n-1, spare, destination, source);
   }  // end else
}  // end Hanoi
