Click here to Skip to main content
15,393,955 members
Articles / Desktop Programming / Win32
Posted 23 Oct 2008

Tagged as


30 bookmarked

Towers of Hanoi with True GUI using Win32 and C++

Rate me:
Please Sign up or sign in to vote.
4.43/5 (18 votes)
23 Oct 2008CPOL2 min read
Shows how to solve Towers of Hanoi puzzle visually using Win32 and C++


Towers of Hanoi is a puzzle game where there are three stands, each holds number of plates.
The idea here is that you have to move plates in the first stand to the third one, while moving plates you must consider:

  1. Size order, meaning when moving a plate you can't put it on a smaller one.
  2. Only one plate can be moved at each step.

Please note that I used the recursive method.


Formally, Towers of Hanoi recursive function was given by :

void Hanoi(int platesCount, int from, int dest, int by)
	if (platesCount==1)
			"Move the plate from %d to %d through %d"
			, from, dest, by);
		Hanoi(platesCount -1, from, by, dest);
		Hanoi(1, from, dest, by);
		Hanoi(platesCount -1, by, dest, from);

And here is a conceptual image :


Credits: MG from Wikipedia

Using the Program

When you launch the application, you will see the next screen: 

Image 2

Then you have to select whether to let the program step into the solution automatically or not.


Using the Code

While implementing the program, I faced a problem in separating steps since I used the recursive version, so I decided to keep the recursive way. But the call to Hanoi method will happen just first time when "next" or "solve" button is pressed. Inside the Hanoi method, I stored the states of the solution, meaning each time a call happens, the solution of the current step is stored in a list of integers. Since each step in the Hanoi method requires 4 parameters as mentioned above, 4 integers represent parameter is added to the list. Then each time SolveNextStep method is called, the program reads from the list 4 items that represent the state.

Available classes are as follows:

  1. HanoiDrawer
  2. PlateGUIInfo
  3. Tools

Anyway I'll describe methods in HanoiDrawer class that are responsible directly for solving the puzzle.

HanoiDrawer Class

// Advance one step to solve Hanoi
void HanoiDrawer::SolveNextStep()
	int platesCount
		, source
		, destination
		, intermediate;

		this->Hanoi(this->iPlatesCount, HanoiDrawer::SOURCE
			, HanoiDrawer::DESTINATION, HanoiDrawer::INTERMEDIATE);

	if(listSavedState.size() % 4 != 0 )

	platesCount = listSavedState.front();

	source = listSavedState.front();

	destination = listSavedState.front();

	intermediate = listSavedState.front();

	MovePlateFromTo(source, destination);


	if(iMovesCount == this->GetTotalMovesCount())
		this->solved = true;

	SetDlgItemInt(this->hWnd, this->countContainerResourceId, 
						GetMovesCount(), FALSE);

	SetDlgItemText(this->hWnd, this->fromContainerResourceId
				, PlaceIntToString(source).c_str() );
	SetDlgItemText(this->hWnd, this->toContainerResourceId
				, PlaceIntToString(destination).c_str() );
	SetDlgItemText(this->hWnd, this->throughContainerResourceId
				, PlaceIntToString(intermediate).c_str() );

// Draws stands and plates
// then do Invalidate
// this operation is required
// in each step
void HanoiDrawer::ReDraw()



// The internal function that is responsible
// about solve the problem.
// platesCount : how many plates
// source : the index of the source
// destination : the index of the destination
// intermediate : the index of the intermediate
void HanoiDrawer::Hanoi(int platesCount, int source, int destination, int intermediate)
	if (platesCount == 1)
		Hanoi(platesCount - 1, source, intermediate, destination);
		Hanoi(1, source, destination, intermediate);
		Hanoi(platesCount - 1, intermediate, destination, source);


  • The user has the ability to drag and drop the plates, then the program should verify user choice.



  • v1.0 14/7/2007: Main core and functionality


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Redwan Albougha
Software Developer
Syrian Arab Republic Syrian Arab Republic
I'm currently studying IT engineering. My general computer skills can be outlined :

• Programming languages :
C/C++, C#, Databases(Oracle and MySQL), PHP, ASP.NET.

• Operating systems :
Windows and Linux

• Frameworks and environments :
Borland development environment (C++), Windows SDK/Shell/GDI, MFC

• Programs and tools :
Adobe Dreamweaver, Adobe Photoshop, Adobe Flash

• Also :
Web Development skills (HTML, CSS, Javascript).

I'm interested in computer vision applications. Also I'm giving some tries to OS programming.

I hope to gain both the applied and theoretical knowledge of image processing field.

Comments and Discussions

Questionnyc bt how do i convert it to a game??!! Pin
code leaner8-Mar-13 3:07
Membercode leaner8-Mar-13 3:07 
GeneralMy vote of 5 Pin
farrelled30-Sep-11 1:40
Memberfarrelled30-Sep-11 1:40 
GeneralI like this! Pin
LloydA11128-Aug-09 10:36
MemberLloydA11128-Aug-09 10:36 
GeneralRe: I like this! Pin
Redwan Albougha29-Aug-09 0:04
MemberRedwan Albougha29-Aug-09 0:04 
GeneralTower of Hanoi Animated GIF... Pin
Drew Stainton23-Oct-08 5:04
MemberDrew Stainton23-Oct-08 5:04 
GeneralRe: Tower of Hanoi Animated GIF... Pin
Redwan Albougha23-Oct-08 5:14
MemberRedwan Albougha23-Oct-08 5:14 
GeneralRe: Tower of Hanoi Animated GIF... Pin
Drew Stainton23-Oct-08 5:24
MemberDrew Stainton23-Oct-08 5:24 
GeneralRe: Tower of Hanoi Animated GIF... Pin
Redwan Albougha23-Oct-08 5:42
MemberRedwan Albougha23-Oct-08 5:42 
GeneralWaiting your comments Pin
Redwan Albougha23-Oct-08 3:36
MemberRedwan Albougha23-Oct-08 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.