#ident "$Id: hanoi.h,v 1.6 2006/05/29 23:34:51 pwh Rel $"

/*
 * Hanoi puzzle object.
 */

#if	!defined ( _HANOI_PUZZLE_H_ )
#define	_HANOI_PUZZLE_H_ 

#include	<curses.h>	/* Use the ncurses library. */

#define		PUZZLE_TITLE	"Tower of Hanoi Puzzle"
#define		ESC		27

/* Animation speed settings. */
#define		WARP_SPEED	2
#define		XTREME_SPEED	10
#define		HIGH_SPEED	20
#define		NORMAL_SPEED	50
#define		SLOW_SPEED	100


/* Disk object. */
typedef struct {

	int	size;
	int	peg;
	int	height;
	int	x;
	int	y;

} DISK;


/* Puzzle object. */
typedef struct {

	int	height;		/* Height of tower. */
	int	y0;		/* Upper left corner of puzzle space. */
	int	x0;		/* Upper left corner of puzzle space. */
	int	xc;		/* Center column of screen. */
	int	xp[3];		/* Screen location of the pegs. */
	int	interactive;	/* Is user playing the game? */
	int	speed;		/* Animation speed. */
	int	newSpeed;	/* Set new speed. */
	int	warpFactor;	/* Animation speed. */
	int	moves;		/* Move count. */
	int	peg_tops [3];	/* Number of disks on each peg. */
	DISK	*disks;		/* Location of each disk. */
	DISK	**pegs [3];	/* Internal representaion of puzzle. */
	DISK	*xDisk;		/* Disk in transition. */

} PUZZLE;


/* Peg mnemonics. */
#define	LEFT_PEG	0
#define	CENTER_PEG	1
#define	RIGHT_PEG	2
#define	OTHER_PEG(peg1,peg2)	(3-(peg1)-(peg2))

/* Puzzle object constructor and destructor and configurator. */
PUZZLE	*openPuzzle ( int height, int animated, int speed );
void	closePuzzle ( PUZZLE *puzzle );
int	resetPuzzle ( PUZZLE *puzzle );
void	setSpeed ( PUZZLE *puzzle, int speed );

/* Interactive display and game play functions. */
int	playGame ( PUZZLE *puzzle );

/* Useful macros. */
#define	isAnimated(puzzle)	((puzzle)->speed)
#define	isSolved(puzl)		((puzl)->peg_tops[RIGHT_PEG]==(puzl)->height)
#define	FIND_DISK(puzzle,disk)	((puzzle)->disks[disk].peg)
#define	DISK_HEIGHT(puzl,disk)	((puzl)->disks[disk].height)
#define	TOP_DISK(puzl,p)	(((puzl)->xDisk&&(puzl)->xDisk->peg==(p))?\
				(puzl)->xDisk->size:((puzl)->peg_tops[p]>0?\
				(puzl)->pegs[p][(puzl)->peg_tops[p]-1]->size:\
				-1))

/* Puzzle manipulation functions. */
int	solvePuzzle ( PUZZLE *puzzle, int speed );
int	moveDisk ( PUZZLE *puzzle, int src, int dest );
int	pickupDisk ( PUZZLE *puzzle, int src );
int	slideDisk ( PUZZLE *puzzle, int dest );
int	dropDisk ( PUZZLE *puzzle, int dest );

/* Solution function. */
int	solve ( PUZZLE *puzzle );

#endif	/* !_HANOI_PUZZLE_H_ */


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