// // manacost.cpp // cards-proof // // Created by Ethan Miller on 3/18/12. // #include #include #include #include "Cost.h" using namespace std; map cost::default_cost; bool cost::default_cost_is_set = false; cost::cost() { m_cost_ = default_cost; // set_default(); } cost::cost( string new_mana_cost ) { // Takes a manacost in the form NC // with N being any number and C being a // char that was defined in the default // key. Case insensitive. cost(); m_cost_ = read_from_string(new_mana_cost); } int cost::get(char key) { return m_cost_.find(key)->second; } void cost::set_default(istream & in) { char temp; string line; getline(in, line); while (line != "enddef" && in) { stringstream file_reader(line); file_reader >> temp; default_cost[tolower(temp)] = 0; getline(in, line); } default_cost_is_set = true; } map cost::read_from_string( string new_mana_cost ) { stringstream input(new_mana_cost); char in; map temp_cost = default_cost; while ( !input.eof() ) { in = tolower(input.peek()); if (isdigit(in)) { if (temp_cost.find('n')!=temp_cost.end()) { input >> temp_cost['n']; } else { input.get(); } continue; // Continue here skips the input.get at the end of the loop. // Since the number was >>'d, we don't want to eat more. } if (temp_cost.find(in) != temp_cost.end()) { temp_cost[in] += 1; } input.get(); } return temp_cost; } string cost::to_str() { // TODO find a way to set order in game file. stringstream cost; cost << get('n'); for ( map::const_iterator itr = m_cost_.begin(); itr != m_cost_.end(); ++itr ) if ( itr->first != 'n' ) { for ( int i = 0; i < itr->second; ++i ) { cost << char(toupper(itr->first)); // cast to char b/c otherwise, outputs the int value } } return cost.str(); }