-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCity.cpp
More file actions
73 lines (66 loc) · 1.73 KB
/
City.cpp
File metadata and controls
73 lines (66 loc) · 1.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by danny on 2018-11-08.
//
#include "City.hpp"
/**
* Default constructor that generates random values for x, y, and name
*/
City::City() {
x = RandomNumGenerator::getInstance().getIntegerInRange(MIN, MAP_BOUNDARY);
y = RandomNumGenerator::getInstance().getIntegerInRange(MIN, MAP_BOUNDARY);
name = genRandom();
}
/**
* Generates a random string for the city's name
* @return string of 8 characters
*/
string City::genRandom() {
string rName;
int len = 8;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
for (int i = 0; i < len; ++i) {
rName += alphanum[rand() % stringLength];
}
return rName;
}
/**
* Overloaded insertion operator
* @param os output
* @param c city being printed
* @return output to console
*/
ostream &operator<<(ostream &os, const City &c) {
return os << c.name << ", <" << c.x << ", " << c.y << ">" << endl;
}
/**
* Calculates the distance between two cities
* @param city1 city being compared to
* @return distance between cities
*/
double City::getDistanceBetweenCities(const City &city1) {
int distX = abs(getX() - city1.getX());
int distY = abs(getY() - city1.getY());
return sqrt(pow(distX, 2) + pow(distY, 2));
}
/**
* Getter for x value
* @return x value
*/
inline int City::getX() const { return x; }
/**
* Getter for y value
* @return y value
*/
inline int City::getY() const { return y; }
/**
* Overloaded equals operator
* @param c city being compared
* @return boolean true if cities are equal
*/
bool City::operator==(const City &c) const {
return (name == c.name) && (x == c.x) && (y == c.y);
}