Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hw4.cpp noble i/o
- // Stephanie Wu
- // CS1124
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- using namespace std;
- // creates warrior class, holds warrior name and weapon pointer
- class Warrior {
- public:
- Warrior(const string& n, const float& h)
- : name(n), health(h), hired(false) {}
- // getters
- string getName() const {return name;}
- float getHealth() const {return health;}
- bool isHired() const {return hired;}
- // setters
- void setHealth(float h) {health = h;}
- void gotHired() {hired = true;}
- void gotFired() {hired = false;}
- //displays health and name
- void display() {
- cout << name << ": " << health << endl;
- }
- private:
- string name;
- float health;
- bool hired;
- };
- // noble class
- class Noble{
- public:
- Noble (const string& n) : name(n), warriors(NULL) {};
- string getName() const {return name;}
- // gets total health of army
- float getHealth() const {
- float h = 0;
- for (size_t i = 0; i < warriors.size(); ++i)
- h += warriors[i]->getHealth();
- return h;
- }
- // displays noble's name, army size, and the name of warriors and their strengths
- void display() {
- cout << name << " has an army of: " << warriors.size() << endl;
- for (size_t i = 0; i < warriors.size(); ++i)
- warriors[i]->display();
- }
- //hire warrior, adds to array of warrior pointers
- void hire(Warrior& w) {
- if( !(w.isHired()) ) {
- warriors.push_back(&w);
- w.gotHired();
- }
- else
- cout << "This warrior already has a job\n";
- }
- // fires warrior, removes from array
- void fire(Warrior& w) {
- w.gotFired();
- for (size_t i = 0; i < warriors.size() ; ++i) {
- if(w.getName() == warriors[i]->getName()){
- warriors[i] = warriors[0];
- warriors.pop_back();
- }
- }
- }
- // sets health of every warrior by multiplying their current health by a ratio
- void setHealth(float ratio) {
- for (size_t i = 0; i < warriors.size() ; ++i){
- warriors[i]->setHealth( ratio* (warriors[i]->getHealth()) );
- }
- }
- // battles two nobles
- void battle(Noble& n) {
- cout << n.getName() << " battles " << this->getName() << endl;
- // if the nobles' armies are evenly matched
- if(n.getHealth() == this->getHealth()) {
- cout << n.getName() << " and " << this->getName() << " kill each other\n";
- n.setHealth(0);
- this->setHealth(0);
- }
- // if the opposing noble is dead
- else if (n.getHealth() == 0 || this->getHealth() == 0) {
- if(n.getHealth() == 0)
- cout << "He's dead " << this->getName() << endl;
- else if(this->getHealth() == 0)
- cout << "You're dead dummy\n";
- }
- // if the opposing noble is more powerful
- else if(n.getHealth() > this->getHealth()) {
- cout << n.getName() << " defeated " << this->getName() << endl;
- float r = (this->getHealth())/(n.getHealth());
- n.setHealth(r);
- this->setHealth(0);
- }
- else if(n.getHealth() < this->getHealth()) {
- cout << this->getName() << " defeated " << n.getName() << endl;
- float r = (n.getHealth())/(this->getHealth());
- this->setHealth(r);
- n.setHealth(0);
- }
- };
- private:
- string name;
- vector<Warrior*> warriors;
- };
- Noble* findNoble(const vector<Noble*>& nobles, const string& noble) {
- for (size_t i = 0; i < nobles.size() ; ++i) {
- if(nobles[i]->getName() == noble) {
- return nobles[i];
- }
- }
- }
- Warrior* findWarrior(const vector<Warrior*>& warriors, const string& warrior) {
- for (size_t i = 0; i < warriors.size(); ++i) {
- if(warriors[i]->getName() == warrior) {
- return warriors[i];
- }
- }
- }
- void displayAll(vector<Noble*>& nobles, vector<Warrior*>& warriors) {
- cout << "\n Status \n ============================= \n";
- for (size_t i = 0; i < nobles.size(); ++i) {
- nobles[i]->display();
- cout << endl;
- }
- cout << "Warriors without nobles \n";
- for (size_t j = 0; j < warriors.size(); j++) {
- if( !warriors[j]->isHired() ) {
- warriors[j]->display();
- }
- }
- }
- void clearAll(vector<Noble*>& nobles, vector<Warrior*>& warriors) {
- for(int i = warriors.size()-1; i > -1; i--) {
- delete warriors[i];
- warriors.pop_back();
- }
- for (int j = nobles.size()-1; j > -1; j--) {
- delete nobles[j];
- nobles.pop_back();
- }
- }
- int main() {
- ifstream ifs("nobleWarriors.txt");
- if(!ifs) {
- cout << "This file could not be found \n";
- exit(0);
- }
- vector<Noble*> nobles;
- vector<Warrior*> warriors;
- string line;
- while (getline(ifs,line)) {
- istringstream lineStream(line);
- string firstW;
- lineStream >> firstW;
- if(firstW == "Noble") {
- string tempN;
- lineStream >> tempN;
- nobles.push_back(new Noble(tempN));
- }
- else if(firstW == "Warrior") {
- string tempN;
- int tempH;
- lineStream >> tempN >> tempH;
- warriors.push_back(new Warrior(tempN,tempH));
- }
- else if(firstW == "Hire"){
- string tempNoble;
- string tempWar;
- lineStream >> tempNoble >> tempWar;
- findNoble(nobles,tempNoble)->hire(*findWarrior(warriors,tempWar));
- }
- else if(firstW == "Fire") {
- string tempNoble;
- string tempWar;
- lineStream >> tempNoble >> tempWar;
- findNoble(nobles,tempNoble)->fire(*findWarrior(warriors,tempWar));
- }
- else if(firstW == "Battle") {
- string tempN1;
- string tempN2;
- lineStream >> tempN1 >> tempN2;
- findNoble(nobles, tempN1)->battle(*findNoble(nobles,tempN2));
- }
- else if(firstW == "Status") {
- displayAll(nobles, warriors);
- }
- else if(firstW == "Clear") {
- clearAll(nobles, warriors);
- }
- }
- cin.get();
- }
Add Comment
Please, Sign In to add comment