GCI/Recruitment

From The Battle for Wesnoth Wiki
< GCI
Revision as of 03:03, 6 March 2011 by Aethaeryn (talk | contribs) (categorizing)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
class potential_recruit
{
public:
       potential_recruit(const std::string &name, int cost, double quality, int max_qty)
               : name_(name), cost_(cost), quality_(quality), max_qty_(max_qty)
               {}
       const std::string& name() const
       {
               return name_;
       }
       int cost() const
       {
               return cost_;
       }
       int quality() const
       {
               return quality_;
       }
       int max_qty() const
       {
               return max_qty_;
       }
private:
       std::string name_;
       int cost_;
       double quality_;
       int max_qty_;
};
class team
{
public:
       int gold()
       {
               return gold_;
       }
       void set_gold(int gold)
       {
               gold_ = gold;
       }
       void add_unit(const std::string &unit)
       {
              units_.push_back(unit);
       }
       const std::vector<std::string>& units()
       {
               return units_;
       }
       int get_current_qty(const potential_recruit &r) const
       {
               return get_current_qty(r.name());
       }
       int get_current_qty(const std::string &name) const
       {
               return std::count(units_.begin(), units_.end(), name);
       }
       void spend_gold(int gold)
       {
               gold_-=gold;
       }
private:
       int gold_;
       std::vector<std::string> units_;
};
/** this is the function to call to do 1 recruit when you've selected it */
static void recruit(team &t, const potential_recruit &r)
{
       if (t.gold()<r.cost()) {
               std::cerr <<  "ERROR: cannot recruit "<<r.name()<<", not enough gold"<< std::endl;
               exit(-1);
       }
       if (t.get_current_qty(r)>=r.max_qty()) {
               std::cerr << "ERROR: cannot recruit "<<r.name()<<", too many in the field"<< std::endl;
               exit(-1);
       }
       std::cout << " Recruiting [" << r.name() << "]" << std::endl;
       t.spend_gold(r.cost());
       t.add_unit(r.name());
}
void expect(const team &t, const std::string &name, int qty)
{
       int current_qty = t.get_current_qty(name);
       if (current_qty != qty) {
               std::cerr <<  "ERROR: wrong number of ["<< name << "], expected "<<qty<<" but got "<< current_qty << std::endl;
               exit(-2);
       } else {
               std::cerr <<  "OK: count of ["<< name << "] is "<< current_qty<< std::endl;
       }
}
static void ai_choose_recruits(team &t, int max_units_to_recruit, double quality_factor, const  std::vector<potential_recruit> &recruit_list)
{
       /* TODO: code your implementation here */
       recruit(t,recruit_list.at(1));//Royal Guard
       recruit(t,recruit_list.at(1));//Royal Guard
       recruit(t,recruit_list.at(0));//Spearman
       recruit(t,recruit_list.at(0));//Spearman
       recruit(t,recruit_list.at(0));//Spearman
}
int main()
{
       //a small testcase
       team t;
       t.set_gold(120);
       t.add_unit("Spearman");
       std::vector<potential_recruit> recruit_list;
       recruit_list.push_back(potential_recruit("Spearman", 13, 12.6 ,10));
       recruit_list.push_back(potential_recruit("Royal Guard", 30, 40.0, 2));
       expect(t,"Spearman", 1);
       expect(t,"Royal Guard", 0);
       ai_choose_recruits(t, 5, 1.0, recruit_list);
       expect(t,"Spearman", 4);
       expect(t,"Royal Guard", 2);
       return 0;
}
This page was last edited on 6 March 2011, at 03:03.