Towers of Hanoi Code explanation

I have to use a recursive function to solve towers of Hanoi Problem.
I found this code online, it works, but didn't understand it. Could someone please provide an explanation or add comments to explain what's happening?

#include <iostream>
using namespace std;


void move(int, int, int, int);

int main()
{
int num;
cout<<"Enter the number of disks: ";
cin>>num;
cout<<endl;
move(num,1,2,3);
return 0;
}

void move(int count, int n1, int n3, int n2)

{
if (count>0)
{
move (count-1,n1,n2,n3);
cout<<"Moving disk"<<"from"<<n1<<"to"<<n3<<"."<<endl;
move(count-1,n2,n3,n1);
}
}
> I found this code online, it works, but didn't understand it.
There's nothing like solving a problem for yourself to help understand it.

Will you understand it any better if someone explains it to you?

Will you have any clue whatsoever as to how to go about writing the solution to a similar problem, when your goto "I'll just google for the answer" fails you?

https://en.wikipedia.org/wiki/Tower_of_Hanoi
You can try adding instructions to let it display what it's doing while it runs.
Just start with little numbers, let's say 3 or 4, or even 2.

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


void move(int count, int n1, int n3, int n2);


int main()
{
    std::cout<<"Enter the number of disks: ";
    int num;
    std::cin >> num;
    std::cout << "Calling move("
                 "count = " << num
              << ", n1 = 1"
                 ", n3 = 2"
                 ", n2 = 3"
                 ")...\n";
    move(num, 1, 2, 3);
    return 0;
}


void move(int count, int n1, int n3, int n2)
{
    if (count > 0)
    {
        std::cout << "Calling move(" << count - 1
                  << ", " << n1
                  << ", " << n2
                  << ", " << n3
                  << ")...\n";
        move(count-1, n1, n2, n3);
        std::cout << "--> Moving disk from " << n1 << " to " << n3 << ".\n";
        std::cout << "Calling move(" << count - 1
                  << ", " << n2
                  << ", " << n3
                  << ", " << n1
                  << ")...\n";
        move(count-1, n2, n3, n1);
    }
}

Topic archived. No new replies allowed.