-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utilities.h
98 lines (78 loc) · 2.84 KB
/
Utilities.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include<vector>
#include<string>
#include<cstdlib>
#include<ctime>
#include"Property.h"
#include <iostream>
class Utilities {
public:
static int getIntInRange(std::string prompt, int low, int high);
static int getInt(std::string prompt);
static std::string getStringYesNo(std::string prompt);
static std::string getStringLimit(std::string prompt, int max);
static std::vector<Space*> getProperty(Player& player, std::vector<Space*> spaces);
static std::vector<Space*> getMortgagedProperty(Player& player, std::vector<Space*> spaces);
static std::vector<Space*> getOwnedRailroads(Player& player, std::vector<Space*> spaces);
static std::vector<Space*> getOwnedUtilities(Player& player, std::vector<Space*> spaces);
static std::vector<std::vector<Space*>> getOwnedSets(Player& player, std::vector<Space*> spaces, std::vector<std::string> colors);
static std::vector<std::vector<Space*>> getHotellableSets(Player& player, std::vector<Space*> spaces, std::vector<std::string> colors);
static void resetOwnership(Player& player, std::vector<Space*> spaces);
template<typename T>
static void removeByValue(std::vector<T>&, T compare);
template<typename T>
static int getIndexPtrObj(std::vector<T>&, T compare);
template<typename T>
static void removeObjectByValue(std::vector<T>&, T compare);
template<typename T>
static void removePtrObjectByValue(std::vector<T>&, T compare);
template<typename T>
static T removeRandomElement(std::vector<T>&);
template<typename T>
static T& getRandomElement(std::vector<T>&);
private:
};
template<typename T>
inline void Utilities::removeByValue(std::vector<T>& vec, T compare) {
for (size_t i{ 0 }; i < vec.size(); ++i) {
if (vec[i] == compare) {
vec.erase(vec.begin() + i);
}
}
}
template<typename T>
inline int Utilities::getIndexPtrObj(std::vector<T>& vec, T compare) {
for (size_t i{ 0 }; i < vec.size(); ++i) {
if (vec[i]->getName() == compare->getName()) {
return i;
}
}
}
template<typename T>
inline void Utilities::removeObjectByValue(std::vector<T>& vec, T compare) {
for (size_t i{ 0 }; i < vec.size(); ++i) {
if (vec[i].getName() == compare.getName()) {
vec.erase(vec.begin() + i);
}
}
}
template<typename T>
inline void Utilities::removePtrObjectByValue(std::vector<T>& vec, T compare) {
for (size_t i{ 0 }; i < vec.size(); ++i) {
if (vec[i]->getName() == compare->getName()) {
vec.erase(vec.begin() + i);
}
}
}
template<typename T>
inline T Utilities::removeRandomElement(std::vector<T>& vec) {
int randIndex = (vec.size() <= 1) ? 0 : rand() % (vec.size());
T randElement = vec[randIndex]; //set it so we don't lose it after erasing
vec.erase(vec.begin() + randIndex);
return randElement;
}
template<typename T>
inline T& Utilities::getRandomElement(std::vector<T>& vec) {
int randIndex = (vec.size() <= 1) ? 0 : rand() % (vec.size());
return vec[randIndex];
}