Guest User

Untitled

a guest
Jan 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. // hw4.cpp noble i/o
  2. // Stephanie Wu
  3. // CS1124
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <sstream>
  10. #include <vector>
  11. using namespace std;
  12.  
  13. // creates warrior class, holds warrior name and weapon pointer
  14. class Warrior {
  15. public:
  16. Warrior(const string& n, const float& h)
  17. : name(n), health(h), hired(false) {}
  18. // getters
  19. string getName() const {return name;}
  20. float getHealth() const {return health;}
  21. bool isHired() const {return hired;}
  22.  
  23. // setters
  24. void setHealth(float h) {health = h;}
  25. void gotHired() {hired = true;}
  26. void gotFired() {hired = false;}
  27.  
  28. //displays health and name
  29. void display() {
  30. cout << name << ": " << health << endl;
  31. }
  32.  
  33. private:
  34. string name;
  35. float health;
  36. bool hired;
  37. };
  38.  
  39. // noble class
  40. class Noble{
  41. public:
  42. Noble (const string& n) : name(n), warriors(NULL) {};
  43. string getName() const {return name;}
  44.  
  45. // gets total health of army
  46. float getHealth() const {
  47. float h = 0;
  48. for (size_t i = 0; i < warriors.size(); ++i)
  49. h += warriors[i]->getHealth();
  50. return h;
  51. }
  52.  
  53. // displays noble's name, army size, and the name of warriors and their strengths
  54. void display() {
  55. cout << name << " has an army of: " << warriors.size() << endl;
  56. for (size_t i = 0; i < warriors.size(); ++i)
  57. warriors[i]->display();
  58. }
  59.  
  60. //hire warrior, adds to array of warrior pointers
  61. void hire(Warrior& w) {
  62. if( !(w.isHired()) ) {
  63. warriors.push_back(&w);
  64. w.gotHired();
  65. }
  66. else
  67. cout << "This warrior already has a job\n";
  68. }
  69. // fires warrior, removes from array
  70. void fire(Warrior& w) {
  71. w.gotFired();
  72. for (size_t i = 0; i < warriors.size() ; ++i) {
  73. if(w.getName() == warriors[i]->getName()){
  74. warriors[i] = warriors[0];
  75. warriors.pop_back();
  76. }
  77. }
  78. }
  79.  
  80. // sets health of every warrior by multiplying their current health by a ratio
  81. void setHealth(float ratio) {
  82. for (size_t i = 0; i < warriors.size() ; ++i){
  83. warriors[i]->setHealth( ratio* (warriors[i]->getHealth()) );
  84. }
  85. }
  86.  
  87. // battles two nobles
  88. void battle(Noble& n) {
  89. cout << n.getName() << " battles " << this->getName() << endl;
  90.  
  91. // if the nobles' armies are evenly matched
  92. if(n.getHealth() == this->getHealth()) {
  93. cout << n.getName() << " and " << this->getName() << " kill each other\n";
  94. n.setHealth(0);
  95. this->setHealth(0);
  96. }
  97.  
  98. // if the opposing noble is dead
  99. else if (n.getHealth() == 0 || this->getHealth() == 0) {
  100. if(n.getHealth() == 0)
  101. cout << "He's dead " << this->getName() << endl;
  102. else if(this->getHealth() == 0)
  103. cout << "You're dead dummy\n";
  104. }
  105.  
  106. // if the opposing noble is more powerful
  107. else if(n.getHealth() > this->getHealth()) {
  108. cout << n.getName() << " defeated " << this->getName() << endl;
  109. float r = (this->getHealth())/(n.getHealth());
  110. n.setHealth(r);
  111. this->setHealth(0);
  112. }
  113. else if(n.getHealth() < this->getHealth()) {
  114. cout << this->getName() << " defeated " << n.getName() << endl;
  115. float r = (n.getHealth())/(this->getHealth());
  116. this->setHealth(r);
  117. n.setHealth(0);
  118. }
  119. };
  120. private:
  121. string name;
  122. vector<Warrior*> warriors;
  123. };
  124.  
  125. Noble* findNoble(const vector<Noble*>& nobles, const string& noble) {
  126. for (size_t i = 0; i < nobles.size() ; ++i) {
  127. if(nobles[i]->getName() == noble) {
  128. return nobles[i];
  129. }
  130. }
  131. }
  132.  
  133. Warrior* findWarrior(const vector<Warrior*>& warriors, const string& warrior) {
  134. for (size_t i = 0; i < warriors.size(); ++i) {
  135. if(warriors[i]->getName() == warrior) {
  136. return warriors[i];
  137. }
  138. }
  139. }
  140.  
  141. void displayAll(vector<Noble*>& nobles, vector<Warrior*>& warriors) {
  142. cout << "\n Status \n ============================= \n";
  143. for (size_t i = 0; i < nobles.size(); ++i) {
  144. nobles[i]->display();
  145. cout << endl;
  146. }
  147. cout << "Warriors without nobles \n";
  148. for (size_t j = 0; j < warriors.size(); j++) {
  149. if( !warriors[j]->isHired() ) {
  150. warriors[j]->display();
  151. }
  152. }
  153. }
  154.  
  155. void clearAll(vector<Noble*>& nobles, vector<Warrior*>& warriors) {
  156. for(int i = warriors.size()-1; i > -1; i--) {
  157. delete warriors[i];
  158. warriors.pop_back();
  159. }
  160. for (int j = nobles.size()-1; j > -1; j--) {
  161. delete nobles[j];
  162. nobles.pop_back();
  163. }
  164. }
  165.  
  166. int main() {
  167.  
  168. ifstream ifs("nobleWarriors.txt");
  169.  
  170. if(!ifs) {
  171. cout << "This file could not be found \n";
  172. exit(0);
  173. }
  174.  
  175. vector<Noble*> nobles;
  176. vector<Warrior*> warriors;
  177.  
  178. string line;
  179. while (getline(ifs,line)) {
  180. istringstream lineStream(line);
  181. string firstW;
  182. lineStream >> firstW;
  183. if(firstW == "Noble") {
  184. string tempN;
  185. lineStream >> tempN;
  186. nobles.push_back(new Noble(tempN));
  187. }
  188. else if(firstW == "Warrior") {
  189. string tempN;
  190. int tempH;
  191. lineStream >> tempN >> tempH;
  192. warriors.push_back(new Warrior(tempN,tempH));
  193. }
  194. else if(firstW == "Hire"){
  195. string tempNoble;
  196. string tempWar;
  197. lineStream >> tempNoble >> tempWar;
  198. findNoble(nobles,tempNoble)->hire(*findWarrior(warriors,tempWar));
  199. }
  200. else if(firstW == "Fire") {
  201. string tempNoble;
  202. string tempWar;
  203. lineStream >> tempNoble >> tempWar;
  204. findNoble(nobles,tempNoble)->fire(*findWarrior(warriors,tempWar));
  205. }
  206. else if(firstW == "Battle") {
  207. string tempN1;
  208. string tempN2;
  209. lineStream >> tempN1 >> tempN2;
  210. findNoble(nobles, tempN1)->battle(*findNoble(nobles,tempN2));
  211. }
  212. else if(firstW == "Status") {
  213. displayAll(nobles, warriors);
  214. }
  215. else if(firstW == "Clear") {
  216. clearAll(nobles, warriors);
  217. }
  218. }
  219.  
  220. cin.get();
  221. }
Add Comment
Please, Sign In to add comment