-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColony.cpp
136 lines (111 loc) · 2.83 KB
/
Colony.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
/***
* @file Colony.cpp
* Ant colony implementation
*/
// Copyright 2019 by Julio Albornoz <[email protected]>
// The License.txt file describes the conditions under which this software may be distributed.
#include "Colony.h"
#include <sstream>
Colony::Colony(Config* config, Tile* tile){
screen[0] = config->GWidth;
screen[1] = config->GHeight;
def_rule = config->def_rule;
sym = config->symbols;
length = config->length;
save = config->Rsave;
Ttype = tile;
if (def_rule.compare("RANDOM") == 0){
def_rule = random_ruleset();
}
load_ants();
}
Colony::~Colony(){
delete ants;
}
void Colony::load_ants(std::string path){
/***
* Loads ant data from a txt file
*
* @param path: File path to be used
*/
std::ifstream file(path); //File content
std::string line;
std::vector<Ant>* insects = new std::vector<Ant>;
while (std::getline(file, line)){ //Read entire line
std::stringstream linestream(line);
std::string buffer;
int X, Y, dir; // Move parameters to an Ant factory
//X position
std::getline(linestream, buffer, '\t'); //Refactor this block
if (buffer.compare("R") == 0){
X = rand() % (screen[0] - 1) + 1;
}
else{
X = std::stoi(buffer);
}
buffer.clear();
//Y position
std::getline(linestream, buffer, '\t'); //Refactor this block
if (buffer.compare("R") == 0){
Y = rand() % (screen[1] - 1) + 1;
}
else{
Y = std::stoi(buffer);
}
buffer.clear();
//Ant orientation
std::getline(linestream, buffer, '\t'); //Refactor this block
if (buffer.compare("R") == 0){
int max = Ttype->directions.size();
dir = rand() % max;
}
else{
dir = std::stoi(buffer);
}
buffer.clear();
//Ant ruleset
if (linestream.eof()){ //Uses default ruleset
buffer = def_rule;
}
std::getline(linestream, buffer); //4th column
if (buffer.compare("RANDOM") == 0){
buffer = random_ruleset();
}
// Initializes Ant
Ant bug = Ant(X * Ttype->X, Y * Ttype->Y, dir, buffer, Ttype);
insects->push_back(bug);
}
ants = insects;
file.close();
if (save){
save_ants("save/ants.txt");
}
}
void Colony::save_ants(std::string path){
/***
* Saves data of generated ants in a backup txt file
*
* @param path: Location of the backu
*/
std::ofstream file(path); //File content
std::vector<Ant> bugs = *ants;
for (unsigned int i = 0; i < bugs.size(); i++){
Ant ant = bugs[i];
file << ant.pos[0]/Ttype->X << "\t" << ant.pos[1]/Ttype->Y << "\t" << ant.dir << "\t" << ant.ruleset << "\n";
}
file.close();
}
std::string Colony::random_ruleset(){
/***
* Generates a random set of instructions from the given specifications
*
* @return String containing the new ruleset
*
*/
std::string Rrule;
for (unsigned int i = 0; i < length; i++){
int ch = rand() % sym.size();
Rrule += sym[ch];
}
return Rrule;
}