Tower of Hanoi

Hello
I'm writing a code for the Tower of Hanoi problem and I currently have no clue what to do. Part of the code is already complete and I just need to finish it in the spots that say ***Add your code here*** There are comments that sort of describe what the code needs to be doing...
(This is just a segment of the code)
If someone could give me some hints or advice on how to tackle this I would really appreciate it!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void MoveDisk(int source, int destination)
{
	NumberOfMoves++;

	int Disk = 0; //temporary variable to hold the disk that is being moved

	//remove disk from source tower
	//start from top of tower and search downward until disk is found
	
	
	//**************************************
	//**********Add your code here**********
	//**************************************


	system("cls");
	cout << endl << "Move Disk " << Disk << " from tower " << code[source];
	cout << " to tower " << code[destination] << "  (Move # " << NumberOfMoves << ")" << endl << endl << endl;


	//add disk to destination tower
	//start from top of tower and search downward until either a disk is found, or bottom of tower is reached
	//if a disk is found -> place moved disk on top of it
	//if bottom of tower is reached -> place moved disk at bottom of tower
	

	
	//**************************************
	//**********Add your code here**********
	//**************************************



	DrawTower();
	cin.get(); //wait for enter press
}
Last edited on
solve it for 3 disks on paper, make sure you fully understand what needs to happen to move the disks.
not knowing what you have covered... can you use a stack? The TOH problem is effectively 3 stacks... you can do it without, of course. Or are you looking to do recursion -- that has a 'free' stack...
Last edited on
For a simple recursive solution, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int moveDisks(int count, int needle1, int needle3, int needle2)
{
   if (count > 0) {
      moveDisks(count - 1, needle1, needle2, needle3);
      cout << "Move disk " << count << " from " << needle1 << " to " << needle3 << endl;
      moveDisks(count - 1, needle2, needle3, needle1);
   }
   return count;
}

int main()
{
        const int cnt = 5;

	return moveDisks(cnt, 1, 3, 2);
}

Last edited on
Ok so I realize that I should have mentioned some other stuff...
I have a recursive function already the does what seeplus said to do. Unfortunately this code is supposed to be unnecessarily complicated and I have to have another piece of code like MoveDisk()
As for stacks, I haven't learnt about them, but this is using a 2D array (are they similar?).
I've figured out part of the code so hopefully it will be easier to understand what I'm trying to do.
Thanks again!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void MoveDisk(int source, int destination)
{
	NumberOfMoves++;

	int Disk = 0; //temporary variable to hold the disk that is being moved

	
	int i = N - 1;

	while (i >= 0 && Disk == 0) {
		if (ToH[source][i] != 0) {
			Disk = ToH[source][i];
		}
		else {
			i-1;
		}
	}


	system("cls");
	cout << endl << "Move Disk " << Disk << " from tower " << code[source];
	cout << " to tower " << code[destination] << "  (Move # " << NumberOfMoves << ")" << endl << endl << endl;


	//this part below is where I'm struggling
	while (i <= N /*??*/) {
		if (ToH[destination][i] == 0) {
			i + 1
		}
		else {
			ToH[destination][i] = Disk;
		}
	}  


	DrawTower();
	cin.get(); //wait for enter press
}
Last edited on
I have a recursive function already the does what seeplus said to do. Unfortunately this code is supposed to be unnecessarily complicated


Complicated???
Topic archived. No new replies allowed.