#ident "$Id: solution.c,v 1.4 2005/01/28 21:29:49 pwh Rel $"

/*
 * Tower of Hanoi Puzzle solution logic.
 */

#include	"hanoi.h"


static int buildTower ( PUZZLE *puzzle, int height, int dest_peg )

{
   int	status = 1;

   if ( height-- > 0 ) {

	int	src_peg = FIND_DISK ( puzzle, height );

	if ( src_peg != dest_peg ) {

		status = ( buildTower ( puzzle, height,
					OTHER_PEG ( src_peg, dest_peg ) )
				&& moveDisk ( puzzle, src_peg, dest_peg ) );
	}

	if ( status ) status = buildTower ( puzzle, height, dest_peg );
   }

   return ( status );
}


int solve ( PUZZLE *puzzle )

{
   return ( buildTower ( puzzle, puzzle->height, RIGHT_PEG ) );
}


Last updated: Saturday, August 01, 2009 10:49:29 PM