//lead the user into a maze and back out again, while searching for a tapestry. #include // Provides toupper #include // Provides cin, cout void eat_line( ) { char next; do cin.get(next); while (next != '\n'); } bool inquire(const char query[ ]) { char answer; do { cout << query << " [Yes or No]" << endl; cin >> answer; answer = toupper(answer); eat_line( ); } while ((answer != 'Y') && (answer != 'N')); return (answer == 'Y'); } bool deadend() // Postcondition:The return value is true if the direction directly in front // is a deadend (i.e., a direction that cannot contain the tapestry). { return inquire("Are you facing a wall?") || inquire("Is your name written in front of you?"); } bool traverse_maze() { int direction; // Counts 1, 2, 3 for the three directions to explore bool found; // Set to true if we find the tapestry cout << "Step forward & write your name on the ground." << endl; found = inquire("Have you found the tapestry?"); if (found) { // Pick up the tapestry and step back from whence you came. cout << "Pick up the tapestry and take a step backwards." << endl; } else { // Explore the three directions (not counting the one that you just // came from). Start with the direction on your left, and then // turn through each of the other possible directions one at a time. cout << "Please turn left 90 degrees." << endl; for (direction = 1; direction <= 3; direction++) { if ( !found && !deadend( ) ) // Not found and Not deadend found = traverse_maze( ); cout << "Please turn right 90 degrees." << endl; } // User is now facing the direction from whence he/she came, so step // forward and turn around. This will put the user in the exact // spot that he/she started was in at the start of this function call. cout << "Please step forward, then turn 180 degrees." << endl; } return found; } void main() { if (traverse_maze()) cout << "You found it!" << endl; else cout << "It wasn't found!" << endl; }