blob: df4ff46c538f7f05003f9b1b458c357e818fd98d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;
}
|