Click here to Skip to main content
5,788,212 members and growing! (15,522 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Algorithms     Advanced License: The Code Project Open License (CPOL)

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

By Redwan Al-Bougha

Shows how to solve Towers of Hanoi puzzle visually using Win32 and C++
C++, Windows, Win32, GDI

Posted: 23 Oct 2008
Updated: 23 Oct 2008
Views: 5,158
Bookmarked: 16 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 4.14 Rating: 3.71 out of 5
3 votes, 23.1%
1
1 vote, 7.7%
2
1 vote, 7.7%
3
2 votes, 15.4%
4
6 votes, 46.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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 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 in a smaller one.
  2. Only one plate can be moved at each step.

Please note that I used the recursive method.

Background 

Formally,  Towers of Hanoi recursive function is given by :

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

And here is a conceptual image :

Tower_of_Hanoi_4.gif

Credits: MG from Wikipedia

Using the Program

When you lunch the application you will see the next screen:

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

hanoi2.gif

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 Hanoi method I stored the states of the solution, meaning each time a call is happened the solution of the current step is stored in a list of integers. Since each step in Hanoi method require 4 parameters as mentioned above, 4 integer represents parameter is added to the list. Then each time SolveNextStep method is called, the program read from the list 4 items that represents the state.

Available classes:

  1. HanoiDrawer
  2. PlateGUIInfo
  3. Tools

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

HanoiDrawer Class

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

	if(listSavedState.size()==0)
	{
		this->Hanoi(this->iPlatesCount, HanoiDrawer::SOURCE
			, HanoiDrawer::DESTINATION, HanoiDrawer::INTERMEDIATE);
	}


	if(listSavedState.size() % 4 != 0 )
	{
		return;
	}

	platesCount = listSavedState.front();
	listSavedState.pop_front();

	source = listSavedState.front();
	listSavedState.pop_front();

	destination = listSavedState.front();
	listSavedState.pop_front();

	intermediate = listSavedState.front();
	listSavedState.pop_front();

	MovePlateFromTo(source, destination);

	this->iMovesCount++;

	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()
{
	DrawStands();

	DrawPlates();

	Invalidate();
}

// 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)
	{
		listSavedState.push_back(platesCount);
		listSavedState.push_back(source);
		listSavedState.push_back(destination);
		listSavedState.push_back(intermediate);
		return;
	}
	else
	{
		Hanoi(platesCount - 1, source, intermediate, destination);
		Hanoi(1, source, destination, intermediate);
		Hanoi(platesCount - 1, intermediate, destination, source);
		return;
	}
}

Improvements

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

Notices

History

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

License

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

About the Author

Redwan Al-Bougha


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.
Occupation: Software Developer
Location: Syrian Arab Republic Syrian Arab Republic

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralTower of Hanoi Animated GIF...supporterDrew Stainton6:04 23 Oct '08  
GeneralRe: Tower of Hanoi Animated GIF...memberRedwan Al-Bougha6:14 23 Oct '08  
GeneralRe: Tower of Hanoi Animated GIF...supporterDrew Stainton6:24 23 Oct '08  
GeneralRe: Tower of Hanoi Animated GIF...memberRedwan Al-Bougha6:42 23 Oct '08  
GeneralWaiting your commentsmemberRedwan Al-Bougha4:36 23 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by Redwan Al-Bougha
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project