Skip to content
Snippets Groups Projects
Commit 3bafdb1a authored by Jake Feddersen's avatar Jake Feddersen
Browse files

Work on assignment 1.07

parent fff4c073
No related branches found
No related tags found
No related merge requests found
...@@ -8,9 +8,19 @@ ...@@ -8,9 +8,19 @@
using namespace std; using namespace std;
monster_desc::monster_desc() { } monster_desc::monster_desc() {
speed = nullptr;
hitpoints = nullptr;
attack_damage = nullptr;
}
monster_desc::~monster_desc() {
if (speed) delete speed;
if (hitpoints) delete hitpoints;
if (attack_damage) delete attack_damage;
}
vector<monster_desc> monster_desc::parse_monster_desc_file() { vector<monster_desc *> monster_desc::parse_monster_desc_file() {
char *home = getenv("HOME"); char *home = getenv("HOME");
char *path; char *path;
...@@ -22,6 +32,8 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() { ...@@ -22,6 +32,8 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
ifstream f; ifstream f;
f.open(path); f.open(path);
free(path);
string line; string line;
getline(f, line); getline(f, line);
if (line != "RLG327 MONSTER DESCRIPTION 1") { if (line != "RLG327 MONSTER DESCRIPTION 1") {
...@@ -29,7 +41,7 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() { ...@@ -29,7 +41,7 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
exit(-1); exit(-1);
} }
vector<monster_desc> descriptions; vector<monster_desc *> descriptions;
while (true) { while (true) {
bool eof = false; bool eof = false;
...@@ -43,14 +55,31 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() { ...@@ -43,14 +55,31 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
} }
if (eof) break; if (eof) break;
monster_desc m; monster_desc *m = new monster_desc;
bool fields[9];
for (int i = 0; i < 9; i++) {
fields[i] = false;
}
bool valid = true;
while (true) { while (true) {
getline(f, line); getline(f, line);
if (line == "END") { if (line == "END") {
descriptions.push_back(m); // Check that all fields are satisfied
break; bool valid = true;
for (int i = 0; i < 9; i++) {
if (!fields[i]) valid = false;
}
if (valid) {
descriptions.push_back(m);
break;
} else {
delete(m);
break;
}
} }
//bool valid = true; //bool valid = true;
...@@ -60,52 +89,111 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() { ...@@ -60,52 +89,111 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
sline >> token; sline >> token;
if (token == "NAME") { if (token == "NAME") {
if (fields[0]) valid = false;
fields[0] = true;
// Get rid of the space // Get rid of the space
sline.get(); sline.get();
getline(sline, m.name); getline(sline, m->name);
} }
else if (token == "DESC") { else if (token == "DESC") {
bool firstline = true; if (fields[1]) valid = false;
while(true) { else {
getline(f, line); fields[1] = true;
if (line == ".") break; bool firstline = true;
if (!firstline) { while(true) {
m.description += "\n"; getline(f, line);
} else { if (line == ".") break;
firstline = false; if (!firstline) {
m->description += "\n";
} else {
firstline = false;
}
m->description += line;
} }
m.description += line;
} }
} }
else if (token == "SYMB") { else if (token == "SYMB") {
sline >> m.symbol; if (fields[2]) valid = false;
else {
fields[2] = true;
sline >> m->symbol;
}
} }
else if (token == "RRTY") { else if (token == "RRTY") {
sline >> m.rarity; if (fields[3]) valid = false;
else {
fields[3] = true;
sline >> m->rarity;
}
} }
else if (token == "SPEED") { else if (token == "SPEED") {
string diceValue; if (fields[4]) valid = false;
sline >> diceValue; else {
m.speed = new dice(diceValue); fields[4] = true;
string diceValue;
sline >> diceValue;
int b, d, s;
if (sscanf(diceValue.c_str(), "%d+%dd%d", &b, &d, &s) != 3) valid = false;
m->speed = new dice(b, d, s);
}
} }
else if (token == "HP") { else if (token == "HP") {
string hpValue; if (fields[5]) valid = false;
sline >> hpValue; else {
m.hitpoints = new dice(hpValue); fields[5] = true;
string diceValue;
sline >> diceValue;
int b, d, s;
if (sscanf(diceValue.c_str(), "%d+%dd%d", &b, &d, &s) != 3) break;
m->hitpoints = new dice(b, d, s);
}
} }
else if (token == "DAM") { else if (token == "DAM") {
string damageValue; if (fields[6]) valid = false;
sline >> damageValue; else {
m.attack_damage = new dice(damageValue); fields[6] = true;
string diceValue;
sline >> diceValue;
int b, d, s;
if (sscanf(diceValue.c_str(), "%d+%dd%d", &b, &d, &s) != 3) break;
m->attack_damage = new dice(b, d, s);
}
} }
//else break; else if (token == "COLOR") {
if (fields[7]) valid = false;
else {
fields[7] = true;
string color;
while(sline >> color) {
m->colors.push_back(color);
}
}
}
else if (token == "ABIL") {
if (fields[8]) break;
else {
fields[8] = true;
string abil;
while(sline >> abil) {
m->abilities.push_back(abil);
}
}
}
else valid = false;
if (!valid) {
delete(m);
break;
}
} }
} }
...@@ -114,15 +202,29 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() { ...@@ -114,15 +202,29 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
return descriptions; return descriptions;
} }
void monster_desc::print_monster_desc(vector<monster_desc> descriptions) { void monster_desc::print_monster_desc(vector<monster_desc *> descriptions) {
for (unsigned int i = 0; i < descriptions.size(); i++) { for (unsigned int i = 0; i < descriptions.size(); i++) {
if (i != 0) cout << endl; if (i != 0) cout << endl;
cout << descriptions[i].name << endl; cout << descriptions[i]->name << endl;
cout << descriptions[i].description << endl; cout << descriptions[i]->description << endl;
cout << descriptions[i].symbol << endl; cout << descriptions[i]->symbol << endl;
descriptions[i].speed->print();
descriptions[i].hitpoints->print(); for (unsigned int j = 0; j < descriptions[i]->colors.size(); j++) {
descriptions[i].attack_damage->print(); if (j != 0) cout << " ";
cout << descriptions[i].rarity << endl; cout << descriptions[i]->colors[j];
}
cout << endl;
descriptions[i]->speed->print();
for (unsigned int j = 0; j < descriptions[i]->abilities.size(); j++) {
if (j != 0) cout << " ";
cout << descriptions[i]->abilities[j];
}
cout << endl;
descriptions[i]->hitpoints->print();
descriptions[i]->attack_damage->print();
cout << descriptions[i]->rarity << endl;
} }
} }
...@@ -11,9 +11,9 @@ class monster_desc { ...@@ -11,9 +11,9 @@ class monster_desc {
private: private:
std::string name; std::string name;
std::string description; std::string description;
std::vector<int> color; std::vector<std::string> colors;
dice *speed; dice *speed;
std::vector<int> abilities; std::vector<std::string> abilities;
dice *hitpoints; dice *hitpoints;
dice *attack_damage; dice *attack_damage;
char symbol; char symbol;
...@@ -21,9 +21,10 @@ class monster_desc { ...@@ -21,9 +21,10 @@ class monster_desc {
public: public:
monster_desc(); monster_desc();
~monster_desc();
static std::vector<monster_desc> parse_monster_desc_file(); static std::vector<monster_desc *> parse_monster_desc_file();
static void print_monster_desc(std::vector<monster_desc> descriptions); static void print_monster_desc(std::vector<monster_desc *> descriptions);
}; };
#endif #endif
\ No newline at end of file
...@@ -27,9 +27,13 @@ void regen_dungeon(dungeon &d, heap_t *h, int nummon) { ...@@ -27,9 +27,13 @@ void regen_dungeon(dungeon &d, heap_t *h, int nummon) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::vector<monster_desc> monster_descriptions = monster_desc::parse_monster_desc_file(); std::vector<monster_desc *> monster_descriptions = monster_desc::parse_monster_desc_file();
monster_desc::print_monster_desc(monster_descriptions); monster_desc::print_monster_desc(monster_descriptions);
for (unsigned int i = 0; i < monster_descriptions.size(); i++) {
delete monster_descriptions[i];
}
return 0; return 0;
int i; int i;
......
...@@ -22,8 +22,10 @@ int sign(int x) { ...@@ -22,8 +22,10 @@ int sign(int x) {
return 0; return 0;
} }
dice::dice(std::string &diceValue) { dice::dice(int b, int d, int s) {
sscanf(diceValue.c_str(), "%d+%dd%d", &base, &num_dice, &sides); base = b;
num_dice = d;
sides = s;
} }
void dice::print() { void dice::print() {
......
...@@ -55,7 +55,7 @@ class dice { ...@@ -55,7 +55,7 @@ class dice {
int sides; int sides;
public: public:
dice(std::string &diceValue); dice(int b, int d, int s);
void print(); void print();
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment