-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
165 lines (157 loc) · 4.61 KB
/
Parser.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
/* Gregoire DUCHARME */
/* Yixuan Zhang */
/* 23/10/2016 */
#include "Parser.hh"
/* Check if the string is only numeric char */
bool Parser::isNumeric(char *str) const
{
for (unsigned int i; i < strlen(str); i++) {
if (str[i] < '0' || str[i] > '9')
return false;
}
return true;
}
void Parser::initGenerationAlgorithm(char *argv)
{
if (!ARG_G_SEED.compare(argv))
new_maze = true;
else if (!ARG_GA_SEED.compare(argv))
new_maze_ga = true;
else if (!ARG_GE_SEED.compare(argv))
new_maze_ge = true;
}
void Parser::checkSolvingFlag(char *argv)
{
if (!ARG_SOLVE_PM.compare(argv)) {
solving = Maze::PM;
}
else if (!ARG_SOLVE_PE.compare(argv)) {
solving = Maze::PE;
}
else if (!ARG_SOLVE_PB.compare(argv)) {
solving = Maze::PB;
}
else if (!ARG_SOLVE_PD.compare(argv)) {
solving = Maze::PD;
}
}
/* Only constructor of the Parser class */
/* Usage of const char defined in Parser.hh */
Parser::Parser(int ac, char **argv)
{
load_bin = false;
new_maze = false;
new_maze_ga = false;
new_maze_ge = false;
save_bin = false;
save_svg = false;
seed = 0;
width = 0;
height = 0;
solving = Maze::NONE;
for (int i = 0; i < ac; i++) {
if (!ARG_LOAD_BINARY.compare(argv[i]) && ac > i + 1) {
load_bin = true;
load_bin_file = argv[i + 1];
}
else if (!ARG_SAVE_BINARY.compare(argv[i]) && ac > i + 1) {
save_bin = true;
save_bin_file = argv[i + 1];
}
else if (!ARG_SAVE_SVG.compare(argv[i]) && ac > i + 1) {
save_svg = true;
save_svg_file = argv[i + 1];
}
else if ((!ARG_G_SEED.compare(argv[i]) ||
!ARG_GA_SEED.compare(argv[i]) ||
!ARG_GE_SEED.compare(argv[i])) &&
ac > i + 2) {
/* Check if a seed is specified */
if (ac > i + 3 && isNumeric(argv[i + 1]) &&
isNumeric(argv[i + 2]) &&
isNumeric(argv[i + 3])) {
seed = std::stoi(argv[i + 1]);
width = std::stoi(argv[i + 2]);
height = std::stoi(argv[i + 3]);
initGenerationAlgorithm(argv[i]);
}
/* Else the seed is set to time(0) */
else if (isNumeric(argv[i + 1]) &&
isNumeric(argv[i + 2])) {
seed = time(0);
width = std::stoi(argv[i + 1]);
height = std::stoi(argv[i + 2]);
initGenerationAlgorithm(argv[i]);
}
}
checkSolvingFlag(argv[i]);
}
}
bool Parser::dispatchNewMaze()
{
std::cout << "Generating new maze with seed "<< seed << std ::endl;
if (new_maze || new_maze_ga)
return (maze.loadNewMaze(seed, width, height, Maze::ALDOUS_BRODER));
else if (new_maze_ge)
return(maze.loadNewMaze(seed, width, height, Maze::ELLER));
return false;
}
bool Parser::run()
{
// Check if parameter are correct
if (load_bin && !new_maze && !new_maze_ga && !new_maze_ge) {
std::cout << "Loading binary file "<< load_bin_file << std ::endl;
if (!maze.loadFromBinaryFile(load_bin_file)) {
std::cerr << "Could not load from binary file " << load_bin_file << std::endl;
return false;
}
}
else if (((new_maze && !new_maze_ga && !new_maze_ge) ||
(!new_maze && new_maze_ga && !new_maze_ge) ||
(!new_maze && !new_maze_ga && new_maze_ge)) &&
!load_bin) {
if (!dispatchNewMaze()) {
std::cerr << "Could not generate new file " << load_bin_file << std::endl;
return false;
}
}
else {
std::cerr << "Error, maze not specified" << std::endl;
std::cerr << "Use "<< ARG_G_SEED << " to generate one" << std::endl;
std::cerr << "Or use "<< ARG_GE_SEED << " to generate one" << std::endl;
std::cerr << "Or use "<< ARG_GA_SEED << " to generate one" << std::endl;
std::cerr << "Or " << ARG_LOAD_BINARY <<" to load from binary file" << std::endl;
return false;
}
if (maze.isValid()) {
if (save_bin) {
std::cout << "Saving binary file"<< save_bin_file << std ::endl;
if (!maze.saveToBinaryFile(save_bin_file)) {
std::cerr << "Could not save to binary file " << save_svg_file << std::endl;
}
}
if (save_svg) {
std::cout << "Saving svg file "<< save_svg_file << std ::endl;
if (!maze.saveToSvgFile(save_svg_file)) {
std::cerr << "Could not save to svg file " << save_svg_file << std::endl;
}
}
if (solving != Maze::NONE) {
auto start = std::chrono::steady_clock::now();
if (maze.solveMaze(solving)) {
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast
<std::chrono::milliseconds>(end - start);
std::cout << "Maze solved in "<< duration.count() << "ms" << std::endl;
}
else {
std::cerr << "Error, could not solve given maze" << std::endl;
}
}
}
else {
std::cerr << "Invalid maze" <<std::endl;
return false;
}
return true;
}