Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I have a simple assignment i\'m having incredible difficulty in. Here is the

ID: 670684 • Letter: H

Question

Hi, I have a simple assignment i'm having incredible difficulty in. Here is the question. Underneath it i've started the program with no loops yet. I'm not sure how to continue.

Find the treasure.

An explorer is allowed to go North, South, West or East.

He (or she) is originally positioned at location (0,0).

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

A treasure whose coordinates (x1,y1) are determined at random,or West respectively.

is hidden somewhere. The purpose of the game is to find the treasure.

At each step of the loop, you must display

the message "Your location is: " x,y; where x and y are the current coordinates . Then display the message "Please enter direction (n,s,e,w), or x to exit: ".

Immediately after, you must use cin >> dir , to get a character from the keyboard.

After this, you must update the coordinates according to the input .

You then must calculate the distance (declare as float) from the explorer to the treasure:

distance = sqrt((x-x1)^2.0+(y-y1)^2.0);

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". In addition, you must check if you have reached the treasure (i.e. x=x1, and y=y1), and if that is the case you must display the message "congratulations, you have reached the treasure". You must declare a bool variable called treasure initialized to false and if you reached the treasure must be set to true.

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <math.h>
#include <random>

int main ()

{ int x=0,y=0,x1,y1;

   char dir='a';

   bool treasure=false;

   randomize(); // random variable

   x1=random(30); // x1 is initialized to a number between 0 and 30;

   y1=random(30); // y1 is initialized to a number between 0 and 30;

Thank you in advance!

-Mike

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

int main ()
{
   int x=0,y=0,x1,y1;
   char dir='a';
   bool treasure=false;
   int max = 30, min = 0;
   double distance;
   x1=rand()%(max-min + 1) + min; // x1 is initialized to a number between 0 and 30;
   y1=rand()%(max-min + 1) + min;// y1 is initialized to a number between 0 and 30;
   cout<<"Target coordinate: "<<x1<<","<<y1<<endl;
   while(1){
       cout<<"Your location is: "<<x<<","<<y<<endl;
       cout<<"Please enter direction (n,s,e,w), or x to exit: "<<endl;
       cin >> dir;
       if(dir == 'x')
           break;
       switch(dir){
           case 'n':
               y++;
               break;
           case 's':
               y--;
               break;
           case 'e':
               x++;
               break;
           case 'w':
               x--;
               break;
       }
       if(x == x1 && y == y1){
           treasure = true;
           break;
       }
       else{
           distance = sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
           if(distance < 4)
               cout<<"You are getting closer to the treasure"<<endl;
           else if(distance > 4 && distance < 8)
               cout<<"you are far from the treasure"<<endl;
           else
               cout<<"you are too far from treasure"<<endl;
       }
      
   }
   if(treasure)
       cout<<"Treasure found"<<endl;
   else
       cout<<"Treasure not found"<<endl;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote