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 @@
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 *path;
......@@ -22,6 +32,8 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
ifstream f;
f.open(path);
free(path);
string line;
getline(f, line);
if (line != "RLG327 MONSTER DESCRIPTION 1") {
......@@ -29,7 +41,7 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
exit(-1);
}
vector<monster_desc> descriptions;
vector<monster_desc *> descriptions;
while (true) {
bool eof = false;
......@@ -43,14 +55,31 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
}
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) {
getline(f, line);
if (line == "END") {
descriptions.push_back(m);
break;
// Check that all fields are satisfied
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;
......@@ -60,52 +89,111 @@ vector<monster_desc> monster_desc::parse_monster_desc_file() {
sline >> token;
if (token == "NAME") {
if (fields[0]) valid = false;
fields[0] = true;
// Get rid of the space
sline.get();
getline(sline, m.name);
getline(sline, m->name);
}
else if (token == "DESC") {
bool firstline = true;
while(true) {
getline(f, line);
if (line == ".") break;
if (!firstline) {
m.description += "\n";
} else {
firstline = false;
if (fields[1]) valid = false;
else {
fields[1] = true;
bool firstline = true;
while(true) {
getline(f, line);
if (line == ".") break;
if (!firstline) {
m->description += "\n";
} else {
firstline = false;
}
m->description += line;
}
m.description += line;
}
}
else if (token == "SYMB") {
sline >> m.symbol;
if (fields[2]) valid = false;
else {
fields[2] = true;
sline >> m->symbol;
}
}
else if (token == "RRTY") {
sline >> m.rarity;
if (fields[3]) valid = false;
else {
fields[3] = true;
sline >> m->rarity;
}
}
else if (token == "SPEED") {
string diceValue;
sline >> diceValue;
m.speed = new dice(diceValue);
if (fields[4]) valid = false;
else {
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") {
string hpValue;
sline >> hpValue;
m.hitpoints = new dice(hpValue);
if (fields[5]) valid = false;
else {
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") {
string damageValue;
sline >> damageValue;
m.attack_damage = new dice(damageValue);
if (fields[6]) valid = false;
else {
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() {
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++) {
if (i != 0) cout << endl;
cout << descriptions[i].name << endl;
cout << descriptions[i].description << endl;
cout << descriptions[i].symbol << endl;
descriptions[i].speed->print();
descriptions[i].hitpoints->print();
descriptions[i].attack_damage->print();
cout << descriptions[i].rarity << endl;
cout << descriptions[i]->name << endl;
cout << descriptions[i]->description << endl;
cout << descriptions[i]->symbol << endl;
for (unsigned int j = 0; j < descriptions[i]->colors.size(); j++) {
if (j != 0) cout << " ";
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 {
private:
std::string name;
std::string description;
std::vector<int> color;
std::vector<std::string> colors;
dice *speed;
std::vector<int> abilities;
std::vector<std::string> abilities;
dice *hitpoints;
dice *attack_damage;
char symbol;
......@@ -21,9 +21,10 @@ class monster_desc {
public:
monster_desc();
~monster_desc();
static std::vector<monster_desc> parse_monster_desc_file();
static void print_monster_desc(std::vector<monster_desc> descriptions);
static std::vector<monster_desc *> parse_monster_desc_file();
static void print_monster_desc(std::vector<monster_desc *> descriptions);
};
#endif
\ No newline at end of file
......@@ -27,9 +27,13 @@ void regen_dungeon(dungeon &d, heap_t *h, int nummon) {
}
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);
for (unsigned int i = 0; i < monster_descriptions.size(); i++) {
delete monster_descriptions[i];
}
return 0;
int i;
......
......@@ -22,8 +22,10 @@ int sign(int x) {
return 0;
}
dice::dice(std::string &diceValue) {
sscanf(diceValue.c_str(), "%d+%dd%d", &base, &num_dice, &sides);
dice::dice(int b, int d, int s) {
base = b;
num_dice = d;
sides = s;
}
void dice::print() {
......
......@@ -55,7 +55,7 @@ class dice {
int sides;
public:
dice(std::string &diceValue);
dice(int b, int d, int s);
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