Quick help with this treasure-finding code

Hello, I am having difficulty adding in one feature into my code that I have written. I only have trouble with the bolded part...

Here is the exercise:
A treasure is hidden someplace! 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. The Explorer is first positioned at location (15,15). 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(static_cast<double>((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
Ask the user to “Please enter direction (n,s,e,w), or x to exit: “.
Update the Explorer’s coordinates.
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). 
At the end of each loop display the Explorer’s coordinates.
Make sure that you print out how many steps it took to reach the treasure.



And here is my code so far:


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
#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
	char direction;
	float 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 << "Please enter a direction to travel or type x to exit" << 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;
		else if (direction == 'x' || direction == 'x')
		{
			cout << "Exit" << endl;
			return 0;
		}

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

		if (distance == 0)
		{
			treasure = true;
			cout << "You have arrived at the treasure" << endl;
			return 0;
		}
	} while (treasure == false || direction != 'x' || direction != 'x');

}
Last edited on
Add a step counter that counts up 1 every time the explorer moves.

Changes are commented

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
#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
	char direction;
	float distance;
	bool treasure = false;
	int stepCounter = 0; //<-- added step counter

	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 << "Please enter a direction to travel or type x to exit" << 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;
		else if (direction == 'x' || direction == 'x')
		{
			cout << "Exit" << endl;
			return 0;
		}

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

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

		if (distance == 0)
		{
			treasure = true;
			cout << "You have arrived at the treasure" << endl;
			cout << "It took the explorer "<< stepCounter << " steps" << endl; //<-- added step output
			return 0;
		}
	} while (treasure == false || direction != 'x' || direction != 'x');

}


Also note that your direction if and else if statements are all testing the lowercase letter twice you either need to change them some to upper case or what I would do is use toupper()
Last edited on
Topic archived. No new replies allowed.