Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.17 KB | None | 0 0
  1. // C++ Battle Text Game created by Justin Gonzales
  2. // Header.h
  3. #pragma once
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <memory>
  8. #include <random>
  9.  
  10. class GameEntity
  11. {
  12. protected:
  13.     const std::string m_name;
  14.     int m_hp;
  15. public:
  16.     GameEntity() {}
  17.     GameEntity(std::string name, int hp) : m_name(name), m_hp(hp){} // NOTE: Get() functions are
  18.         // justifyable under this hierchy
  19.     const std::string GetName() const { return m_name; }
  20.     const int GetHP() const { return m_hp;  }
  21.         // NOT VIRTUAL BECAUSE doesnt need to be changed
  22. };
  23.  
  24. struct Result
  25. {
  26.     Result() : m_energyCost(0), m_totalDamage(0), m_effect(nothing) {}
  27.     Result(const int energyCost, const int totalDamage, const int effect) : m_energyCost(energyCost), m_totalDamage(totalDamage)
  28.     {
  29.         switch (effect)
  30.         {
  31.         case 0:
  32.             m_effect = nothing;
  33.         case 1:
  34.             m_effect = fire;
  35.         case 2:
  36.             m_effect = freeze;
  37.         }
  38.     }
  39.  
  40.     bool Result::operator==(const Result result);
  41.  
  42.     enum Effect // local scope Effect enum
  43.     {
  44.         freeze,
  45.         fire,
  46.         nothing,
  47.     };
  48.  
  49.     const int m_energyCost;
  50.     const int m_totalDamage;
  51.     Effect m_effect;
  52. };
  53.  
  54. class Item : public GameEntity // NOTE: With polymorphism, the game will declare vectors to smart pointers'
  55.     // to Items. This allows functions to use an item as a weapon or item.
  56. {
  57. protected:
  58.     const std::shared_ptr<const Result> m_mainResult;
  59. public:
  60.     Item() {}
  61.     Item(std::string name, int hp, const Result mainResult) :
  62.         GameEntity(name, hp), m_mainResult(std::make_shared<const Result>(mainResult)) {}
  63.  
  64.     const int GetDurability() const;
  65.  
  66.     virtual std::shared_ptr<const Result> Use();
  67. };
  68.  
  69. class Weapon : public Item
  70. {
  71. private:
  72.     const std::shared_ptr<const Result> m_specialResult;
  73. public:
  74.     Weapon(std::string name, int hp, const Result mainResult, const Result specialResult):
  75.         Item(name, hp, mainResult), m_specialResult(std::make_shared<const Result>(specialResult)) {}
  76.  
  77.     virtual std::shared_ptr<const Result> Use(); // NOTE: Giving the Weapon this functionality might be a
  78.         // problem. The game will have to check if the item is a weapon or not to use the m_specail result.
  79.         // This goes against normal polymorphic practices. But since this is a very small game,
  80.         // I think we can sacrifice using dynamic_cast once.
  81. };
  82.  
  83. class OffensiveEntity : public GameEntity
  84. {
  85. protected:
  86.     std::vector< std::shared_ptr<Item> > m_items;
  87.     Result::Effect m_currentEffect;
  88. public:
  89.     OffensiveEntity(std::string name, int hp) : GameEntity(name, hp) {}
  90.  
  91.     void ClearItems();
  92.  
  93.     std::vector<std::shared_ptr<Item>> GetItems(std::vector<std::string> names) const;
  94.     std::vector<std::shared_ptr<Item>> GetItems() const;
  95.  
  96.     void AddItems(std::vector<std::shared_ptr<Item>> items);
  97.  
  98.     virtual void TakeResults(std::vector<std::shared_ptr<const Result>> results);
  99.  
  100.     virtual const int GetEnergy() const { return 0; } // These are here so the Player could work better with the Interface class
  101.  
  102.     virtual void SubtractEnergy(int sub) const { }
  103.  
  104.     virtual void Reset(); //NOTE: This class has a Reset function so it can be polymorphic with the Player class
  105.         // to be used in the interface classes. This function resets the current effect and changes the hp accordingly.
  106.         // The player function adds the added functionality of reseting the energy aswell.
  107. };
  108.  
  109. class Player : public OffensiveEntity // NOTE: Enemies are offensive entities because
  110.     // I couldn't find a difference between enemies and this base class.
  111.     // The player is different because he has energy.
  112. {
  113. private:
  114.     const int MAX_ENERGY = 10;
  115.     int m_currentEnergy;
  116. public:
  117.     Player(std::string name, int hp) : OffensiveEntity(name, hp),
  118.         m_currentEnergy(MAX_ENERGY) {}
  119.  
  120.     void TakeResults(std::vector<std::shared_ptr<const Result>> results);
  121.  
  122.     virtual const int GetEnergy() const { return m_currentEnergy; } // These are here so the Player could work better with the Interfave class
  123.  
  124.     virtual void SubtractEnergy(const int sub) { m_currentEnergy -= sub; }
  125.  
  126.     void Reset();
  127. };
  128.  
  129. class OffensiveEntityInterface
  130. {
  131. protected:
  132.     std::shared_ptr<OffensiveEntity> m_offensiveEntity;
  133. public:
  134.     OffensiveEntityInterface() {}
  135.  
  136.     OffensiveEntityInterface(std::string name, int hp)
  137.     { m_offensiveEntity = std::make_shared<OffensiveEntity>((OffensiveEntity(name, hp))); }
  138.  
  139.     OffensiveEntityInterface(OffensiveEntity o)
  140.     { m_offensiveEntity = std::make_shared<OffensiveEntity>(o); }
  141.  
  142.     virtual void Equip(std::vector<std::shared_ptr<Item>> items); // This function just calls AddItems()
  143.         // I don't know if that is invalid hierachercally, but it's the best idea when it comes to this
  144.         // class.
  145.  
  146.     std::vector<std::shared_ptr<Item>> GetEquiped() { return m_offensiveEntity->GetItems(); } // for debug reasons
  147.  
  148.     virtual std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const = 0; // NOTE: Only pure virtual function because
  149.         // cannot have an implementaion for this base class. And will not need to create any of these objects.
  150. };
  151.  
  152. class EnemyAI : public OffensiveEntityInterface
  153. {
  154. private:
  155.     const int m_difficultyLevel; //NOTE: This variable is not in a class for Enemies for instance.
  156.  
  157.     // NOTE: These functions are private because nothing else needs to use them
  158.     std::shared_ptr<Item> CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect) const;
  159.     std::shared_ptr<Item> CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect, int specialEnergyCost,
  160.         int specialTotalDamage, Result::Effect specialEffect) const;
  161. public:
  162.     EnemyAI(std::string name, int hp, int difficulty) :
  163.         OffensiveEntityInterface(name, hp), m_difficultyLevel(difficulty) {} //NOTE: Calls OffensiveEntityInterface's constructor because
  164.             // Enemies are just OffensiveEntities
  165.     EnemyAI(OffensiveEntity o, int difficulty) : OffensiveEntityInterface(o), m_difficultyLevel(difficulty) {}
  166.  
  167.     virtual void Equip(); // NOTE: This gives the Enemy random weapons based on its dificulty level.
  168.  
  169.     std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const; // NOTE: This function makes random choices on what weapons
  170.         // it will use in one turn based on the difficulty level. The enemy doesn't have energy because it is an uneeded complexity.
  171. };
  172.  
  173. class PlayerInterface : public OffensiveEntityInterface
  174. { // NOTE: Only class that doesn't have any added private member variables because it can use polymorphism to
  175.     // just reuse code. Specifically, realocating the offensiveEntity variable to a Player object.
  176. public:
  177.     PlayerInterface(std::string name, int hp)
  178.     {m_offensiveEntity = std::make_shared<Player>(Player(name, hp));}
  179.  
  180.     std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const; // NOTE: this function prompts the user
  181.         // on what items to use and how much energy they have and what items they have chose to use.
  182.         // This is why it is redefined here.
  183. };
  184.  
  185. class Game
  186. {
  187. private:
  188.     std::vector<std::shared_ptr<EnemyAI>> m_enemies;
  189.     PlayerInterface m_player;
  190.  
  191. };
  192.  
  193. // Main.cpp. Test file
  194. #include "Header.h"
  195.  
  196. int main()
  197. {
  198.     std::shared_ptr<EnemyAI> offensiveEntityInterface =
  199.         std::make_shared<EnemyAI>(EnemyAI("Dank Memerson", 50, 1));
  200.     std::vector<std::shared_ptr<Item>> tempItems;
  201.     tempItems.push_back(std::make_shared<Item>(Item("Dank Doodles", 20, Result(2, 40, Result::Effect::freeze))));
  202.     tempItems.push_back(std::make_shared<Item>(Item("Dank Doodlea", 36, Result(4, 56, Result::Effect::fire))));
  203.     offensiveEntityInterface->OffensiveEntityInterface::Equip(tempItems);
  204.     for (auto & i : offensiveEntityInterface->GetEquiped())
  205.     {
  206.         std::cout << i->GetName() << " name \n";
  207.         std::cout << i->GetHP() << " durability \n";
  208.         std::shared_ptr<const Result> tempResult = i->Use();
  209.         std::cout << tempResult->m_totalDamage << " total damage \n";
  210.         std::cout << tempResult->m_energyCost << " energy cost \n";
  211.         std::cout << tempResult->m_effect << " effect \n";
  212.     }
  213.     offensiveEntityInterface->Equip();
  214.     for (auto & i : offensiveEntityInterface->GetEquiped())
  215.     {
  216.         std::cout << i->GetName() << " name \n";
  217.         std::cout << i->GetHP() << " durability \n";
  218.         std::shared_ptr<const Result> tempResult = i->Use();
  219.         std::cout << tempResult->m_totalDamage << " total damage \n";
  220.         std::cout << tempResult->m_energyCost << " energy cost \n";
  221.         std::cout << tempResult->m_effect << " effect \n";
  222.     }
  223.     std::cin.get();
  224.     return 0;
  225. }
  226.  
  227. // MemberDefinitions.h
  228. #include "Header.h"
  229.  
  230. // random number generator from Stroustrup:
  231. // http://www.stroustrup.com/C++11FAQ.html#std-random
  232. template<class T>
  233. T rand_num(const T & low, const T & high)
  234. {
  235.     static std::default_random_engine re{};
  236.     using Dist = std::uniform_int_distribution<T>;
  237.     static Dist uid{};
  238.     return uid(re, Dist::param_type{ low,high });
  239. }
  240.  
  241. //Result definitions
  242. bool Result::operator==(const Result result)
  243. {
  244.     if ((result.m_effect == m_effect) || (result.m_energyCost == m_energyCost)
  245.         || (result.m_totalDamage == m_totalDamage))
  246.         return true;
  247.     else
  248.         return false;
  249. }
  250.  
  251. //Item definitions
  252. std::shared_ptr<const Result> Item::Use()
  253. {
  254.     if ((m_hp != 0) && (m_hp > 0))
  255.     {
  256.         m_hp -= m_mainResult->m_energyCost;
  257.         return std::make_shared<const Result>(*m_mainResult);
  258.     }
  259.     else
  260.     {
  261.         return std::make_shared<const Result>(0, 0, 0);
  262.     }
  263. }
  264.  
  265. const int Item::GetDurability() const
  266. {
  267.     return m_hp;
  268. }
  269.  
  270. //Weapon definitions
  271. std::shared_ptr<const Result> Weapon::Use()
  272. {
  273.     bool special = false;
  274.     if (rand_num<int>(1, 3) == 2)
  275.         special = false;
  276.     else
  277.         special = true;
  278.  
  279.     if ((m_hp != 0) && (m_hp > 0))
  280.     {
  281.         if (special == true)
  282.         {
  283.             m_hp -= m_specialResult->m_energyCost;
  284.             return std::make_shared<const Result>(*m_specialResult);
  285.         }
  286.         else
  287.         {
  288.             m_hp -= m_mainResult->m_energyCost;
  289.             return std::make_shared<const Result>(*m_mainResult);
  290.         }
  291.     }
  292.     else
  293.         return std::make_shared<const Result>(0, 0, 0);
  294. }
  295.  
  296. //OffensiveEntity definitions
  297.  
  298. void OffensiveEntity::ClearItems()
  299. {
  300.     m_items.clear();
  301. }
  302.  
  303. // Iterates through items to find if their name is the same as the names specified
  304. std::vector<std::shared_ptr<Item>> OffensiveEntity::GetItems(std::vector<std::string> names) const
  305. {
  306.     std::vector<std::shared_ptr<Item>> items;
  307.  
  308.     for (const std::shared_ptr<Item> & i : m_items)
  309.     {
  310.         for (std::string j : names)
  311.         {
  312.             if (i->GetName() == j) // Checks if name is equal
  313.                 items.push_back(std::make_shared<Item>(*i)); // Assigns item to vector
  314.         }
  315.     }
  316.     return items;
  317. }
  318.  
  319. std::vector<std::shared_ptr<Item>>  OffensiveEntity::GetItems() const
  320. {
  321.     return m_items;
  322. }
  323.  
  324. void OffensiveEntity::AddItems(std::vector<std::shared_ptr<Item>> items)
  325. {
  326.     for (auto & i : items)
  327.     {
  328.         m_items.push_back(std::make_shared<Item>(*i));
  329.     }
  330. }
  331.  
  332. void OffensiveEntity::TakeResults(std::vector<std::shared_ptr<const Result>> results)
  333. {
  334.     for (auto & i : results)
  335.     {
  336.         m_hp -= i->m_totalDamage;
  337.         m_currentEffect = i->m_effect;
  338.     }
  339. }
  340.  
  341. void OffensiveEntity::Reset()
  342. {
  343.     switch (m_currentEffect)
  344.     {
  345.     case (Result::Effect::freeze):
  346.         m_hp -= 10;
  347.  
  348.         m_currentEffect = Result::Effect::nothing;
  349.         break;
  350.     case (Result::Effect::fire):
  351.         m_hp -= 20;
  352.  
  353.         m_currentEffect = Result::Effect::nothing;
  354.         break;
  355.     }
  356. }
  357.  
  358. //Player definitions
  359.  
  360. void Player::Reset()
  361. {
  362.     int oldHp = m_hp;
  363.     m_currentEnergy = MAX_ENERGY;
  364.     OffensiveEntity::Reset();
  365.     switch (m_currentEffect)
  366.     {
  367.     case (Result::Effect::freeze):
  368.         m_currentEnergy -= 3;
  369.         break;
  370.     case (Result::Effect::fire):
  371.         m_currentEnergy -= 1;
  372.         break;
  373.     }
  374.     std::cout << "You received " << oldHp - m_hp << " damage from current effect\n";
  375. }
  376.  
  377. void Player::TakeResults(std::vector<std::shared_ptr<const Result>> results)
  378. {
  379.     int oldHp = m_hp;
  380.     OffensiveEntity::TakeResults(results);
  381.     for (const std::shared_ptr<const Result> & i : results)
  382.     {
  383.         m_currentEnergy -= i->m_energyCost;
  384.     }
  385.     std::cout << "You received " << oldHp - m_hp << " damage\n";
  386. }
  387.  
  388. //OffensiveEntityInterface definitions
  389.  
  390. void OffensiveEntityInterface::Equip(std::vector<std::shared_ptr<Item>> items)
  391. {
  392.     m_offensiveEntity->AddItems(items);
  393. }
  394.  
  395. //EnemyAI definitions
  396.  
  397. std::shared_ptr<Item> EnemyAI::CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect) const
  398. {
  399.     std::shared_ptr<const Result> tempResult = std::make_shared<const Result>(mainEnergyCost, mainTotalDamage, mainEffect);
  400.     std::shared_ptr<Item> tempItem = std::make_shared<Item>(name, hp, *tempResult);
  401.     return std::make_shared<Item>(*tempItem);
  402. }
  403.  
  404. std::shared_ptr<Item> EnemyAI::CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect, int specialEnergyCost,
  405.     int specialTotalDamage, Result::Effect specialEffect) const
  406. {
  407.     std::shared_ptr<const Result> tempMainResult = std::make_shared<const Result>(mainEnergyCost, mainTotalDamage, mainEffect);
  408.  
  409.     std::shared_ptr<const Result> tempSpecialResult = std::make_shared<const Result>(specialEnergyCost, specialTotalDamage, specialEffect);
  410.  
  411.     std::shared_ptr<Item> tempItem = std::make_shared<Weapon>(Weapon(name, hp, *tempMainResult, *tempSpecialResult));
  412.     return std::make_shared<Item>(*tempItem);
  413. }
  414.  
  415. //  This function assigns different names and different levels for different
  416. //  weapons and items. These items are then assigned to the m_items member variable.
  417. //  Values are hard coded because I didn't know how to use any kind of parser like xml.
  418. void EnemyAI::Equip()
  419. {
  420.     m_offensiveEntity->ClearItems();
  421.  
  422.     std::vector<std::shared_ptr<Item>> tempItems;
  423.  
  424.     int tempRandom = 0;
  425.  
  426.     switch (m_difficultyLevel)
  427.     {
  428.     case 0:
  429.     case 1:
  430.     {
  431.         tempRandom = rand_num<int>(2, 4);
  432.         if ((tempRandom == 1) || (tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4))
  433.             tempItems.push_back(CreateTempItem("Health Potion : HP", 3, 3, -10, Result::Effect::nothing));
  434.         if (tempRandom == 3)
  435.             tempItems.push_back(CreateTempItem("Wooden Stick : DMG", 5, 2, 10, Result::Effect::nothing, 3, 13, Result::Effect::nothing));
  436.         break;
  437.     }
  438.     case 2:
  439.     {
  440.         tempRandom = rand_num<int>(1, 4);
  441.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4))
  442.             tempItems.push_back(CreateTempItem("Health Potion : HP", 3, 3, -15, Result::Effect::nothing));
  443.         if ((tempRandom == 3) || (tempRandom == 4))
  444.             tempItems.push_back(CreateTempItem("Slightly Better Wooden Stick : DMG", 5, 2, 12, Result::Effect::nothing, 3, 15, Result::Effect::nothing));
  445.         if (tempRandom == 2)
  446.             tempItems.push_back(CreateTempItem("Mini Knife : DMG", 3, 1, 7, Result::Effect::nothing, 2, 11, Result::Effect::nothing));
  447.         break;
  448.     }
  449.     case 3:
  450.     {
  451.         tempRandom = rand_num<int>(1, 5);
  452.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
  453.             tempItems.push_back(CreateTempItem("Freeze Potion : SPLL", 3, 3, 4, Result::Effect::freeze));
  454.         if ((tempRandom == 2) || (tempRandom == 3))
  455.             tempItems.push_back(CreateTempItem("Wooden Sword : DMG", 5, 2, 16, Result::Effect::nothing, 3, 27, Result::Effect::nothing));
  456.         if (tempRandom == 4)
  457.             tempItems.push_back(CreateTempItem("Rock : DMG", 4, 1, 12, Result::Effect::nothing));
  458.         if ((tempRandom == 2) || (tempRandom == 5))
  459.             tempItems.push_back(CreateTempItem("Steak : HP", 2, 2, -20, Result::Effect::nothing));
  460.         break;
  461.     }
  462.     case 4:
  463.     {
  464.         tempRandom = rand_num<int>(1, 6);
  465.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
  466.             tempItems.push_back(CreateTempItem("Fire Potion : SPLL", 3, 3, 10, Result::Effect::fire));
  467.         if ((tempRandom == 3) || (tempRandom == 4))
  468.             tempItems.push_back(CreateTempItem("Frozen Wooden Sword : DMG", 4, 3, 16, Result::Effect::freeze, 4, 27, Result::Effect::nothing));
  469.         if (tempRandom == 4)
  470.             tempItems.push_back(CreateTempItem("Broom on Fire : DMG", 1, 1, 13, Result::Effect::fire));
  471.         if ((tempRandom == 5) || (tempRandom == 6))
  472.             tempItems.push_back(CreateTempItem("Magic Potato : HP", 2, 2, -20, Result::Effect::nothing));
  473.         if ((tempRandom == 7))
  474.             tempItems.push_back(CreateTempItem("Stone Sword : DMG", 6, 2, 21, Result::Effect::nothing, 3, 32, Result::Effect::nothing));
  475.         break;
  476.     }
  477.     case 5:
  478.     {
  479.         tempRandom = rand_num<int>(1, 6);
  480.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
  481.         {
  482.             tempItems.push_back(CreateTempItem("Super Health Potion : HP", 3, 3, -20, Result::Effect::nothing));
  483.             tempItems.push_back(CreateTempItem("Stone Sword : DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
  484.         }
  485.         if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
  486.         {
  487.             tempItems.push_back(CreateTempItem("Stone Sword: DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
  488.             tempItems.push_back(CreateTempItem("Stone Sword: DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
  489.         }
  490.         if (tempRandom == 4)
  491.             tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 15, Result::Effect::freeze));
  492.         if ((tempRandom == 2))
  493.             tempItems.push_back(CreateTempItem("Iron Rod : DMG", 2, 2, 20, Result::Effect::fire));
  494.         if ((tempRandom == 6))
  495.             tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -26, Result::Effect::nothing));
  496.         break;
  497.     }
  498.     case 6:
  499.     {
  500.         tempRandom = rand_num<int>(1, 6);
  501.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
  502.         {
  503.             tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
  504.             tempItems.push_back(CreateTempItem("Iron Rod : DMG", 2, 2, 20, Result::Effect::fire));
  505.         }
  506.         if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
  507.         {
  508.             tempItems.push_back(CreateTempItem("Stone Sword: DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
  509.             tempItems.push_back(CreateTempItem("Stone Sword: DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
  510.         }
  511.         if (tempRandom == 4)
  512.         {
  513.             tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 21, Result::Effect::fire));
  514.             tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 11, Result::Effect::freeze));
  515.         }
  516.         if ((tempRandom == 2))
  517.             tempItems.push_back(CreateTempItem("Fired Up Stone Sword : DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
  518.         if ((tempRandom == 6))
  519.             tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
  520.         break;
  521.     }
  522.     case 7:
  523.     {
  524.         tempRandom = rand_num<int>(1, 7);
  525.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6) || (tempRandom == 7))
  526.             tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
  527.         if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
  528.             tempItems.push_back(CreateTempItem("Iron Sword: DMG", 9, 3, 34, Result::Effect::nothing, 4, 51, Result::Effect::nothing));
  529.         if (tempRandom == 4)
  530.         {
  531.             tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
  532.             tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 24, Result::Effect::freeze));
  533.         }
  534.         if ((tempRandom == 2))
  535.             tempItems.push_back(CreateTempItem("Pearl Rod : DMG", 2, 2, 44, Result::Effect::nothing));
  536.         if ((tempRandom == 6))
  537.             tempItems.push_back(CreateTempItem("WaterShroom : HP", 2, 2, -37, Result::Effect::nothing));
  538.         if ((tempRandom == 7))
  539.             tempItems.push_back(CreateTempItem("Summon Slash : SPLL", 3, 3, 61, Result::Effect::nothing));
  540.         break;
  541.     }
  542.     case 10:
  543.     {
  544.         tempRandom = rand_num<int>(1, 7);
  545.         if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6) || (tempRandom == 7))
  546.         {
  547.             tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
  548.             tempItems.push_back(CreateTempItem("Eye of Diamond : SPLL", 2, 2, 34, Result::Effect::freeze));
  549.         }
  550.         if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
  551.             tempItems.push_back(CreateTempItem("Steel Sword: DMG", 11, 3, 51, Result::Effect::nothing, 4, 76, Result::Effect::nothing));
  552.         if (tempRandom == 4)
  553.         {
  554.             tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
  555.             tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
  556.             tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 24, Result::Effect::freeze));
  557.         }
  558.         if ((tempRandom == 2))
  559.         {
  560.             tempItems.push_back(CreateTempItem("Diamond Rod : DMG", 2, 2, 87, Result::Effect::nothing));
  561.             tempItems.push_back(CreateTempItem("Steel Sword on Fire: DMG", 11, 3, 42, Result::Effect::fire, 4, 76, Result::Effect::fire));
  562.         }
  563.         if ((tempRandom == 6))
  564.         {
  565.             tempItems.push_back(CreateTempItem("WaterShroom : HP", 2, 2, -37, Result::Effect::nothing));
  566.             tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
  567.             tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
  568.         }
  569.         if ((tempRandom == 7))
  570.         {
  571.             tempItems.push_back(CreateTempItem("Diamond Blade: DMG", 13, 3, 91, Result::Effect::nothing, 4, 127, Result::Effect::nothing));
  572.         }
  573.         break;
  574.     }
  575.     }
  576.  
  577.     m_offensiveEntity->AddItems(tempItems);
  578. }
  579.  
  580. std::vector< std::shared_ptr<const Result> > EnemyAI::MakeNextDecisions() const
  581. {
  582.     std::vector< std::shared_ptr<const Result> > finalResults;
  583.     std::vector<std::string> names;
  584.  
  585.     struct itemType
  586.     {
  587.         int number;
  588.         std::string findString;
  589.     };
  590.  
  591.     std::vector<struct itemType> itemTypes;
  592.     itemTypes.push_back({1, ": HP"});
  593.     itemTypes.push_back({1, ": DMG"});
  594.     itemTypes.push_back({1, ": SPLL"});
  595.  
  596.     switch (rand_num<int>(1, 4)) // Picks a random number
  597.     {
  598.     case 2://this determines the number the difficulty level will be divided by that
  599.         //determines the num of that item
  600.         for (itemType i : itemTypes)
  601.         {
  602.             if (i.findString == ": HP")
  603.                 i.number = 2;
  604.             else if (i.findString == ": DMG")
  605.                 i.number = 4;
  606.             else if (i.findString == ": SPLL")
  607.                 i.number = 5;
  608.         }
  609.     case 3:
  610.         for (itemType i : itemTypes)
  611.         {
  612.             if (i.findString == ": HP")
  613.                 i.number = 5;
  614.             else if (i.findString == ": DMG")
  615.                 i.number = 4;
  616.             else if (i.findString == ": SPLL")
  617.                 i.number = 2;
  618.         }
  619.     case 4:
  620.         for (itemType i : itemTypes)
  621.         {
  622.             if (i.findString == ": HP")
  623.                 i.number = 4;
  624.             else if (i.findString == ": DMG")
  625.                 i.number = 2;
  626.             else if (i.findString == ": SPLL")
  627.                 i.number = 5;
  628.         }
  629.     }
  630.    
  631.    
  632.     for (itemType j : itemTypes)
  633.     {
  634.         for (int k = 0; k < (m_difficultyLevel / j.number); k++) // This determines the amount of items it will use
  635.         {
  636.             for (const std::shared_ptr<Item> & i : m_offensiveEntity->GetItems()) // This block of code goes through all possible items
  637.             {
  638.                 for (std::string j : names) // finds name that equals item name
  639.                 {
  640.                     if (j == i->GetName()) // if item name already in list continue
  641.                         continue;
  642.                 }
  643.                 if (i->GetName().find(j.findString)) // It finds an name that is in the specified Item
  644.                 {
  645.                     if ((i->Use() == std::make_shared<const Result>(0, 0, 0)) != false) // checks if item has no more durability
  646.                     {
  647.                         finalResults.push_back(std::make_shared<Result>(*(i->Use()))); // NOTE: Special is never specified because Weapon.Use() picks randomly
  648.                         names.push_back(i->GetName());
  649.                     }
  650.                 }
  651.             }
  652.         }
  653.     }
  654.     std::cout << "Its the enmies turn! The enemy attacks with items: \n";
  655.     for (std::string i : names)
  656.         std::cout << " : " << i;
  657.     std::cout << "\n";
  658.     return finalResults;
  659. }
  660.  
  661. //PlayerInterface definitions
  662.  
  663. std::vector < std::shared_ptr<const Result> > PlayerInterface::MakeNextDecisions() const
  664. {
  665.     std::vector < std::shared_ptr<const Result> > finalResults; // vector that will be returned
  666.     std::vector <std::string> names; // names of items currently using durring turn
  667.  
  668.     std::cout << "Its your turn! What items will you use?/n";
  669.     std::cout << "Current items in pouch:\n";
  670.     for (std::shared_ptr<Item> & i : m_offensiveEntity->GetItems()) // prints out all items
  671.         std::cout << " : " << i->GetName();
  672.     while (true)
  673.     {
  674.         std::cout << "Current energy: " << m_offensiveEntity->GetEnergy();
  675.         if ((m_offensiveEntity->GetEnergy() == 0) || (m_offensiveEntity->GetEnergy() < 0)) // breaks if no more energy
  676.         {
  677.             std::cout << "No more energy";
  678.             break;
  679.         }
  680.         std::cout << "\nCurrent items chosen: ";
  681.         for (std::string i : names)
  682.         {
  683.             std::cout << " : " << i;
  684.         }
  685.  
  686.         std::cout << " \nDo you want to use another item? 1 Yes and 2 No.\n";
  687.         int input;
  688.         std::cin >> input;
  689.         if (input == 2)
  690.             break;
  691.  
  692.         std::string temp;
  693.         std::cout << "\n Enter Item: ";
  694.         std::getline(std::cin, temp);
  695.         for (std::string i : names) // loops through names. if name already there than continues to next prompt
  696.         {
  697.             if (temp == i)
  698.             {
  699.                 std::cout << "Item already chosen";
  700.                 continue;
  701.             }
  702.         }
  703.         for (const std::shared_ptr<Item> & i : m_offensiveEntity->GetItems())
  704.         {
  705.             if (i->GetName() == temp)
  706.             {
  707.                 if (i->Use() == std::make_shared<const Result>(Result(0, 0, 0)))
  708.                 {
  709.                     std::cout << "Item broken. Try again.";
  710.                     break;
  711.                 }
  712.                 finalResults.push_back(std::make_shared<Result>(*(i->Use())));
  713.                 m_offensiveEntity->SubtractEnergy(i->Use()->m_energyCost);
  714.                 names.push_back(i->GetName());
  715.                 continue;
  716.             }
  717.            
  718.         }
  719.         std::cout << "Item not found. Please try again.";
  720.     }
  721.     int totalDamage = 0;
  722.     std::cout << "Total damage: ";
  723.     for (std::shared_ptr<const Result> & i : finalResults)
  724.     {
  725.         totalDamage += i->m_totalDamage;
  726.     }
  727.     std::cout << totalDamage;
  728.     return finalResults;
  729. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement