/*	Recursive Towers of Hanoi program */

#include <pegclass.h>
#include "utility.h"
//#include <conio.h>
#include <iostream.h>

using namespace std;	// October 5, 2001

//--------------------------------------------------------------------------------
void MoveRing(PegClass &FromPeg, PegClass &ToPeg)
/*	Moves a ring from the FromPeg to the ToPeg
	Pre: FromPeg is not empty
	Post: ring has been moved                                 */
{
	int Size = FromPeg.RemoveRing();
	ToPeg.AddRing(Size);
}
//--------------------------------------------------------------------------------
void Hanoi(int NumRings, PegClass &SourcePeg, PegClass &DestPeg,
					PegClass &SparePeg)
/*	Moves NumRings rings from SourcePeg to DestPeg using SparePeg for
	temporary storage.                                                                                       */
{
	if (NumRings > 0) {
		// Move N–1 rings to the spare peg for storage
		Hanoi(NumRings-1, SourcePeg, SparePeg, DestPeg);
 
		// Move the single big ring over 
		MoveRing(SourcePeg, DestPeg); Pause(); SourcePeg.ShowAll();
 
		// Move the N–1 ring back from spare peg to destination 
		Hanoi(NumRings-1, SparePeg, DestPeg, SourcePeg);
	}
}
//--------------------------------------------------------------------------------
int main()
{
	PegClass Left(7), Middle(7), Right(7);

	const int NumRings = 3;

	// Fill Left peg with NumRings, starting with biggest
	int RingCount;
	for (RingCount = NumRings; RingCount >= 1; RingCount--)
		Left.AddRing(RingCount);

	Pause();
	Hanoi(NumRings, Left, Middle, Right);
	return(0);
}
