school/cs142/lab10/arena.cpp
2016-04-06 20:45:34 -07:00

112 lines
2.4 KiB
C++

#include <vector>
using namespace std;
#include "arena.h"
#include "fighter.h"
#include "archer.h"
#include "cleric.h"
#include "robot.h"
#include "util.h"
arena::~arena() {
fighter * f = NULL;
for(vector<FighterInterface *>::iterator it = fighters.begin();
it != fighters.end();
it++) {
f = dynamic_cast<fighter *>(*it);
delete f;
}
}
bool arena::contains(string name) {
fighter * f = NULL;
for(unsigned int i = 0; i < fighters.size(); i++) {
f = dynamic_cast<fighter *>(fighters[i]);
if(f->getName() == name) {
return true;
}
}
return false;
}
bool arena::addFighter(string info) {
fighter * f = NULL;
string name;
string type;
int max_hp;
int strength;
int speed;
int magic;
bool worked;
worked = parse_n_load(info, name, type, max_hp, strength, speed, magic);
if(not worked) {
return false;
}
if(contains(name)) {
return false;
}
if(type == "A") {
f = new archer(name, max_hp, strength, speed, magic);
}
else if(type == "C") {
f = new cleric(name, max_hp, strength, speed, magic);
}
else if(type == "R") {
f = new robot(name, max_hp, strength, speed, magic);
}
fighters.push_back(f);
return true;
}
bool arena::removeFighter(string name) {
fighter * f = NULL;
for(vector<FighterInterface *>::iterator it = fighters.begin();
it != fighters.end();
it++) {
f = dynamic_cast<fighter *>(*it);
if(f->getName() == name) {
delete f;
fighters.erase(it);
return true;
}
}
return false;
}
FighterInterface * arena::getFighter(string name) {
fighter * f = NULL;
for(vector<FighterInterface *>::iterator it = fighters.begin();
it != fighters.end();
it++) {
f = dynamic_cast<fighter *>(*it);
if(f->getName() == name) {
return dynamic_cast<FighterInterface *>(f);
}
}
return NULL;
}
int arena::getSize() {
return fighters.size();
}
ostream & operator<<(ostream & os, arena & a) {
os << "[";
for(vector<FighterInterface * >::iterator it = a.fighters.begin();
it != a.fighters.end();
it++) {
os << *(dynamic_cast<fighter *>( *it));
if (it != a.fighters.end() - 1) {
os << ", ";
}
}
os << "]";
return os;
}