Making a Tower of Hanoi problem that displays each step visually.

I've been stuck for a couple days now on this assignment. I just need a couple pointers of where I need to go next, so if you could help, it'd be greatly appreciated. I have the function prototypes of what I know I need (although their parameters may not be what they need to be). Any help would be great.

PS: The code I have right now probably isn't right. I just wanted to test out printing the tower of hanoi.

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
  #include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void solve(int n, string A[], string B[], string C[], char src, char dest, char spare);
void initializeBoard(string A[], string B[], string C[]);
void printBoard(string A[], string B[], string C[]);
void moveDisk();

int main(void) {
	int num = 4;
	string A[4];
	string B[4];
	string C[4];

	solve(num, A, B, C, 'A', 'C', 'B');

	return 0;
}

void 
initializeBoard(string A[], string B[], string C[]) {

	A[0] = "#";
	for(int i=1; i<4; i++) {
		A[i] = A[i-1] + "##";
	}
	
	cout << "     |" << setw(23) << "|" << setw(23) << "|" << endl 
			 << "     " << A[0] << setw(23) << "|" << setw(23) << "|" << endl 
			 << "    " << A[1] << setw(22) << "|" << setw(23) << "|" << endl 
			 << "   " << A[2] << setw(21) << "|" << setw(23) << "|" << endl 
			 << "  " << A[3] << setw(20) << "|" << setw(23) << "|" << endl;
}


void
solve(int n, string A[], string B[], string C[], char src, char dest, char spare) {
	if(n==1) {
		cout << src << " to " << dest << ":" << endl; 
		cout << setfill('-') << setw(63) << '-' << endl << setfill(' ');			
		initializeBoard(A, B, C);
    cout << setfill('-') << setw(63) << '-' << setfill(' ') << endl 
			  << setw(6) << right << "A" << setw(23) << "B" << setw(23) << "C" << endl << endl;
		return;
	}

	solve(n-1, A, B, C, src, spare, dest);
	solve(1, A, B, C, src, dest, spare);
	solve(n-1, A, B, C, spare, dest, src);
}
Topic archived. No new replies allowed.