-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNA.cpp
146 lines (110 loc) · 3.26 KB
/
DNA.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
#include "DNA.h"
DNA::DNA()
{
}
DNA::DNA(string InpDNA)
{
DNAString = InpDNA;
DNALength = DNAString.size();
SetCurrentPosition(DNAString.size() - 12);
changingChance = GetGeneInt(1, 80);
addingChance = GetGeneInt(1, 80);
removingChance = GetGeneInt(1, 80);
}
void DNA::GenerateRandomDNA(int size)
{
DNAString = "";
for (int i = 0; i < size; i++)
{
DNAString.append({ static_cast<char> ((rand() % UniqCharsInDNA) + 32) });
//DNAString.append({ static_cast<char> (94 + 32) });
}
DNALength = DNAString.size();
SetCurrentPosition(DNAString.size() - 12);
changingChance = GetGeneInt(1, 80);
addingChance = GetGeneInt(1, 80);
removingChance = GetGeneInt(1, 80);
}
DNA* DNA::CloneDNA()
{
string ClonedDNAString = "";
SetCurrentPosition(DNAString.size() - 12);
changingChance = GetGeneInt(1, 80);
addingChance = GetGeneInt(1, 80);
removingChance = GetGeneInt(1, 80);
for (char i : DNAString)
{
if (rand() % changingChance == 0)
{
ClonedDNAString.append({ static_cast<char> min(max(i + (rand() % 20) - 10, 32), UniqCharsInDNA + 32) });
}
if (rand() % addingChance == 0) // if (!rand() % addingChance) would have also been possible but i think this is more readable
{
ClonedDNAString.append({ i });
ClonedDNAString.append({ static_cast<char> ((rand() % UniqCharsInDNA) + 32) });
}
if (!rand() % removingChance == 0)
{
ClonedDNAString.append({ i });
}
}
return new DNA(ClonedDNAString);
}
float DNA::GetGeneFloat(float min, float max)
{
currentPos += 4;
return (((GetCharacter((currentPos - 4) % DNALength) + GetCharacter((currentPos - 3) % DNALength) + GetCharacter((currentPos - 2) % DNALength) + GetCharacter((currentPos - 1) % DNALength)) * (max - min)) / (UniqCharsInDNA * 4)) + min;
}
float DNA::GetGeneFloatFromSingleChar(float min, float max)
{
currentPos++;
return (GetCharacter((currentPos - 1) % DNALength) * (max - min) / UniqCharsInDNA) + min;
}
int DNA::GetGeneInt(int min, int max)
{
currentPos += 4;
return (((GetCharacter((currentPos - 4) % DNALength) + GetCharacter((currentPos - 3) % DNALength) + GetCharacter((currentPos - 2) % DNALength) + GetCharacter((currentPos - 1) % DNALength)) * (max - min)) / (UniqCharsInDNA * 4)) + min;
}
bool DNA::MutateDNAThroughPoison(float poison)
{
if (poison <= 0.0001)
return false;
if (poison >= 1000.0f)
return false;
SetCurrentPosition(DNAString.size() - 12);
string ClonedDNAString = "";
changingChance = GetGeneInt(1, 100);
addingChance = GetGeneInt(1, 100);
removingChance = GetGeneInt(1, 100);
bool change = false;
for (char i : DNAString)
{
if (rand() % max(1, static_cast<int>((changingChance * 10000.0f) / poison)) == 0)
{
ClonedDNAString.append({ static_cast<char> min(max(i + (rand() % 20) - 10, 32), UniqCharsInDNA + 32) });
change = true;
continue;
}
if (rand() % max(1, static_cast<int>((addingChance * 10000.0f) / poison)) == 0)
{
ClonedDNAString.append({ i });
ClonedDNAString.append({ static_cast<char> ((rand() % UniqCharsInDNA) + 32) });
change = true;
continue;
}
if (rand() % max(1, static_cast<int>((removingChance * 10000.0f) / poison)) == 0)
{
change = true;
}
else
{
ClonedDNAString.append({ i });
}
}
DNAString = ClonedDNAString;
DNALength = ClonedDNAString.size();
return change;
}
DNA::~DNA()
{
}