aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-10-31 13:00:26 +0000
committerzedarider <ymherklotz@gmail.com>2016-10-31 13:00:26 +0000
commitabd4894e31a573a7b6e9dbcbecb61a8b8b78ec8b (patch)
treedf400eb9a5ca0017d4a458009f04f16ac89d2a24
parent16fb95a1056df76c71af9d31bd05820a6d159aa0 (diff)
downloadCardeval-abd4894e31a573a7b6e9dbcbecb61a8b8b78ec8b.tar.gz
Cardeval-abd4894e31a573a7b6e9dbcbecb61a8b8b78ec8b.zip
adding all necessary files
-rw-r--r--Makefile4
-rw-r--r--include/cardeval.hpp57
-rw-r--r--src/cardeval.cpp151
-rw-r--r--src/main.cpp34
-rw-r--r--src/textanalysis.cpp38
5 files changed, 251 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index b1386f5..74d96a7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC := g++ # this is the main compiler
-# CC := clange --analyze # and comment out the linker last line
+# CC := clang --analyze # and comment out the linker last line
SRCDIR := src
BUILDDIR := build
TARGETDIR := bin
@@ -8,7 +8,7 @@ TARGET := bin/CardEval
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)")
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
-CFLAGS := -g -std=c++14 # -Wall
+CFLAGS := -g -std=c++14 -Wall
LIBDIR := -L/usr/local/lib64/
LIB := -ltinyxml2
INC := -Iinclude/
diff --git a/include/cardeval.hpp b/include/cardeval.hpp
index 5fc507e..832047b 100644
--- a/include/cardeval.hpp
+++ b/include/cardeval.hpp
@@ -13,6 +13,8 @@
#ifndef CARDEVAL_INCL
#define CARDEVAL_INCL
+#define MAX_MANA 10
+
// necessary standard includes
#include <cstdio>
#include <cstdlib>
@@ -26,6 +28,24 @@
// external libraries
#include <tinyxml2.h>
+namespace cardeval {
+
+struct atk_health {
+ double atk;
+ double health;
+
+ atk_health() {
+ atk = 0;
+ health = 0;
+ }
+
+ std::string prnt_str() {
+ std::stringstream ss;
+ ss << "(" << atk << ", " << health << ")";
+ return ss.str();
+ }
+};
+
// structure that stores card information
struct card_struct {
// booleans to be set to determine card
@@ -41,6 +61,8 @@ struct card_struct {
int health;
int rarity;
+ double value;
+
// extra card abilities such as charge or battlecry
std::string abilities;
std::string card_name;
@@ -60,15 +82,18 @@ struct card_struct {
health = -1;
rarity = -1;
+ value = -1;
+
// empty strings
abilities = "";
card_name = "";
- class_name = "";
+ class_name = "neutral";
}
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;
+ ss << card_name << " (Rarity: " << rarity << "):\n\tCost: " << cost << ", Attack: " << attack << ", Health: " << health << ", Durability: " << durability << ", Value: " << value << "\n\tClass: " << class_name << "\n\tAbilities: " << abilities << std::endl;
return ss.str();
}
};
@@ -83,9 +108,16 @@ public:
// default deconstructor
~CardEval();
+ void print_cards(std::string card_str);
+ void print_stat_average();
+ void set_minions(std::vector<card_struct> &card_info);
+ void set_spells(std::vector<card_struct> &card_info);
+ void set_weapons(std::vector<card_struct> &card_info);
+ void set_avg_stats(std::vector<atk_health> &stat_avg);
+protected:
// 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 get_cards();
-protected:
+ void get_stat_average();
// exits the program with an error
void exit_with_error();
// adds the card to a vector that it belongs to
@@ -96,7 +128,7 @@ protected:
// 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);
+ void set_card_info(tinyxml2::XMLElement* tag_it, card_struct &curr_card);
private:
// variable that holds the xml document in memory
tinyxml2::XMLDocument card_doc;
@@ -108,6 +140,21 @@ private:
std::vector<card_struct> heroes;
std::vector<card_struct> hero_powers;
std::vector<card_struct> random;
-};
+
+ // contains all the averages of the card stats
+ std::vector<atk_health> card_avg_stats;
+}; // Cardeval
+
+class TextAnalysis {
+public:
+ TextAnalysis();
+ ~TextAnalysis();
+
+ void get_limited_text(std::string &lim_text, std::string &text_limiter, std::string &text_limiter_close);
+protected:
+private:
+}; // TextAnalysis
+
+} // cardeval
#endif
diff --git a/src/cardeval.cpp b/src/cardeval.cpp
index 6c04d29..c3ff7b9 100644
--- a/src/cardeval.cpp
+++ b/src/cardeval.cpp
@@ -12,6 +12,14 @@
#include "../include/cardeval.hpp"
using namespace tinyxml2;
+using namespace std;
+using namespace cardeval;
+
+/*
+
+ Constructor
+
+ */
CardEval::CardEval() {
card_doc.LoadFile("resources/CardDefs.xml");
@@ -21,10 +29,10 @@ CardEval::CardEval(int &argc, char** &argv) {
// if only one argument is given
if(argc == 1) {
// use strncmp to compare parts of the string
- if(std::strncmp(argv[0], "bin/xxx", 4) || std::strncmp(argv[0], "./bin/xxx", 6)) {
+ if(strncmp(argv[0], "bin/xxx", 4) || strncmp(argv[0], "./bin/xxx", 6)) {
// if program is executed from main program directory
card_doc.LoadFile("resources/CardDefs.xml");
- } else if(std::strncmp(argv[0], "./xxx", 2)) {
+ } else if(strncmp(argv[0], "./xxx", 2)) {
// when program is executed directly
card_doc.LoadFile("../resources/CardDefs.xml");
}
@@ -35,12 +43,75 @@ CardEval::CardEval(int &argc, char** &argv) {
// otherwise exit with errorcode
exit_with_error();
}
+
+ get_cards();
+ get_stat_average();
}
CardEval::~CardEval() {
}
+/*
+
+ Public
+
+ */
+
+void CardEval::print_cards(string card_str) {
+ if(card_str == "minions") {
+ for(card_struct curr_card : minions) {
+ cout << curr_card.card_print_format();
+ }
+ } else if(card_str == "spells") {
+ for(card_struct curr_card : spells) {
+ cout << curr_card.card_print_format();
+ }
+ } else if(card_str == "weapons") {
+ for(card_struct curr_card : weapons) {
+ cout << curr_card.card_print_format();
+ }
+ } else {
+ cout << "not available" << endl;
+ }
+}
+
+void CardEval::print_stat_average() {
+ for(atk_health card_avg : card_avg_stats) {
+ cout << card_avg.prnt_str() << endl;
+ }
+}
+
+void CardEval::set_minions(vector<card_struct> &card_info) {
+ for(card_struct cards : minions) {
+ card_info.push_back(cards);
+ }
+}
+
+void CardEval::set_spells(vector<card_struct> &card_info) {
+ for(card_struct cards : spells) {
+ card_info.push_back(cards);
+ }
+}
+
+void CardEval::set_weapons(vector<card_struct> &card_info) {
+ for(card_struct cards : weapons) {
+ card_info.push_back(cards);
+ }
+}
+
+void CardEval::set_avg_stats(vector<atk_health> &stat_avg) {
+ for(atk_health stats : card_avg_stats) {
+ stat_avg.push_back(stats);
+ }
+}
+
+/*
+
+ Protected
+
+ */
+
void CardEval::get_cards() {
// load root node from the file into memory
XMLElement* root_el = card_doc.FirstChildElement("CardDefs");
@@ -55,14 +126,50 @@ void CardEval::get_cards() {
for(XMLElement* tag_iterator = ent_iterator->FirstChildElement("Tag");
tag_iterator != NULL; tag_iterator = tag_iterator->NextSiblingElement("Tag")) {
- card_info = set_card_info(tag_iterator);
+ set_card_info(tag_iterator, card_info);
}
+
+ add_card_to_vec(card_info);
+ }
+
+ // makes the vectors smaller and fit to memory
+ minions.shrink_to_fit();
+ spells.shrink_to_fit();
+ weapons.shrink_to_fit();
+}
+
+void CardEval::get_stat_average() {
+ const int atk_health_size = MAX_MANA+1;
+ atk_health atk_health_count[atk_health_size];
+
+ int card_total[atk_health_size];
+ for(int i = 0; i < atk_health_size; i++) {
+ card_total[i] = 0;
+ }
+
+ for(card_struct card_info : minions) {
+ if(card_info.cost >= 0 && card_info.cost < atk_health_size && card_info.is_collectible) {
+
+ atk_health_count[card_info.cost].atk += double(card_info.attack);
+ atk_health_count[card_info.cost].health += double(card_info.health);
+
+ card_total[card_info.cost]++;
+ }
+ }
+
+ for(int i = 0; i < atk_health_size; i++) {
+ atk_health tmp_ah;
+
+ tmp_ah.atk = atk_health_count[i].atk / double(card_total[i]);
+ tmp_ah.health = atk_health_count[i].health / double(card_total[i]);
+
+ card_avg_stats.push_back(tmp_ah);
}
}
void CardEval::exit_with_error() {
// exit the program with an error message
- std::exit(EXIT_FAILURE);
+ exit(EXIT_FAILURE);
}
void CardEval::add_card_to_vec(card_struct &in_card) {
@@ -78,8 +185,8 @@ void CardEval::add_card_to_vec(card_struct &in_card) {
}
}
-char CardEval::get_type(std::string type_str) {
- int type_int = std::stoi(type_str);
+char CardEval::get_type(string type_str) {
+ int type_int = stoi(type_str);
switch(type_int) {
// when it is a minion
@@ -97,9 +204,10 @@ char CardEval::get_type(std::string type_str) {
}
}
-std::string CardEval::get_class(std::string class_str) {
- int class_int = std::stoi(class_str);
+string CardEval::get_class(string class_str) {
+ int class_int = stoi(class_str);
+ // using switch to return the string of the cardtype
switch(class_int) {
case 2:
return "druid";
@@ -120,44 +228,41 @@ std::string CardEval::get_class(std::string class_str) {
case 10:
return "warrior";
default:
- return "";
+ return "\0";
}
}
-card_struct CardEval::set_card_info(XMLElement* tag_it) {
- card_struct tmp_card;
-
+void CardEval::set_card_info(XMLElement* tag_it, card_struct &curr_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();
+ curr_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;
+ curr_card.is_collectible = true;
} else if(tag_it->Attribute("name", "CardType")) {
- tmp_card.card_type = get_type(tag_it->Attribute("value"));
+ curr_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"));
+ curr_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"));
+ curr_card.cost = stoi(tag_it->Attribute("value"));
} else if(tag_it->Attribute("name", "Atk")) {
- tmp_card.attack = std::stoi(tag_it->Attribute("value"));
+ curr_card.attack = stoi(tag_it->Attribute("value"));
} else if(tag_it->Attribute("name", "Health")) {
- tmp_card.health = std::stoi(tag_it->Attribute("value"));
+ curr_card.health = stoi(tag_it->Attribute("value"));
} else if(tag_it->Attribute("name", "Rarity")) {
- tmp_card.rarity = std::stoi(tag_it->Attribute("value"));
+ curr_card.rarity = stoi(tag_it->Attribute("value"));
} else if(tag_it->Attribute("name", "Durability")) {
- tmp_card.durability = std::stoi(tag_it->Attribute("value"));
+ curr_card.durability = stoi(tag_it->Attribute("value"));
} else{
+ // do nothing
}
-
- return tmp_card;
}
diff --git a/src/main.cpp b/src/main.cpp
index 1335355..58da1cd 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,18 +8,46 @@
*/
+#include <tinyxml2.h>
+
#include <iostream>
#include <string>
-#include <tinyxml2.h>
-
#include "../include/cardeval.hpp"
using namespace std;
using namespace tinyxml2;
+using namespace cardeval;
int main(int argc, char** argv) {
CardEval ce(argc, argv);
- ce.get_cards();
+
+ vector<card_struct> minions;
+ vector<atk_health> avg_stats;
+
+ ce.set_minions(minions);
+ ce.set_avg_stats(avg_stats);
+
+ for(unsigned int i = 0; i < minions.size(); i++) {
+ if(minions[i].is_collectible){
+ double atk_diff, health_diff;
+
+ atk_diff = double(minions[i].attack) - avg_stats[minions[i].cost].atk;
+ health_diff = double(minions[i].health) - avg_stats[minions[i].cost].health;
+
+ minions[i].value = health_diff - atk_diff;
+ }
+ }
+
+ for(card_struct cards : minions) {
+ if(cards.is_collectible) {
+ cout << cards.card_print_format();
+ }
+ }
+
+ for(atk_health card_avg : avg_stats) {
+ cout << card_avg.prnt_str() << endl;
+ }
+
return 0;
}
diff --git a/src/textanalysis.cpp b/src/textanalysis.cpp
new file mode 100644
index 0000000..3868d02
--- /dev/null
+++ b/src/textanalysis.cpp
@@ -0,0 +1,38 @@
+/*
+
+ description: This class gets text from in between multiple patterns.
+
+ author: Yann Herklotz <ymherklotz@gmail.com>
+ date created: 10-08-2016
+ filename: cardeval.hpp
+
+ */
+
+#include "../include/cardeval.hpp"
+
+using namespace cardeval;
+using namespace std;
+
+TextAnalysis::TextAnalysis() {
+
+}
+
+TextAnalysis::~TextAnalysis() {
+
+}
+
+void TextAnalysis::get_limited_text(string &lim_text, string &text_limiter_open, string &text_limiter_close) {
+ const unsigned int OPEN_LEN = text_limiter_open.length();
+ const unsigned int CLOSE_LEN = text_limiter_close.length();
+
+ bool found_start = true;
+ bool found_end = true;
+
+ for(unsigned int text_i = 0; text_i < lim_text.length(); ++text_i) {
+ for(unsigned int open_i = 0; open_i < OPEN_LEN; ++open_i) {
+ if(lim_text[text_i + open_i] != text_limiter_open[open_i]) {
+ found_start = false;
+ }
+ }
+ }
+}