-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSemigraphics.cpp
More file actions
245 lines (192 loc) · 6.83 KB
/
Copy pathSemigraphics.cpp
File metadata and controls
245 lines (192 loc) · 6.83 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// ===========================================================================
// Semigraphics.cpp
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
#include <array>
#include <unordered_map>
#include <random>
// ===========================================================================
class Character
{
private:
std::string m_color;
std::string m_font;
public:
// c'tor(s), d'tor
Character() = default;
virtual ~Character() = default;
Character(const std::string& color, const std::string& font)
: m_color{ color }, m_font{ font } {}
// getter
std::string getColor() const { return m_color; }
std::string getFont() const { return m_font; }
friend std::ostream& operator<<(std::ostream&, const Character&);
};
std::ostream& operator<<(std::ostream& os, const Character& ch)
{
return os << "[ Color: " << ch.m_color << " - Font: " << ch.m_font << "]";
}
// ===========================================================================
class ConcreteCharacter
{
private:
std::shared_ptr<Character> m_sharedState;
public:
ConcreteCharacter() = default;
explicit ConcreteCharacter(const std::shared_ptr<Character>& state)
: m_sharedState{ state } {}
~ConcreteCharacter() = default;
void render(int x, int y) const noexcept;
friend std::ostream& operator<<(std::ostream&, const ConcreteCharacter&);
};
void ConcreteCharacter::render(int x, int y) const noexcept {
std::cout
<< "Character: Position (" << x << ", " << y <<
") with shared state " << *m_sharedState << std::endl;
}
std::ostream& operator<<(std::ostream& os, const ConcreteCharacter& cc)
{
return os << "[ConcreteCharacter: " << *cc.m_sharedState << "]";
}
// ===========================================================================
class CharacterFactory {
private:
std::unordered_map<std::string, std::shared_ptr<Character>> m_characterMap;
public:
void addCharacter(const std::string& color, const std::string& font);
ConcreteCharacter getConcreteCharacter(const std::string& color, const std::string& font);
std::string getKey(const std::string& color, const std::string& font) const;
friend std::ostream& operator<<(std::ostream&, const CharacterFactory&);
};
// returns an ConcreteCharacter with a given state or creates a new one
ConcreteCharacter CharacterFactory::getConcreteCharacter(const std::string& color, const std::string& font) {
std::string key = getKey(color, font);
if (m_characterMap.find(key) == m_characterMap.end()) {
std::cout << "CharacterFactory: Can't find this character, creating new one." << std::endl;
std::shared_ptr<Character> chptr = std::make_shared<Character>(color, font);
m_characterMap[key] = chptr;
}
else {
std::cout << "CharacterFactory: Reusing existing character." << std::endl;
}
std::shared_ptr<Character> ptr = m_characterMap.at(key);
return ConcreteCharacter(ptr);
}
std::string CharacterFactory::getKey(const std::string& color, const std::string& font) const
{
return color + "_" + font; // returns a hash for a given intrinsic state
}
void CharacterFactory::addCharacter(const std::string& color, const std::string& font)
{
std::string key = getKey(color, font);
m_characterMap[key] = std::make_shared<Character>(color, font);;
}
std::ostream& operator<<(std::ostream& os, const CharacterFactory& factory) {
size_t count = factory.m_characterMap.size();
os << "CharacterFactory: " << count << " characters:" << std::endl;
for (std::pair<std::string, std::shared_ptr<Character>> ch : factory.m_characterMap) {
os << ch.first << " => " << *ch.second << std::endl;
}
return os;
}
// ===========================================================================
class CharacterClient {
private:
std::array<std::string, 7> m_colors;
std::array<std::string, 3> m_fonts;
std::default_random_engine m_engine{};
std::uniform_int_distribution<int> m_colorsDistribution{ 0, static_cast<int>(m_colors.size()) - 1 };
std::uniform_int_distribution<int> m_fontsDistribution{ 0, static_cast<int>(m_fonts.size()) - 1 };
public:
// c'tor
CharacterClient();
std::string getRandomColor();
std::string getRandomFont();
int getRandomX();
int getRandomY();
};
CharacterClient::CharacterClient() {
m_colors = {
std::string{ "Red" },
std::string{ "Green" },
std::string{ "Blue" },
std::string{ "White" },
std::string{ "Black" },
std::string{ "Yellow" },
std::string{ "Magenta" }
};
m_fonts = {
std::string{ "Arial" },
std::string{ "Verdana" },
std::string{ "Roboto" }
};
}
std::string CharacterClient::getRandomColor() {
long long int index = m_colorsDistribution(m_engine);
return m_colors[index];
}
std::string CharacterClient::getRandomFont() {
long long int index = m_fontsDistribution(m_engine);
return m_fonts[index];
}
int CharacterClient::getRandomX() {
return (m_colorsDistribution(m_engine) * 100);
}
int CharacterClient::getRandomY() {
return (m_colorsDistribution(m_engine) * 100);
}
// ===========================================================================
void initSmallFactory(CharacterFactory& factory)
{
std::array<std::string, 5> colors = {
std::string{ "Red" },
std::string{ "Green" },
std::string{ "Blue" }
};
for (const std::string& color : colors) {
factory.addCharacter(color, std::string{ "Arial" });
}
}
void initLargeFactory(CharacterFactory& factory)
{
std::array<std::string, 5> colors = {
std::string{ "Red" },
std::string{ "Green" },
std::string{ "Blue" },
std::string{ "White" },
std::string{ "Black" }
};
std::array<std::string, 3> fonts = {
std::string{ "Arial" },
std::string{ "Fixedsys" },
std::string{ "Roboto" }
};
for (const std::string& color : colors) {
for (const std::string& font : fonts) {
factory.addCharacter(color, font);
}
}
}
void testSemigraphics01() {
CharacterFactory factory;
initLargeFactory(factory);
std::cout << factory << std::endl;
}
void testSemigraphics02() {
CharacterClient client;
CharacterFactory factory;
initSmallFactory(factory);
// or
// initLargeFactory(factory);
for (int i = 0; i < 15; i++) {
std::string color{ client.getRandomColor() };
std::string font{ client.getRandomFont() };
ConcreteCharacter cc{ factory.getConcreteCharacter(color, font) };
cc.render(client.getRandomX(), client.getRandomY());
}
}
// ===========================================================================
// End-of-File
// ===========================================================================