/* testfunc16.cpp CIS 150 2008-06-10 David Klick Recursive solution to Towers of Hanoi. */ #include #include using std::cin; using std::cout; int getInt(char* prompt, int min, int max); char getChoice(char* prompt, char* allowed); void moveDisks(int numDisks, int fromPole, int toPole); void cont(); int main(int argc, char* argv[]) { int disks; // number of disks to be moved char ans; // holds user answer for "play again?" do { // get number of disks from user (1 to 10) disks = getInt("Enter number of disks (1-10): ", 1, 10); // move all disks from first pole to third pole cout << "Moving " << disks << " disks from pole 1 to pole 3\n"; moveDisks(disks, 1, 3); // find out if user wants to play again ans = getChoice("Would you like to play again? (Y/N) ", "YyNn"); } while (ans == 'Y'); // say "Goodnight" Gracie cout << "Normal program termination.\n"; cont(); return 0; } /* int getInt(char* prompt, int min, int max); Arguments: prompt: a pointer to a C-style string to be used as a prompt min: the minimum value allowed to be entered max: the maximum value allowed to be entered Returns: an integer entered by the user Prompts user with message passed in as argument and returns the integer value entered. The number must be between min and max (inclusive) or the user will be reprompted to enter another number. Note: For simplicity, no error checking on input is done, therefore this function may crash the program on invalid input. */ int getInt(char* prompt, int min, int max) { int num; do { cout << prompt; cin >> num; if (num < min) cout << "Error: value below minimum allowed\n"; else if (num > max) cout << "Error: value above maximum allowed\n"; } while (nummax); return num; } /* char getChoice(char* prompt, char* allowed); Arguments: char* prompt: a C-style string containing the prompt to show the user char* allowed: a C-style string containing all allowed user responses (each valid response being a single character) Returns: the user's choice (a character) Displays a prompt and then waits for the user to enter an allowed choice (a single character). Once a valid choice has been made, the function returns it to the calling function. */ char getChoice(char* prompt, char* allowed) { char ans; do { cout << prompt; cin >> ans; ans = toupper(ans); // convert answer to upper case if (strchr(allowed, ans) == NULL) cout << "Error: Invalid choice\n"; } while (strchr(allowed, ans) == NULL); return ans; } /* void moveDisks(int numDisks, int fromPole, int toPole); Arguments: int numDisks: the number of disks to be moved int fromPole: the pole to move the disks from (s/b 1, 2, or 3) int toPole: the pole to move the disks to (s/b 1, 2, or 3, and not be the same as the fromPole) Returns: nothing Effects: Displays the solution to the Towers of Hanoi problem on the screen move-by-move. This function solves the Towers of Hanoi problem by recursively moving disks from pole to pole. If there is only one disk to be moved, the routine moves it and is done. If there is more than one disk to be moved, it moves the stack of disks in pieces so that it calls this method again with fewer disks to be moved. Eventually, all calls result in moves of a single disk which can be easily solved. */ void moveDisks(int numDisks, int fromPole, int toPole) { int otherPole = 6 - fromPole - toPole; if (numDisks == 1) { cout << "Move disk from " << fromPole << " to " << toPole << '\n'; } else { moveDisks(numDisks-1, fromPole, otherPole); cout << "Move disk from " << fromPole << " to " << toPole << '\n'; moveDisks(numDisks-1, otherPole, toPole); } } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }