Created
October 15, 2014 10:11
-
-
Save Zerophase/9f0f789a44e4e5af7b78 to your computer and use it in GitHub Desktop.
Tower of Hanoi Peg to move from code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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