Help with Lab!

This is my lab: (A treasure is hidden someplace ! The coordinates (x1,y1) are determined randomly, using the code that is listed at the end of this lab. The purpose of the game is for the Explorer to find the Treasure !

The explorer is allowed to go North, South, West or East. The Explorer is first positioned at location (30,30). If the explorer goes North, only the y coordinate is increased by 1. Similarly if the explorer goes South the y coordinate is decreased by 1. In the same fashion the x coordinate is increased or decreased by 1 depending if the explorer goes East
or West, respectively.

Each time the Explorer ‘moves’, the distance between the Explorer and the treasure is computed. Distance = sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
When the Explorer’s position is the same as the Treasure’s position, the Explorer wins the Treasure !!

Procedures
1. You must use a LOOP
2. At the start of the loop, display the message "Please enter direction (n,s,e,w), or x to exit: ".
3. Now, get the position from the keyboard.
4. Update the Explorer’s coordinates
5. Calculate the distance from the explorer to the treasure:
Distance = sqrt(static_cast<double>((x-x1)*(x-x1)+(y-y1)*(y-y1)))

6. Display the distance from the Treasure (this information will clue the Explorer to either keep going in the same direction or switch directions).

7. Decisions……………
If the distance is greater than 8 then you display the message "you are too far from the treasure".
If the distance is greater than 4 and less or equal to 8 you display the message "you are far from the treasure".
If the distance is less or equal to 4 you display the message "you are getting closer to the treasure".
Also, check if you have reached the treasure (i.e., x=x1, and y=y1); and if so, Get out of the loop and display the message "congratulations, you have reached the treasure".

8. At the end of each loop display the message "Your location is: ( x,y); where x and y are the
Explorer’s coordinates

9. Make sure that you print out how many steps it took to reach the treasure)


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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath> 
#include <string>
#include <iomanip>
using namespace std;
 
int main ()
{
char direction;// direction
int x=30,y=30;                       // Explorer’s coordinates
int x1,y1;                                // Treasure’s coordinates
char dir='a';
double distance;
bool treasure=false;
  
srand(time(0));                      // secretly seed the rand function !
x1=rand( ) %30 + 1;            // x1 is randomly set to a number between 1 //and 30
y1=rand( ) % 30 + 1;           // y1 is randomly set to a number between //1and 30;
  

            //write loop to find the treasure
while(1) 
{ 
cout<< "Please enter direction (n,s,e,w), or x to exit: ";
cin>>direction;

if (direction == 'n')
cout<<y++<<endl;

else if (direction == 's')
cout<<y--<<endl;

else if (direction == 'e')
cout<<x++<<endl;

else if (direction == 'w')
cout<<x--<<endl;

else if (direction == 'x')
return 0;

distance = sqrt(static_cast<double>((x-x1)*(x-x1)+(y-y1)*(y-y1)));
}
}


This what I did so far but I'm stuck on what to do next.
Any suggestion will help!
Thank you!
Last edited on
Inside the loop, you have to keep track of the current location of the Treasure Hunter as well as the location of the Treasure. Your code to increment/decrement x or y is good, as is your definition of the distance.

After computing the new location of the Treasure Hunter, call the distance calculator. If the distance > 8, print "Too far!", otherwise, if the distance is > 4, print "Far", otherwise print the distance. Hmm, if you put the call to the distance function (probably want to make that a function) at the top of the loop, before user input, the user can see how far away she is at the start, and after the move, can see whether the distance decreased or increased.

When the user takes a turn, make sure that they don't wander off of the map:

1
2
3
4
case "W":
case "w":
    x = x - 1 < 0 ? 0, x - 1;
    break;


(The above assumes you replace the "if /else if / else" with "switch (direction){}" .) Check for > 30 for "E" and "N" (again, assuming (30, 30) is top right).

Rather than "while (1) {}", I would use "do {} while (distance > 0);"; that's your break condition, everything else just loops around for another turn. Remember, for every iteration of the loop, you need to increase the number of moves made for the final output.

Based on your existing code, it looks like you want to exit the program when the user inputs "X". Think about what happens if some other value is input.
Last edited on
Topic archived. No new replies allowed.