solve using c++ program Switch, If, Loops A treasure is hidden someplace ! The c
ID: 3681868 • Letter: S
Question
solve using c++ program
Switch, If, Loops 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 l 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 II Procedures 1. You must use a LOOP 2. At the start of the loop, display the message "Please enter direction (n,s,ew), 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-castExplanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main ()
{
int x=30,y=30,a=0; // Explorers coordinates
int x1,y1; // Treasures coordinates
char direction;
float 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 1 and 30;
//Prompt user to enter direction.
cout << "I've hidden your precious treaure in my virtual map!" << endl;
cout << "Enter a direction (n,s,e,w) to start searching for it: " << endl;
while(!treasure)
{
a++;
//User input direction.
cin>>direction;
//Cases to add to corridinates.
switch (direction)
{
case 'n':
x=x+0;
y=y+1;
break;
case 's':
x=x+0;
y=y-1;
break;
case 'e':
x=x+1;
y=y+0;
break;
case 'w':
x=x-1;
y=y+0;
break;
}
//Display new coordinates.
cout<<"Your new corridintates are "<<x<<' '<<y<<endl;
//Compute distance.
distance = sqrt(static_cast<double>((x-x1)*(x-x1)+(y-y1)*(y-y1)));
//Display distance.
cout<<"Your distance is "<<distance<<endl;
//Display how far if distance is greater than 8
if(distance>8)
cout<<"You are too far from the treasure!"<<endl;
//Display how far if distance is between 8 and 4
else if(distance>4 && distance<=8)
cout<<"You are far from the treasure!"<<endl;
//Display how far if distance is inbetween 0 and 4
else if(distance<=4 && distance>0)
cout<<"You are getting close to the treasure!"<<endl;
//When we found treasure, get out of loop
else
{
cout<<"You have found the treasure!"<<endl;
cout<<"It took you "<<a<<" moves."<<endl;
treasure=true;
}
}
return 0;
}
sample output
I've hidden your precious treaure in my virtual map!
Enter a direction (n,s,e,w) to start searching for it:
n
Your new corridintates are 30 31
Your distance is 33.9411
You are too far from the treasure!
s
Your new corridintates are 30 30
Your distance is 33.2415
You are too far from the treasure!
e
Your new corridintates are 31 30
Your distance is 33.9411
You are too far from the treasure!
s
Your new corridintates are 30 30
Your distance is 33.2415
You are too far from the treasure!
e
Your new corridintates are 31 30
Your distance is 33.9706
You are too far from the treasure!
w
Your new corridintates are 30 30
Your distance is 33.2415
You are too far from the treasure!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.