-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.cpp
142 lines (124 loc) · 3.52 KB
/
Factory.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
/**
* Factory.cpp
*
* Created on: Aug 2, 2011
* Author: Jeroen Vlek
* Email: [email protected]
* Website: www.perceptivebits.com
* License: Free Beer (Feel free to use it in every
* way possible and if you like it, make
* sure to give me credit and buy me a drink
* if we ever meet ;) )
*/
#include "Factory.h"
#include "QtFactory.h"
#include <ctime>
#include <iostream>
Factory::Factory()
: m_gen(boost::rand48(std::time(0))),
m_randPrimary(
new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned char> >(
m_gen,
boost::uniform_int<unsigned char>(0, 255)
)
),
m_randX(
new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(0, 0))
),
m_randY(
new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(0, 0))
),
m_randGeneSize(
new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(
Config::GetMinGeneSize(),
Config::GetMaxGeneSize()
)
)
),
m_randGenomeIndex(
new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(
0,
Config::GetGenomeSize() - 1
)
)
) {
}
Factory::~Factory() {
delete m_randPrimary;
delete m_randX;
delete m_randY;
delete m_randGeneSize;
delete m_randGenomeIndex;
}
Factory& Factory::Instance() {
#ifdef QT_YES
static QtFactory factory;
return factory;
#endif
}
void Factory::update() {
// changing random number generators
if(Config::GetWidth() > 0) {
delete m_randX;
m_randX = new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen, boost::uniform_int<unsigned int>(0, Config::GetWidth() - 1));
}
if(Config::GetHeight() > 0) {
delete m_randY;
m_randY = new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen, boost::uniform_int<unsigned int>(0, Config::GetHeight() - 1));
}
delete m_randGeneSize;
m_randGeneSize = new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(
Config::GetMinGeneSize(),
Config::GetMaxGeneSize()
)
);
delete m_randGenomeIndex;
m_randGenomeIndex = new boost::variate_generator<boost::rand48&, boost::uniform_int<unsigned int> >(
m_gen,
boost::uniform_int<unsigned int>(
0,
Config::GetGenomeSize() - 1
)
);
std::cout << "[ Factory::update() ] Updated factory" << std::endl;
}
unsigned int Factory::randX() {
return (*m_randX)();
}
unsigned int Factory::randGeneSize() {
return (*m_randGeneSize)();
}
unsigned int Factory::randY() {
return (*m_randY)();
}
unsigned char Factory::randPrimary() {
return (*m_randPrimary)();
}
unsigned int Factory::randGenomeIndex() {
return (*m_randGenomeIndex)();
}
Gene Factory::makeRandomGene() {
PIXEL color;
color.r = randPrimary();
color.g = randPrimary();
color.b = randPrimary();
color.a = randPrimary();
unsigned int numPoints = randGeneSize();
std::vector<std::pair<int, int> > points(numPoints);
for(unsigned int i = 0; i < numPoints; ++i) {
points.push_back(std::pair<int, int>(randX(), randY()));
}
return Gene(color, points);
}