diff options
author | zedarider <ymherklotz@gmail.com> | 2016-07-20 21:05:42 +0200 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-07-20 21:05:42 +0200 |
commit | 16fb95a1056df76c71af9d31bd05820a6d159aa0 (patch) | |
tree | a2b7200f53ce9d2384deab4bb48e3a33b0091823 | |
parent | 4d6b9a03d9de2d63c82d212fc745eb83aa03548c (diff) | |
download | Cardeval-16fb95a1056df76c71af9d31bd05820a6d159aa0.tar.gz Cardeval-16fb95a1056df76c71af9d31bd05820a6d159aa0.zip |
renamed all objects so that they are unified
-rw-r--r-- | include/cardeval.hpp | 34 | ||||
-rw-r--r-- | src/cardeval.cpp | 76 | ||||
-rw-r--r-- | src/main.cpp | 36 |
3 files changed, 89 insertions, 57 deletions
diff --git a/include/cardeval.hpp b/include/cardeval.hpp index 87493a6..5fc507e 100644 --- a/include/cardeval.hpp +++ b/include/cardeval.hpp @@ -20,11 +20,14 @@ #include <vector> #include <sstream> +// test include will be removed later +#include <iostream> + // external libraries #include <tinyxml2.h> // structure that stores card information -struct cardStruct { +struct card_struct { // booleans to be set to determine card bool is_collectible; @@ -43,7 +46,7 @@ struct cardStruct { std::string card_name; std::string class_name; - cardStruct() { + card_struct() { // initialising the variables is_collectible = false; @@ -62,6 +65,12 @@ struct cardStruct { card_name = ""; class_name = ""; } + + std::string card_print_format() { + std::stringstream ss; + ss << card_name << " (Rarity: " << rarity << "):\n\tCost: " << cost << ", Attack: " << attack << ", Health: " << health << ", Durability: " << durability << "\n\tClass: " << class_name << "\n\tAbilities: " << abilities << std::endl; + return ss.str(); + } }; class CardEval { @@ -75,21 +84,30 @@ public: ~CardEval(); // gets the cards from the xml file and saves them in memory for easy usability inside the program with all the information in a struct - void getCards(std::vector<cardStruct> &cardDeck); + void get_cards(); protected: // exits the program with an error void exit_with_error(); + // adds the card to a vector that it belongs to + void add_card_to_vec(card_struct &in_card); + + // gets the type of the card and returns the appropriate char char get_type(std::string type_str); + // gets the class of the card and returns the class name std::string get_class(std::string class_str); + // assigns the values to the card struct which can then be added to the vectors + card_struct set_card_info(tinyxml2::XMLElement* tag_it); private: + // variable that holds the xml document in memory tinyxml2::XMLDocument card_doc; // vectors that load all the card information into memory - std::vector<cardStruct> minions; - std::vector<cardStruct> spells; - std::vector<cardStruct> weapons; - std::vector<cardStruct> heroes; - std::vector<cardStruct> hero_powers; + std::vector<card_struct> minions; + std::vector<card_struct> spells; + std::vector<card_struct> weapons; + std::vector<card_struct> heroes; + std::vector<card_struct> hero_powers; + std::vector<card_struct> random; }; #endif diff --git a/src/cardeval.cpp b/src/cardeval.cpp index e1e6f5b..6c04d29 100644 --- a/src/cardeval.cpp +++ b/src/cardeval.cpp @@ -41,7 +41,7 @@ CardEval::~CardEval() { } -void CardEval::getCards(std::vector<cardStruct> &cardDeck) { +void CardEval::get_cards() { // load root node from the file into memory XMLElement* root_el = card_doc.FirstChildElement("CardDefs"); @@ -49,45 +49,49 @@ void CardEval::getCards(std::vector<cardStruct> &cardDeck) { for(XMLElement* ent_iterator = root_el->FirstChildElement("Entity"); ent_iterator != NULL; ent_iterator = ent_iterator->NextSiblingElement("Entity")) { - cardStruct card_info; + card_struct card_info; // iterates through the card information in tags for(XMLElement* tag_iterator = ent_iterator->FirstChildElement("Tag"); tag_iterator != NULL; tag_iterator = tag_iterator->NextSiblingElement("Tag")) { - // add information to the cardinfo - if(tag_iterator->Attribute("name", "CardName")) { - // if tag is cardname - card_info.card_name = tag_iterator->FirstChildElement("enUS")->GetText(); - - } else if(tag_iterator->Attribute("name", "Collectible") && - tag_iterator->Attribute("value", "1")) { - card_info.is_collectible = true; - - } else if(tag_iterator->Attribute("name", "CardType")) { - get_type(tag_iterator->Attribute("value")); - - } else if(tag_iterator->Attribute("name", "Class")) { - get_class(tag_iterator->Attribute("value")); - } + card_info = set_card_info(tag_iterator); } } } void CardEval::exit_with_error() { + // exit the program with an error message std::exit(EXIT_FAILURE); } +void CardEval::add_card_to_vec(card_struct &in_card) { + // just adds the card to the respective vector + if(in_card.card_type == 'm') { + minions.push_back(in_card); + } else if(in_card.card_type == 's') { + spells.push_back(in_card); + } else if(in_card.card_type == 'w') { + weapons.push_back(in_card); + } else { + random.push_back(in_card); + } +} + char CardEval::get_type(std::string type_str) { int type_int = std::stoi(type_str); switch(type_int) { + // when it is a minion case 4: return 'm'; + // when the card is a spell case 5: return 's'; + // when the card is a weapon case 7: return 'w'; + // otherwise return null default: return '\0'; } @@ -119,3 +123,41 @@ std::string CardEval::get_class(std::string class_str) { return ""; } } + +card_struct CardEval::set_card_info(XMLElement* tag_it) { + card_struct tmp_card; + + // add information to the cardinfo + if(tag_it->Attribute("name", "CardName")) { + // if tag is cardname + tmp_card.card_name = tag_it->FirstChildElement("enUS")->GetText(); + } else if(tag_it->Attribute("name", "Collectible") && + tag_it->Attribute("value", "1")) { + tmp_card.is_collectible = true; + + } else if(tag_it->Attribute("name", "CardType")) { + tmp_card.card_type = get_type(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Class")) { + tmp_card.class_name = get_class(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Cost")) { + tmp_card.cost = std::stoi(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Atk")) { + tmp_card.attack = std::stoi(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Health")) { + tmp_card.health = std::stoi(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Rarity")) { + tmp_card.rarity = std::stoi(tag_it->Attribute("value")); + + } else if(tag_it->Attribute("name", "Durability")) { + tmp_card.durability = std::stoi(tag_it->Attribute("value")); + } else{ + + } + + return tmp_card; +} diff --git a/src/main.cpp b/src/main.cpp index e855621..1335355 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,41 +13,13 @@ #include <tinyxml2.h> +#include "../include/cardeval.hpp" + using namespace std; using namespace tinyxml2; int main(int argc, char** argv) { - cout << "executing " << argv[0] << endl; - cout << "arguments given: " << argc-1 << endl; - - XMLDocument doc; - doc.LoadFile("resources/CardDefs.xml"); - - XMLElement* entityElement = doc.FirstChildElement("CardDefs"); - for(XMLElement* elIterator = entityElement->FirstChildElement("Entity"); elIterator != NULL; elIterator = elIterator->NextSiblingElement("Entity")) { - bool setCardName = false; - bool setCollectible = false; - bool setMinion = false; - bool written = false; - string cardName = ""; - for(XMLElement* tagIterator = elIterator->FirstChildElement("Tag"); tagIterator != NULL; tagIterator = tagIterator->NextSiblingElement("Tag")) { - if(tagIterator->Attribute("name", "CardName")) { - setCardName = true; - cardName = tagIterator->FirstChildElement("enUS")->GetText(); - } else if(tagIterator->Attribute("name", "Collectible") && tagIterator->Attribute("value", "1")) { - setCollectible = true; - } else if(tagIterator->Attribute("name", "CardType") && tagIterator->Attribute("value", "10")) { - setMinion = true; - } - - if(setCardName && setCollectible && setMinion && !written) { - cout << cardName << endl; - written = true; - } if(tagIterator == nullptr) { - - } - } - } - + CardEval ce(argc, argv); + ce.get_cards(); return 0; } |