-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.cpp
219 lines (203 loc) · 5.99 KB
/
generate.cpp
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <exception>
#include <memory>
#include <random>
#include <chrono>
#include <numeric>
#include <iterator>
#include "cxxopts.hpp"
class CMDOptions {
private:
int mSize = 5;
bool mHexagonal = false;
bool mSquare = false;
bool mHelp = false;
double mRatio = 0.1;
cxxopts::Options options;
bool parse(int argc, char* argv[]) {
try {
options.parse(argc, argv);
return true;
} catch(const cxxopts::OptionException& e) {
std::cerr << "error parsing options: " << e.what() << std::endl;
return false;
}
}
void ensureConsistency() {
mSize = std::max(1, mSize);
if (mSquare && mHexagonal) {
mSquare = false;
}
if (!mSquare && !mHexagonal) {
mHexagonal = true;
}
}
public:
CMDOptions() : options("generate", "Generator of simple maps") {
options.add_options()
("n,size", "size of the map (default: 5)", cxxopts::value<int>(mSize))
("x,hexagonal", "generate hexagonal maps (default)", cxxopts::value<bool>(mHexagonal))
("q,square", "generate square maps", cxxopts::value<bool>(mSquare))
("r,ratio", "wall block to free block ratio (default: 0.1)", cxxopts::value<double>(mRatio))
("h,help", "Prints help", cxxopts::value<bool>(mHelp))
;
}
bool parseCMDLine(int argc, char* argv[]) {
if (!parse(argc, argv)) {
return false;
}
ensureConsistency();
return true;
}
int getSize() const {return mSize;}
bool isHexagonal() const {return mHexagonal;}
bool isSquare() const {return mSquare;}
bool isHelp() const {return mHelp;}
double getRatio() const {return mRatio;}
std::string helpMessage() const {return options.help({""});}
void print() const {
std::cout << "options = {"
<< "\n size: " << mSize
<< ",\n hexagonal: " << mHexagonal
<< ",\n square: " << mSquare
<< ",\n help: " << mHelp
<< "\n}" << std::endl;
}
};
class MapGenerator {
protected:
int mSize = 5;
double mRatio = 0.1;
public:
void setRatio(double ratio) {
if (ratio < 0 || ratio > 1.0) {
return;
}
mRatio = ratio;
}
void setSize(int size) {
if (size < 1) {
return;
}
mSize = size;
}
MapGenerator(int size, double ratio) {
setSize(size);
setRatio(ratio);
}
virtual std::vector<int> generate() = 0;
};
/*
Example hexagonal map for size == 1
(0,-1) (1,-1)
(-1,0) (0,0) (1, 0)
(-1,1) (0,1)
*/
class HexagonalMapGenerator : public MapGenerator {
public:
HexagonalMapGenerator(int size, double ratio): MapGenerator(size, ratio) {}
virtual std::vector<int> generate() override;
};
class SquareMapGenerator : public MapGenerator {
public:
SquareMapGenerator(int size, double ratio): MapGenerator(size, ratio) {}
virtual std::vector<int> generate() override;
};
std::unique_ptr<MapGenerator> generatorForCMDOptions(const CMDOptions& opts) {
if (opts.isHexagonal()) {
return std::unique_ptr<MapGenerator>(
new HexagonalMapGenerator(opts.getSize(), opts.getRatio())
);
}
if (opts.isSquare()) {
return std::unique_ptr<MapGenerator>(
new SquareMapGenerator(opts.getSize(), opts.getRatio())
);
}
throw cxxopts::OptionException("CMDOptions are not consistent.");
}
template <typename Sequence>
void print(const Sequence& sequence)
{
std::cout << "[";
bool notFirst = false;
for (const auto& s : sequence) {
if (notFirst) {
std::cout << ", ";
}
std::cout << s;
notFirst = true;
}
std::cout << "]\n";
}
std::vector<int> HexagonalMapGenerator::generate()
{
int mapSize = 3 * mSize * mSize + 3 * mSize + 1;
// generate (size - 2) num of blocks using ratio
// 2 blocks are reserved for the start and target position
std::vector<int> blocks(mapSize - 2);
for (int i = 0; i < mRatio * (mapSize - 2); ++i) {
blocks[i] = 1;
}
// randomize the blocks
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 m(seed);
std::shuffle(blocks.begin(), blocks.end(), m);
// enumerate coordinates where target can be
// 1/6 slice of a pizza
// where (x, y) x>0, y>=0
std::uniform_int_distribution<int> dist(1, (mSize * mSize + mSize) / 2);
auto dice = std::bind(dist, m);
int target = dice();
// assemble return vector:
// a) rand elements from array
// b) fixed source element (which is a free block)
// c) picked target element (which is a -1 block)
std::vector<int> ret;
ret.reserve(mapSize);
int countToTarget = 0;
for (int y = -mSize; y <= mSize; ++y) {
for (int x = -mSize; x <= mSize; ++x) {
int z = -x - y;
if (z > mSize || z < -mSize) {
// We are off the grid
continue;
}
if (x > 0 && y >= 0) {
++countToTarget;
if (countToTarget == target) {
ret.push_back(-1);
continue;
}
}
if (x == 0 && y == 0) {
ret.push_back(0);
continue;
}
ret.push_back(blocks.back());
blocks.pop_back();
}
}
return ret;
}
std::vector<int> SquareMapGenerator::generate()
{
return std::vector<int>();
}
int main(int argc, char* argv[]) {
CMDOptions opts;
if (!opts.parseCMDLine(argc, argv)) {
return 1;
}
if (opts.isHelp())
{
std::cout << opts.helpMessage() << std::endl;
return 0;
}
// opts.print();
auto gen = generatorForCMDOptions(opts);
auto map = gen->generate();
print(map);
}