-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInput.cpp
142 lines (122 loc) · 3.79 KB
/
Input.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
/*
* File: Input.cpp
* Author: Tomáš Čerevka, Adam Činčura
*
* Created on November 2, 2011, 10:28 AM
*/
#include <string.h>
#include <stdlib.h>
#include "Input.h"
Input::Input(Board* _board) {
board = _board;
}
Input::~Input() {
}
void Input::parseArguments(int _count, char** _args) {
if (_count < 2) {
throw "To few arguments!";
}
bool helpSwitch = false;
bool tokensCountSwitch = false;
bool towersCountSwitch = false;
bool targetTowerSwitch = false;
bool maxDepthSwitch = false;
bool fileNameSwitch = false;
for (int i = 1; i < _count; i = i + 2) {
if (strcmp(_args[i], "-h") == 0) {
// Napoveda.
helpSwitch = true;
printHelp();
} else if (strcmp(_args[i], "-n") == 0) {
// Pocet tokenu.
tokensCount = atoi(_args[i + 1]);
tokensCountSwitch = true;
} else if (strcmp(_args[i], "-s") == 0) {
// Pocet vezi.
towersCount = atoi(_args[i + 1]);
towersCountSwitch = true;
} else if (strcmp(_args[i], "-r") == 0) {
// Cilova vez.
targetTower = atoi(_args[i + 1]);
targetTowerSwitch = true;
} else if (strcmp(_args[i], "-d") == 0) {
// Maximalni hloubka.
maxDepth = atoi(_args[i + 1]);
maxDepthSwitch = true;
} else if (strcmp(_args[i], "-f") == 0) {
fileName = _args[i + 1];
fileNameSwitch = true;
} else {
throw "Unknown arguments. Use -h for help.";
}
}
if (helpSwitch == false) {
if (fileNameSwitch == true) {
// Data se nacitai ze souboru.
parseFile();
} else {
// Data se generuji.
if (tokensCountSwitch && towersCountSwitch && targetTowerSwitch && maxDepthSwitch) {
generateBoard();
} else {
throw "Incomplete input!";
}
}
// printTask(); //chci aby to vypsal jen master
}
}
void Input::generateBoard() {
// Inicilizuje veze na desce.
board->init(towersCount);
// Inicializuje randomizer.
srand(time(NULL));
for (int i = tokensCount; i > 0; --i) {
// Nahodne se urci vez.
int tower = rand() % towersCount;
board->addTowerTop(tower, i);
}
}
void Input::parseFile() {
ifstream file(fileName.c_str());
string line;
getline(file, line);
stringstream config(line);
// Pocet vezi, cilova vez, omezeni hloubky.
config >> towersCount >> targetTower >> maxDepth;
// Inicializuje se deska.
board->init(towersCount);
// Na jednotlive veze se nasazi tokeny.
for (int i = 0; i < towersCount; ++i) {
getline(file, line);
stringstream tokens(line);
while (tokens.good()) {
int token;
tokens >> token;
board->addTowerTop(i, token);
}
}
file.close();
}
void Input::printTask() const {
cout << "Task:" << endl;
if (fileName.empty()) {
cout << "Number of tokens: " << tokensCount << endl;
} else {
cout << "Loaded from file: " << fileName << endl;
}
cout << "Number of towers: " << towersCount << endl;
cout << "Target tower: " << targetTower << endl;
cout << "Max depth: " << maxDepth << endl;
cout << "Board:" << endl;
cout << (*board) << endl;
}
void Input::printHelp() const {
cout << "Help:" << endl;
cout << "A) From file:" << endl;
cout << " -f path to file" << endl;
cout << "B) Generate:" << endl;
cout << " -s number of towers" << endl;
cout << " -n number of tokens" << endl;
cout << " -r target tower" << endl;
cout << " -d max depth" << endl;
}