Skip to content

Instantly share code, notes, and snippets.

@Zerophase
Created October 15, 2014 10:11
Show Gist options
  • Save Zerophase/9f0f789a44e4e5af7b78 to your computer and use it in GitHub Desktop.
Save Zerophase/9f0f789a44e4e5af7b78 to your computer and use it in GitHub Desktop.
Tower of Hanoi Peg to move from code
Peg *pegToMoveFrom = nullptr;
for (auto peg = pegs.begin(); peg != pegs.end(); peg++)
{
// hard coded values need to generalize for N
// makes sure to move till all of the pegs are filled.
// Then it moves the smallest peg from the goal peg to the auxillary peg
// Then it moves the smallest peg from the auxillary peg to the start peg
// then it moves the peg on the bottom of the auxillary peg to the goal peg
// then it moves the smallest peg to the goal peg.
if (peg == pegs.begin())
pegToMoveFrom = (*peg);
else if (goalPeg->Compare() == 0 &&
pegToMoveFrom->Compare() == 2 &&
(*peg)->Compare() != -1)
pegToMoveFrom = goalPeg;
if (goalPeg->Compare() == 2 &&
(*peg)->Compare() == 0)
pegToMoveFrom = *peg;
else if ((*peg)->Compare() == 1 &&
goalPeg->Compare() == 2)
pegToMoveFrom = *peg;
}
pegToMoveFrom->Update();
// if a peg has a Deque with elements return the size of the back element.
// size is how big a disk is.
int Peg::Compare()
{
if (HasDeque())
return disks.back()->Size();
else
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment