diff options
| author | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-04-07 23:18:32 -0700 |
| commit | c1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch) | |
| tree | e8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter9/maze.cxx | |
| download | dscode-main.tar.xz dscode-main.zip | |
Diffstat (limited to 'chapter9/maze.cxx')
| -rw-r--r-- | chapter9/maze.cxx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/chapter9/maze.cxx b/chapter9/maze.cxx new file mode 100644 index 0000000..df4ff46 --- /dev/null +++ b/chapter9/maze.cxx @@ -0,0 +1,75 @@ +// FILE: maze.cxx
+// An interactive program to lead the user into a maze and back out again,
+// while searching for a magic tapestry.
+
+#include <cctype> // Provides toupper
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream.h> // Provides cin, cout
+#include "useful.h" // From Appendix I; Provides eatline, inquire
+using namespace std;
+
+// PROTOTYPES of functions used in this program.
+bool dead_end();
+// Postcondition:The return value is true if the direction directly in front
+// is a deadend (i.e., a direction that cannot contain the tapestry).
+
+bool traverse_maze();
+// Precondition: The user of the program is facing an unblocked spot in the
+// maze which has not previously been visited by the user.
+// Postcondition: The function has asked a series of questions and provided
+// various directions to the user. The questions and directions have led the
+// user through the maze and back to the exact same position that the user
+// started at. The return value of the function is a true/false value
+// indicating whether or not the user found a magic tapestry in the maze.
+
+
+int main()
+{
+ if (traverse_maze())
+ cout << "You found it!" << endl;
+ else
+ cout << "It wasn't found!" << endl;
+ return EXIT_SUCCESS;
+}
+
+bool dead_end()
+// Library facilities used: useful.h (From Appendix I)
+{
+ return inquire("Are you facing a wall?")
+ ||
+ inquire("Is your name written in front of you?");
+}
+
+bool traverse_maze()
+// Library facilities used: iostream.h
+{
+ int direction; // Counts 1, 2, 3 for the three directions to explore
+ bool found; // Will be 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 backward." << 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 && !dead_end( ) )
+ found = traverse_maze( );
+ cout << "Please turn right 90 degrees." << endl;
+ }
+
+ // You're now facing the direction from whence you came, so step
+ // forward and turn around. This will put you in the exact
+ // spot that you were at when the function call began.
+ cout << "Please step forward, then turn 180 degrees." << endl;
+ }
+ return found;
+}
|