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!
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
}
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...
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!
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
}