Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. // Strategy.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <vector>
  7. #include <iostream>
  8. #include <map>
  9. using namespace std;
  10.  
  11. class Unit {
  12. protected:
  13. string name;
  14. int attack;
  15. int health;
  16. int defense;
  17. int initiative;
  18. const string attack_type;
  19. public:
  20. void info() {
  21. cout << name << '\n';
  22. };
  23. };
  24.  
  25. class Warrior : public Unit {
  26. protected:
  27. const string attack_type = "Closest target";
  28. };
  29.  
  30. class Mage : public Unit {
  31. protected:
  32. const string attack_type = "All targets";
  33. string element;
  34. };
  35.  
  36. class Ranger : public Unit {
  37. private:
  38. const string attack_type = "Any target";
  39. };
  40.  
  41. class Knight : public Warrior {
  42. private:
  43. public:
  44. const static int ID = 0;
  45. const static int cost = 10;
  46. Knight() {
  47. name = "Knight";
  48. attack = 0;
  49. defense = 0;
  50. health = 0;
  51. initiative = 0;
  52. };
  53. ~Knight() {};
  54. };
  55.  
  56. class Archer : public Ranger {
  57. private:
  58. public:
  59. const static int ID = 200;
  60. const static int cost = 20;
  61. Archer() {
  62. name = "Archer";
  63. attack = 0;
  64. defense = 0;
  65. health = 0;
  66. initiative = 0;
  67. };
  68. ~Archer() {};
  69. };
  70.  
  71. class Wizard : public Mage {
  72. private:
  73. public:
  74. const static int ID = 100;
  75. const static int cost = 30;
  76. Wizard() {
  77. name = "Wizard";
  78. attack = 0;
  79. defense = 0;
  80. health = 0;
  81. initiative = 0;
  82. element = "Air";
  83. };
  84. ~Wizard() {};
  85. };
  86.  
  87. class Army {
  88. public:
  89. Army(): Warriors(0), Mages(0), Rangers(0) {
  90.  
  91. }
  92. vector<Warrior*> Warriors;
  93. vector<Mage*> Mages;
  94. vector<Ranger*> Rangers;
  95. void info() {
  96. cout << "Warriors:\n";
  97. for (auto i : Warriors) {
  98. i->info();
  99. }
  100. cout << "Mages:\n";
  101. for (auto i : Mages) {
  102. i->info();
  103. }
  104. cout << "Rangers:\n";
  105. for (auto i : Rangers) {
  106. i->info();
  107. }
  108. }
  109. ~Army() {
  110. for (auto i : Warriors) {
  111. delete i;
  112. }
  113. for (auto i : Mages) {
  114. delete i;
  115. }
  116. for (auto i : Rangers) {
  117. delete i;
  118. }
  119. }
  120. };
  121.  
  122. class ArmyFactory {
  123. protected:
  124. public:
  125. vector<pair<string, vector<pair<string, int>>>> FractionOverview;
  126. virtual Warrior* createWarrior(int ID) = 0;
  127. virtual Mage* createMage(int ID) = 0;
  128. virtual Ranger* createRanger(int ID) = 0;
  129. ~ArmyFactory() {};
  130. };
  131.  
  132. class HumanArmyFactory : public ArmyFactory {
  133. public:
  134. HumanArmyFactory() {
  135. vector<pair<string, int>> Warriors = { make_pair("Knight", Knight::cost) };
  136. vector<pair<string, int>> Mages = { make_pair("Wizard", Wizard::cost) };
  137. vector<pair<string, int>> Rangers = { make_pair("Archer", Archer::cost) };
  138. FractionOverview.push_back(make_pair("Warriors", Warriors));
  139. FractionOverview.push_back(make_pair("Mages", Mages));
  140. FractionOverview.push_back(make_pair("Rangers", Rangers));
  141. }
  142. Warrior* createWarrior(int ID) {
  143. switch (ID) {
  144. case Knight::ID:
  145. return new Knight;
  146. }
  147. }
  148. Mage* createMage(int ID) {
  149. switch (ID) {
  150. case Wizard::ID:
  151. return new Wizard;
  152. }
  153. }
  154. Ranger* createRanger(int ID) {
  155. switch (ID) {
  156. case Archer::ID:
  157. return new Archer;
  158. }
  159. }
  160. };
  161.  
  162. class UndeadArmyFactory : public ArmyFactory {
  163. public:
  164.  
  165. };
  166.  
  167. class Menu {
  168. public:
  169. Army* CreateArmy(int &balance, ArmyFactory* factory) {
  170. Army* PlArmy = new Army();
  171. while (true) {
  172. cout << "Your balance: " << balance << "\n";
  173. for (int i = 0; i < factory->FractionOverview.size(); i++) {
  174. cout << i + 1 << ". " << factory->FractionOverview[i].first << "\n";
  175. }
  176.  
  177. cout << "0. Quit\n";
  178. int type_; cin >> type_;
  179. if (type_ == 0) break;
  180.  
  181. for (int i = 0; i < factory->FractionOverview[type_ - 1].second.size(); i++) {
  182. cout << i + 1 << ". " << factory->FractionOverview[type_ - 1].second[i].first << " - ";
  183. cout << factory->FractionOverview[type_ - 1].second[i].second << "\n";
  184. }
  185. cout << "0. Back\n";
  186. int unit_; cin >> unit_;
  187. if (unit_ == 0) continue;
  188. int price = factory->FractionOverview[type_ - 1].second[unit_ - 1].second;
  189. int id_ = (type_ - 1) * 100 + unit_ - 1;
  190. if (balance < price) continue;
  191.  
  192. balance -= price;
  193.  
  194. if (type_ == 1) PlArmy->Warriors.push_back(factory->createWarrior(id_));
  195. if (type_ == 2) PlArmy->Mages.push_back(factory->createMage(id_));
  196. if (type_ == 3) PlArmy->Rangers.push_back(factory->createRanger(id_));
  197. }
  198. return PlArmy;
  199. }
  200. };
  201.  
  202. class Player {
  203. private:
  204. int fraction_ID;
  205. Army* Player_Army;
  206. int balance;
  207. ArmyFactory* factory;
  208. public:
  209. Player(int balance): fraction_ID(0), Player_Army(nullptr), balance(balance) {}
  210. int ShowFraction() {
  211. return fraction_ID;
  212. }
  213. void SetFraction()
  214. {
  215. cout << "Choose your fraction:\n1. Humans\n2. Undead\n";
  216. cin >> fraction_ID;
  217. if (fraction_ID == 1) factory = new HumanArmyFactory();
  218. if (fraction_ID == 2) factory = new HumanArmyFactory(); //фабрика UndeadArmyFactory будет здесь в будущем, т.к. пока не реализована
  219. }
  220. void Initialize() {
  221. SetFraction();
  222. Menu PlMenu;
  223. Player_Army = PlMenu.CreateArmy(balance, factory);
  224. Player_Army->info();
  225. }
  226. };
  227.  
  228. int main()
  229. {
  230. int balance;
  231. cout << "Enter the starting treasury >> ";
  232. cin >> balance;
  233. Player Player_1(balance);
  234. Player_1.Initialize();
  235. Player Player_2(balance);
  236. Player_2.Initialize();
  237.  
  238. return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement