Hanoi Tower puzzle with vectors

Hello,

I am trying to implement a Hanoi Tower puzzle with vectors in C++. I specifically have to use vectors, and I am trying to avoid making this into a class. I am pretty sure I have implement most of this correctly, but my tower switches numbers funnily in a couple places. The last two lines are correct at least...

I have been stuck on this all day...I am getting a little disheartened :( Can somebody please help with what I am doing wrong? Thanks again.

Code:
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
39
40
41
42
43
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

    void hanoiTower(vector<int> &src, vector<int> &dest, vector<int> &temp, long size) {
        if (size == 0)
            return;
        
        hanoiTower(src, temp, dest, size - 1);
        dest.push_back(src.back());
        
        src.pop_back();
        
        cout << "src: ";
        for (vector<int >::const_iterator iter = src.begin(); iter != src.end(); iter++)
            cout << *iter << " ";
        cout << " | ";
        cout << "dest: ";
        for (vector<int >::const_iterator iter = dest.begin(); iter != dest.end(); iter++)
            cout << *iter << " ";
        cout << " | ";
        cout << "temp: ";
        for (vector<int >::const_iterator iter = temp.begin(); iter != temp.end(); iter++)
            cout << *iter << " ";
        cout << endl;
            hanoiTower(temp, dest, src, size - 1);

    }

int main() {
    int numPegs;
    vector<int> source, tempo, destin;
    cout << "Enter the tower height: ";
    cin >> numPegs;
    int counter = numPegs;
    for (int i = 0; i < numPegs; i++) {
        source.push_back(counter);
        counter--;
    }
    hanoiTower(source,destin,tempo,source.size());
}



Output:

Enter the tower height: 4
src: 4 3 2 | dest: 1 | temp:
src: 4 3 | dest: 2 | temp: 1
src: | dest: 2 1 | temp: 4 3
src: 4 | dest: 3 | temp: 2 1
src: 2 | dest: 4 1 | temp: 3
src: | dest: 3 2 | temp: 4 1
src: 4 | dest: 3 2 1 | temp:
src: | dest: 4 | temp: 3 2 1
src: 3 2 | dest: 4 1 | temp:
src: 3 | dest: 2 | temp: 4 1
src: 4 | dest: 2 1 | temp: 3
src: | dest: 4 3 | temp: 2 1
src: 2 | dest: 1 | temp: 4 3
src: | dest: 4 3 2 | temp: 1
src: | dest: 4 3 2 1 | temp:
Program ended with exit code: 0
Last edited on
Anyone?
@MrArtichoke

I don't think your program is switching your numbers, it's that your labels, src, dest and temp, are unchanging. Remember, your mixing up the vector locations at different times in the function, but your wording in the function, doesn't change. So, line 1 in the output, is correct. Line 2, should show 'src: 4 3 | temp: 2 | dest: 1'. Line 3 should show 'dest: | temp: 2 1 | src: 4 3 '. Then Line 4 is 'src: 4 | dest: 3 | temp: 2 1'. So, you have to figure a way to print different labels in the function, at different times. How, I'm not sure.

Edit..

I found it's easier to visualize the movements of the disks, if you comment out the labels in the function.
Last edited on
@whitenite1

Thank you so much for clarifying the unchanging labels! This helped me solve the program. :)

I declared the vectors as global static members instead of in my main function in order to solve the problem, and I made a display function with the labels to change them at different times.
That's great. I wouldn't mind seeing the changes you made, as I'm trying to get your program working correctly, as well. Here's what I came up with, but the labels still weren't correct

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Hanoi Puzzle.cpp : main project file.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;

static vector<int> source, tempo, destin;

void StartTower(vector<int> &src, vector<int> &dest, vector<int> &temp, int numDisks, int &counter)
{
	cout << "Starting disks : ";
	counter++;
	for (vector<int >::const_iterator iter = src.begin(); iter != src.end(); ++iter)
		cout << *iter << " ";
	cout << " | ";
	//cout << "dest: ";
	for (vector<int >::const_iterator iter = dest.begin(); iter != dest.end(); ++iter)
		cout << *iter << " ";
	cout << " | ";
	//cout << "temp: ";
	for (vector<int >::const_iterator iter = temp.begin(); iter != temp.end(); ++iter)
		cout << *iter << " ";
	cout << endl;
}

void hanoiTower(vector<int> &src, vector<int> &dest, vector<int> &temp, int numDisks, int &counter, int shuffle[3] )
{
	
	string label[3] = { "Src", "Dest", "Temp" };
	if (numDisks == 0)
		return;
	shuffle[0] = 0;
	shuffle[1] = 2;
	shuffle[2] = 1;
	hanoiTower(src, temp, dest, numDisks - 1, counter,shuffle);

	dest.push_back(src.back());
	src.pop_back();

	cout << "Move #" << counter << ": ";
	counter++;
	
	for (int show = 0; show < 3; show++)
	{
		switch (shuffle[show])
		{
			
		case 0:
			{
				cout << label[shuffle[show]] << " : "; 
				for (vector<int >::const_iterator iter = src.begin(); iter != src.end(); ++iter)
					cout << *iter << " ";
				cout << " | ";
			}
			break;
		case 1:
		{
			cout << label[shuffle[show]] << " : "; 
			for (vector<int >::const_iterator iter = dest.begin(); iter != dest.end(); ++iter)
				cout << *iter << " ";
			cout << " | ";
		}
		break;
		case 2:
		{
			cout << label[shuffle[show]] << " : "; 
			for (vector<int >::const_iterator iter = temp.begin(); iter != temp.end(); ++iter)
				cout << *iter << " ";
			cout << " | ";
		}
		break;
		}
	}
	cout << endl;
	
   shuffle[0] = 2;
   shuffle[1] = 1;
   shuffle[2] = 0;
hanoiTower(temp, dest, src, numDisks - 1, counter, shuffle);

}

int main() {
	int numDisks, counter = 0, shuffle[3] = { 0, 1, 2 };
	
	cout << "Enter the tower height: ";
	cin >> numDisks;
	for (int i = 0; i < numDisks; i++)
	{
		source.push_back(numDisks - i);
	}

	//Show initial disk placement
	StartTower(source, destin, tempo, numDisks, counter);
	// Solve tower
	hanoiTower(source, destin, tempo, numDisks, counter, shuffle);

	cout << endl << "Tower of Hanoi, solved..  Game finished." << endl;
	cin >> counter;

}
Here is how I did 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

static vector<int> source, tempo, destin;

void displayTowers();
void hanoiTower(vector<int> &, vector<int> &, vector<int> &, long);

int main() {
    long numPegs;
    cout << "Enter the tower height: ";
    cin >> numPegs;
    long counter = numPegs;

    for (long i = 0; i < numPegs; i++) {
        source.push_back(counter);
    counter--;
    }

    displayTowers();
    hanoiTower(source,destin,tempo,numPegs);
}

void displayTowers() {
    cout << "src: ";
    for (vector<int >::const_iterator iter = source.begin(); iter != source.end(); iter++)
        cout << *iter << " ";
    cout << " | ";
    cout << "dest: ";
    for (vector<int >::const_iterator iter = destin.begin(); iter != destin.end(); iter++)
        cout << *iter << " ";
    cout << " | ";
    cout << "temp: ";
    for (vector<int >::const_iterator iter = tempo.begin(); iter != tempo.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}

void hanoiTower(vector<int> &source, vector<int> &destin, vector<int> &tempo, long size) {
    if (size > 0) {
        hanoiTower(source, tempo, destin, size - 1);
        destin.push_back(source.back());
        source.pop_back();
        displayTowers();
        hanoiTower(tempo, destin, source, size - 1);
     }
}
Last edited on
@MrArtichoke

Very nice, and a lot shorter than what I was coming up with.
( And still wasn't working correctly ;) ). Thanks for the updated code. I replaced my version with yours, though, technically, they both were yours.
Topic archived. No new replies allowed.