//---------------------------------------------------------------- // File: towers_of_hanoi.C // Author: Alicia Thorsen // Course: CS1129 // // Program outputs the moves to solve the towers of hanoi puzzle. //---------------------------------------------------------------- #include using namespace std; void solveHanoi(int, string, string, string); int main() { int disks; cout << "Enter # of disks: "; cin >> disks; solveHanoi(disks, "first", "middle", "last"); return 0; } //---------------------------------------------------------------- // Precondition: n must be greater than 0. // Postcondition: Outputs the moves to solve a towers of hanoi puzzle // of size n. //---------------------------------------------------------------- void solveHanoi(int n, string start, string temp, string dest) { string line; // Nothing to do if we have less than one disk. if (n < 1) return; // If we have at least one disk: // 1. Move all but the last disk from start to temp. solveHanoi(n - 1, start, dest, temp); // 2. Move the largest disk from start to dest. cout << start << "\tto\t" << dest << endl; getline(cin, line); // 3. Move the remaining disks from temp to dest. solveHanoi(n - 1, temp, start, dest); }