Recursion for Hanoi

I am asked to do a exercise which is about recursion in C++ for the Hannoi Tower.

The skeleton code is given like this: 

void move(int disc, char from, char to)

{

cout << "move disc " << disc << " from peg " << from << " to peg " << to << endl;

}

void tower_of_hanoi(int num_discs, char pegA, char pegB, char pegC)

{

if (num_discs == 1) // base case

{

move(1, pegA, pegC);

return;

}


tower_of_hanoi(num_discs - 1, pegA, pegC, pegB);

move(num_discs, pegA, pegC);

tower_of_hanoi(num_discs - 1, pegB, pegA, pegC);

}


What we are asked to do is to design a code making use of recursion to play the Hannoi game which orignally the stacks are placed on the two sides of peg with odd number on the left side (Peg A) and even number on the right side (Peg C). We need to place all the stack from pegA and PegC to the middle peg under the same rules of usual hanoi game. E.g. Peg A: (1,3,5) Peg B:  PegC:(2,4,6)


The hint asks us to start with solving n=4 in order to solve n=6. I have no idea how to solve it. Can anyone offer some help? Thank you so much!


void move_to_center(int num_discs)

{

// Please Help

}

Last edited on
Topic archived. No new replies allowed.