Question

# C++ Homework on Loop Structure A treasure is hidden someplace – the treasures coordinates are (x1,...

C++

Homework on Loop Structure

A treasure is hidden someplace – the treasures coordinates are (x1, y1)! The coordinates (x1, y1) are determined randomly, using the code that is listed at the end of this exercise. The purpose of the game is for the Explorer to find the Treasure!
The explorer is allowed to go North, South, West or East by one step each time. The Explorer is first positioned at location (15,15). If the explorer goes North by one step, the Explorer’s y coordinate is increased by 1. If the explorer goes South by one step, the y coordinate is decreased by 1. In the same fashion the x coordinate is increased by 1 if the explorer goes East by one step and the x coordinate is decreased by 1 if the explorer goes West by one step.
Each time the Explorer ‘moves’, the distance between the Explorer and the treasure is computed. The formula for the distance between (x, y) and (x1, y1) is
distance = sqrt(static_cast<double>((x-x1) *(x-x1) + (y-y1)
* (y-y1)));
//static_cast<double> in this statement is for data type
//conversion from int to double.
When the Explorer’s position is the same as the Treasure’s position, the Explorer found the Treasure!
Procedures
2. Update the Explorer’s coordinates.
3. Calculate and display the distance from the Explorer to the Treasure (this
information will clue the Explorer to either keep going in the same direction or
switch directions).
4. At the end of each loop display the Explorer’s coordinates.
5. Make sure that you print out how many moves it took (number of times the loop
ran) to reach the treasure.
6. Use the starter code below, and see hints below the starter code to help with
developing code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;
int main () {
int x=15, y=15;
int x1, y1;
char direction;
double distance;
bool treasure=false;
srand(time(0));
function !
x1=rand( ) % 30 + 1;
and 30
y1=rand( ) % 30 + 1;
and 30
// Explorer’s coordinates
// Treasure’s coordinates
//write loop to find the treasure
HINT: When developing the code, print out the coordinates of the treasure right after the rand() function is called. This is to help you test your program. Once the code works well, comment or remove those "peeking" statements.
// secretly seed the rand
// set X1 to random between 1
// set y1 to random between 1

The code snippet for the the problem is given below with all the functions properly commented.

The code uses a do-while loop when the variable treasure = false, the control enters the loop and calculates the distance given by the formula. Now in nested if-else statements user input is evaluated and the coordinates are printed after every input. Finally if the distance is zero than the treasure is found.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{
int x = 15, y = 15;             // Explorer’s coordinates
int x1, y1;             // Treasure’s coordinates
int counter = 0; //<-- added counter
char direction;
double distance;
bool treasure = false;

srand(time(0));
x1 = rand() % 30 + 1;
y1 = rand() % 30 + 1;

do
{
distance = sqrt(static_cast<double>((x - x1)*(x - x1) + (y - y1)*(y - y1)));
cout << "Enter enter a direction to travel" << endl;
cin >> direction;

if (direction == 'n' || direction == 'N')
y += 1;
else if (direction == 's' || direction == 'S')
y -= 1;
else if (direction == 'e' || direction == 'E')
x += 1;
else if (direction == 'w' || direction == 'W')
x -= 1;

cout << "Your coordinates are now: " << endl;
cout << "X-Coordinate: " << x << endl;
cout << "Y-Coordinate: " << y << endl;
cout << "You are " << distance << " coordinates away from the treasure" << endl;

counter++;//<-- steps count up

if (distance == 0)
{
treasure = true;
cout << "Treasure Found" << endl;
return 0;
}
} while (treasure == false);

}
``````

#### Earn Coins

Coins can be redeemed for fabulous gifts.