Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ Battle Text Game created by Justin Gonzales
- // Header.h
- #pragma once
- #include <iostream>
- #include <string>
- #include <vector>
- #include <memory>
- #include <random>
- class GameEntity
- {
- protected:
- const std::string m_name;
- int m_hp;
- public:
- GameEntity() {}
- GameEntity(std::string name, int hp) : m_name(name), m_hp(hp){} // NOTE: Get() functions are
- // justifyable under this hierchy
- const std::string GetName() const { return m_name; }
- const int GetHP() const { return m_hp; }
- // NOT VIRTUAL BECAUSE doesnt need to be changed
- };
- struct Result
- {
- Result() : m_energyCost(0), m_totalDamage(0), m_effect(nothing) {}
- Result(const int energyCost, const int totalDamage, const int effect) : m_energyCost(energyCost), m_totalDamage(totalDamage)
- {
- switch (effect)
- {
- case 0:
- m_effect = nothing;
- case 1:
- m_effect = fire;
- case 2:
- m_effect = freeze;
- }
- }
- bool Result::operator==(const Result result);
- enum Effect // local scope Effect enum
- {
- freeze,
- fire,
- nothing,
- };
- const int m_energyCost;
- const int m_totalDamage;
- Effect m_effect;
- };
- class Item : public GameEntity // NOTE: With polymorphism, the game will declare vectors to smart pointers'
- // to Items. This allows functions to use an item as a weapon or item.
- {
- protected:
- const std::shared_ptr<const Result> m_mainResult;
- public:
- Item() {}
- Item(std::string name, int hp, const Result mainResult) :
- GameEntity(name, hp), m_mainResult(std::make_shared<const Result>(mainResult)) {}
- const int GetDurability() const;
- virtual std::shared_ptr<const Result> Use();
- };
- class Weapon : public Item
- {
- private:
- const std::shared_ptr<const Result> m_specialResult;
- public:
- Weapon(std::string name, int hp, const Result mainResult, const Result specialResult):
- Item(name, hp, mainResult), m_specialResult(std::make_shared<const Result>(specialResult)) {}
- virtual std::shared_ptr<const Result> Use(); // NOTE: Giving the Weapon this functionality might be a
- // problem. The game will have to check if the item is a weapon or not to use the m_specail result.
- // This goes against normal polymorphic practices. But since this is a very small game,
- // I think we can sacrifice using dynamic_cast once.
- };
- class OffensiveEntity : public GameEntity
- {
- protected:
- std::vector< std::shared_ptr<Item> > m_items;
- Result::Effect m_currentEffect;
- public:
- OffensiveEntity(std::string name, int hp) : GameEntity(name, hp) {}
- void ClearItems();
- std::vector<std::shared_ptr<Item>> GetItems(std::vector<std::string> names) const;
- std::vector<std::shared_ptr<Item>> GetItems() const;
- void AddItems(std::vector<std::shared_ptr<Item>> items);
- virtual void TakeResults(std::vector<std::shared_ptr<const Result>> results);
- virtual const int GetEnergy() const { return 0; } // These are here so the Player could work better with the Interface class
- virtual void SubtractEnergy(int sub) const { }
- virtual void Reset(); //NOTE: This class has a Reset function so it can be polymorphic with the Player class
- // to be used in the interface classes. This function resets the current effect and changes the hp accordingly.
- // The player function adds the added functionality of reseting the energy aswell.
- };
- class Player : public OffensiveEntity // NOTE: Enemies are offensive entities because
- // I couldn't find a difference between enemies and this base class.
- // The player is different because he has energy.
- {
- private:
- const int MAX_ENERGY = 10;
- int m_currentEnergy;
- public:
- Player(std::string name, int hp) : OffensiveEntity(name, hp),
- m_currentEnergy(MAX_ENERGY) {}
- void TakeResults(std::vector<std::shared_ptr<const Result>> results);
- virtual const int GetEnergy() const { return m_currentEnergy; } // These are here so the Player could work better with the Interfave class
- virtual void SubtractEnergy(const int sub) { m_currentEnergy -= sub; }
- void Reset();
- };
- class OffensiveEntityInterface
- {
- protected:
- std::shared_ptr<OffensiveEntity> m_offensiveEntity;
- public:
- OffensiveEntityInterface() {}
- OffensiveEntityInterface(std::string name, int hp)
- { m_offensiveEntity = std::make_shared<OffensiveEntity>((OffensiveEntity(name, hp))); }
- OffensiveEntityInterface(OffensiveEntity o)
- { m_offensiveEntity = std::make_shared<OffensiveEntity>(o); }
- virtual void Equip(std::vector<std::shared_ptr<Item>> items); // This function just calls AddItems()
- // I don't know if that is invalid hierachercally, but it's the best idea when it comes to this
- // class.
- std::vector<std::shared_ptr<Item>> GetEquiped() { return m_offensiveEntity->GetItems(); } // for debug reasons
- virtual std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const = 0; // NOTE: Only pure virtual function because
- // cannot have an implementaion for this base class. And will not need to create any of these objects.
- };
- class EnemyAI : public OffensiveEntityInterface
- {
- private:
- const int m_difficultyLevel; //NOTE: This variable is not in a class for Enemies for instance.
- // NOTE: These functions are private because nothing else needs to use them
- std::shared_ptr<Item> CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect) const;
- std::shared_ptr<Item> CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect, int specialEnergyCost,
- int specialTotalDamage, Result::Effect specialEffect) const;
- public:
- EnemyAI(std::string name, int hp, int difficulty) :
- OffensiveEntityInterface(name, hp), m_difficultyLevel(difficulty) {} //NOTE: Calls OffensiveEntityInterface's constructor because
- // Enemies are just OffensiveEntities
- EnemyAI(OffensiveEntity o, int difficulty) : OffensiveEntityInterface(o), m_difficultyLevel(difficulty) {}
- virtual void Equip(); // NOTE: This gives the Enemy random weapons based on its dificulty level.
- std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const; // NOTE: This function makes random choices on what weapons
- // it will use in one turn based on the difficulty level. The enemy doesn't have energy because it is an uneeded complexity.
- };
- class PlayerInterface : public OffensiveEntityInterface
- { // NOTE: Only class that doesn't have any added private member variables because it can use polymorphism to
- // just reuse code. Specifically, realocating the offensiveEntity variable to a Player object.
- public:
- PlayerInterface(std::string name, int hp)
- {m_offensiveEntity = std::make_shared<Player>(Player(name, hp));}
- std::vector<std::shared_ptr<const Result>> MakeNextDecisions() const; // NOTE: this function prompts the user
- // on what items to use and how much energy they have and what items they have chose to use.
- // This is why it is redefined here.
- };
- class Game
- {
- private:
- std::vector<std::shared_ptr<EnemyAI>> m_enemies;
- PlayerInterface m_player;
- };
- // Main.cpp. Test file
- #include "Header.h"
- int main()
- {
- std::shared_ptr<EnemyAI> offensiveEntityInterface =
- std::make_shared<EnemyAI>(EnemyAI("Dank Memerson", 50, 1));
- std::vector<std::shared_ptr<Item>> tempItems;
- tempItems.push_back(std::make_shared<Item>(Item("Dank Doodles", 20, Result(2, 40, Result::Effect::freeze))));
- tempItems.push_back(std::make_shared<Item>(Item("Dank Doodlea", 36, Result(4, 56, Result::Effect::fire))));
- offensiveEntityInterface->OffensiveEntityInterface::Equip(tempItems);
- for (auto & i : offensiveEntityInterface->GetEquiped())
- {
- std::cout << i->GetName() << " name \n";
- std::cout << i->GetHP() << " durability \n";
- std::shared_ptr<const Result> tempResult = i->Use();
- std::cout << tempResult->m_totalDamage << " total damage \n";
- std::cout << tempResult->m_energyCost << " energy cost \n";
- std::cout << tempResult->m_effect << " effect \n";
- }
- offensiveEntityInterface->Equip();
- for (auto & i : offensiveEntityInterface->GetEquiped())
- {
- std::cout << i->GetName() << " name \n";
- std::cout << i->GetHP() << " durability \n";
- std::shared_ptr<const Result> tempResult = i->Use();
- std::cout << tempResult->m_totalDamage << " total damage \n";
- std::cout << tempResult->m_energyCost << " energy cost \n";
- std::cout << tempResult->m_effect << " effect \n";
- }
- std::cin.get();
- return 0;
- }
- // MemberDefinitions.h
- #include "Header.h"
- // random number generator from Stroustrup:
- // http://www.stroustrup.com/C++11FAQ.html#std-random
- template<class T>
- T rand_num(const T & low, const T & high)
- {
- static std::default_random_engine re{};
- using Dist = std::uniform_int_distribution<T>;
- static Dist uid{};
- return uid(re, Dist::param_type{ low,high });
- }
- //Result definitions
- bool Result::operator==(const Result result)
- {
- if ((result.m_effect == m_effect) || (result.m_energyCost == m_energyCost)
- || (result.m_totalDamage == m_totalDamage))
- return true;
- else
- return false;
- }
- //Item definitions
- std::shared_ptr<const Result> Item::Use()
- {
- if ((m_hp != 0) && (m_hp > 0))
- {
- m_hp -= m_mainResult->m_energyCost;
- return std::make_shared<const Result>(*m_mainResult);
- }
- else
- {
- return std::make_shared<const Result>(0, 0, 0);
- }
- }
- const int Item::GetDurability() const
- {
- return m_hp;
- }
- //Weapon definitions
- std::shared_ptr<const Result> Weapon::Use()
- {
- bool special = false;
- if (rand_num<int>(1, 3) == 2)
- special = false;
- else
- special = true;
- if ((m_hp != 0) && (m_hp > 0))
- {
- if (special == true)
- {
- m_hp -= m_specialResult->m_energyCost;
- return std::make_shared<const Result>(*m_specialResult);
- }
- else
- {
- m_hp -= m_mainResult->m_energyCost;
- return std::make_shared<const Result>(*m_mainResult);
- }
- }
- else
- return std::make_shared<const Result>(0, 0, 0);
- }
- //OffensiveEntity definitions
- void OffensiveEntity::ClearItems()
- {
- m_items.clear();
- }
- // Iterates through items to find if their name is the same as the names specified
- std::vector<std::shared_ptr<Item>> OffensiveEntity::GetItems(std::vector<std::string> names) const
- {
- std::vector<std::shared_ptr<Item>> items;
- for (const std::shared_ptr<Item> & i : m_items)
- {
- for (std::string j : names)
- {
- if (i->GetName() == j) // Checks if name is equal
- items.push_back(std::make_shared<Item>(*i)); // Assigns item to vector
- }
- }
- return items;
- }
- std::vector<std::shared_ptr<Item>> OffensiveEntity::GetItems() const
- {
- return m_items;
- }
- void OffensiveEntity::AddItems(std::vector<std::shared_ptr<Item>> items)
- {
- for (auto & i : items)
- {
- m_items.push_back(std::make_shared<Item>(*i));
- }
- }
- void OffensiveEntity::TakeResults(std::vector<std::shared_ptr<const Result>> results)
- {
- for (auto & i : results)
- {
- m_hp -= i->m_totalDamage;
- m_currentEffect = i->m_effect;
- }
- }
- void OffensiveEntity::Reset()
- {
- switch (m_currentEffect)
- {
- case (Result::Effect::freeze):
- m_hp -= 10;
- m_currentEffect = Result::Effect::nothing;
- break;
- case (Result::Effect::fire):
- m_hp -= 20;
- m_currentEffect = Result::Effect::nothing;
- break;
- }
- }
- //Player definitions
- void Player::Reset()
- {
- int oldHp = m_hp;
- m_currentEnergy = MAX_ENERGY;
- OffensiveEntity::Reset();
- switch (m_currentEffect)
- {
- case (Result::Effect::freeze):
- m_currentEnergy -= 3;
- break;
- case (Result::Effect::fire):
- m_currentEnergy -= 1;
- break;
- }
- std::cout << "You received " << oldHp - m_hp << " damage from current effect\n";
- }
- void Player::TakeResults(std::vector<std::shared_ptr<const Result>> results)
- {
- int oldHp = m_hp;
- OffensiveEntity::TakeResults(results);
- for (const std::shared_ptr<const Result> & i : results)
- {
- m_currentEnergy -= i->m_energyCost;
- }
- std::cout << "You received " << oldHp - m_hp << " damage\n";
- }
- //OffensiveEntityInterface definitions
- void OffensiveEntityInterface::Equip(std::vector<std::shared_ptr<Item>> items)
- {
- m_offensiveEntity->AddItems(items);
- }
- //EnemyAI definitions
- std::shared_ptr<Item> EnemyAI::CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect) const
- {
- std::shared_ptr<const Result> tempResult = std::make_shared<const Result>(mainEnergyCost, mainTotalDamage, mainEffect);
- std::shared_ptr<Item> tempItem = std::make_shared<Item>(name, hp, *tempResult);
- return std::make_shared<Item>(*tempItem);
- }
- std::shared_ptr<Item> EnemyAI::CreateTempItem(std::string name, int hp, int mainEnergyCost, int mainTotalDamage, Result::Effect mainEffect, int specialEnergyCost,
- int specialTotalDamage, Result::Effect specialEffect) const
- {
- std::shared_ptr<const Result> tempMainResult = std::make_shared<const Result>(mainEnergyCost, mainTotalDamage, mainEffect);
- std::shared_ptr<const Result> tempSpecialResult = std::make_shared<const Result>(specialEnergyCost, specialTotalDamage, specialEffect);
- std::shared_ptr<Item> tempItem = std::make_shared<Weapon>(Weapon(name, hp, *tempMainResult, *tempSpecialResult));
- return std::make_shared<Item>(*tempItem);
- }
- // This function assigns different names and different levels for different
- // weapons and items. These items are then assigned to the m_items member variable.
- // Values are hard coded because I didn't know how to use any kind of parser like xml.
- void EnemyAI::Equip()
- {
- m_offensiveEntity->ClearItems();
- std::vector<std::shared_ptr<Item>> tempItems;
- int tempRandom = 0;
- switch (m_difficultyLevel)
- {
- case 0:
- case 1:
- {
- tempRandom = rand_num<int>(2, 4);
- if ((tempRandom == 1) || (tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4))
- tempItems.push_back(CreateTempItem("Health Potion : HP", 3, 3, -10, Result::Effect::nothing));
- if (tempRandom == 3)
- tempItems.push_back(CreateTempItem("Wooden Stick : DMG", 5, 2, 10, Result::Effect::nothing, 3, 13, Result::Effect::nothing));
- break;
- }
- case 2:
- {
- tempRandom = rand_num<int>(1, 4);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4))
- tempItems.push_back(CreateTempItem("Health Potion : HP", 3, 3, -15, Result::Effect::nothing));
- if ((tempRandom == 3) || (tempRandom == 4))
- tempItems.push_back(CreateTempItem("Slightly Better Wooden Stick : DMG", 5, 2, 12, Result::Effect::nothing, 3, 15, Result::Effect::nothing));
- if (tempRandom == 2)
- tempItems.push_back(CreateTempItem("Mini Knife : DMG", 3, 1, 7, Result::Effect::nothing, 2, 11, Result::Effect::nothing));
- break;
- }
- case 3:
- {
- tempRandom = rand_num<int>(1, 5);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
- tempItems.push_back(CreateTempItem("Freeze Potion : SPLL", 3, 3, 4, Result::Effect::freeze));
- if ((tempRandom == 2) || (tempRandom == 3))
- tempItems.push_back(CreateTempItem("Wooden Sword : DMG", 5, 2, 16, Result::Effect::nothing, 3, 27, Result::Effect::nothing));
- if (tempRandom == 4)
- tempItems.push_back(CreateTempItem("Rock : DMG", 4, 1, 12, Result::Effect::nothing));
- if ((tempRandom == 2) || (tempRandom == 5))
- tempItems.push_back(CreateTempItem("Steak : HP", 2, 2, -20, Result::Effect::nothing));
- break;
- }
- case 4:
- {
- tempRandom = rand_num<int>(1, 6);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
- tempItems.push_back(CreateTempItem("Fire Potion : SPLL", 3, 3, 10, Result::Effect::fire));
- if ((tempRandom == 3) || (tempRandom == 4))
- tempItems.push_back(CreateTempItem("Frozen Wooden Sword : DMG", 4, 3, 16, Result::Effect::freeze, 4, 27, Result::Effect::nothing));
- if (tempRandom == 4)
- tempItems.push_back(CreateTempItem("Broom on Fire : DMG", 1, 1, 13, Result::Effect::fire));
- if ((tempRandom == 5) || (tempRandom == 6))
- tempItems.push_back(CreateTempItem("Magic Potato : HP", 2, 2, -20, Result::Effect::nothing));
- if ((tempRandom == 7))
- tempItems.push_back(CreateTempItem("Stone Sword : DMG", 6, 2, 21, Result::Effect::nothing, 3, 32, Result::Effect::nothing));
- break;
- }
- case 5:
- {
- tempRandom = rand_num<int>(1, 6);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
- {
- tempItems.push_back(CreateTempItem("Super Health Potion : HP", 3, 3, -20, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Stone Sword : DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
- }
- if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
- {
- tempItems.push_back(CreateTempItem("Stone Sword: DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Stone Sword: DMG", 6, 3, 21, Result::Effect::nothing, 4, 32, Result::Effect::nothing));
- }
- if (tempRandom == 4)
- tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 15, Result::Effect::freeze));
- if ((tempRandom == 2))
- tempItems.push_back(CreateTempItem("Iron Rod : DMG", 2, 2, 20, Result::Effect::fire));
- if ((tempRandom == 6))
- tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -26, Result::Effect::nothing));
- break;
- }
- case 6:
- {
- tempRandom = rand_num<int>(1, 6);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
- {
- tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Iron Rod : DMG", 2, 2, 20, Result::Effect::fire));
- }
- if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
- {
- tempItems.push_back(CreateTempItem("Stone Sword: DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Stone Sword: DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
- }
- if (tempRandom == 4)
- {
- tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 21, Result::Effect::fire));
- tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 11, Result::Effect::freeze));
- }
- if ((tempRandom == 2))
- tempItems.push_back(CreateTempItem("Fired Up Stone Sword : DMG", 7, 3, 27, Result::Effect::nothing, 4, 39, Result::Effect::nothing));
- if ((tempRandom == 6))
- tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
- break;
- }
- case 7:
- {
- tempRandom = rand_num<int>(1, 7);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6) || (tempRandom == 7))
- tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
- if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5))
- tempItems.push_back(CreateTempItem("Iron Sword: DMG", 9, 3, 34, Result::Effect::nothing, 4, 51, Result::Effect::nothing));
- if (tempRandom == 4)
- {
- tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
- tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 24, Result::Effect::freeze));
- }
- if ((tempRandom == 2))
- tempItems.push_back(CreateTempItem("Pearl Rod : DMG", 2, 2, 44, Result::Effect::nothing));
- if ((tempRandom == 6))
- tempItems.push_back(CreateTempItem("WaterShroom : HP", 2, 2, -37, Result::Effect::nothing));
- if ((tempRandom == 7))
- tempItems.push_back(CreateTempItem("Summon Slash : SPLL", 3, 3, 61, Result::Effect::nothing));
- break;
- }
- case 10:
- {
- tempRandom = rand_num<int>(1, 7);
- if ((tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6) || (tempRandom == 7))
- {
- tempItems.push_back(CreateTempItem("Super Health Potion : HP", 2, 2, -20, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Eye of Diamond : SPLL", 2, 2, 34, Result::Effect::freeze));
- }
- if ((tempRandom == 3) || (tempRandom == 4) || (tempRandom == 5) || (tempRandom == 6))
- tempItems.push_back(CreateTempItem("Steel Sword: DMG", 11, 3, 51, Result::Effect::nothing, 4, 76, Result::Effect::nothing));
- if (tempRandom == 4)
- {
- tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
- tempItems.push_back(CreateTempItem("Fire Potion: SPLL", 3, 3, 31, Result::Effect::fire));
- tempItems.push_back(CreateTempItem("Freeze Potion: SPLL", 3, 3, 24, Result::Effect::freeze));
- }
- if ((tempRandom == 2))
- {
- tempItems.push_back(CreateTempItem("Diamond Rod : DMG", 2, 2, 87, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Steel Sword on Fire: DMG", 11, 3, 42, Result::Effect::fire, 4, 76, Result::Effect::fire));
- }
- if ((tempRandom == 6))
- {
- tempItems.push_back(CreateTempItem("WaterShroom : HP", 2, 2, -37, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
- tempItems.push_back(CreateTempItem("Large Steak : HP", 2, 2, -30, Result::Effect::nothing));
- }
- if ((tempRandom == 7))
- {
- tempItems.push_back(CreateTempItem("Diamond Blade: DMG", 13, 3, 91, Result::Effect::nothing, 4, 127, Result::Effect::nothing));
- }
- break;
- }
- }
- m_offensiveEntity->AddItems(tempItems);
- }
- std::vector< std::shared_ptr<const Result> > EnemyAI::MakeNextDecisions() const
- {
- std::vector< std::shared_ptr<const Result> > finalResults;
- std::vector<std::string> names;
- struct itemType
- {
- int number;
- std::string findString;
- };
- std::vector<struct itemType> itemTypes;
- itemTypes.push_back({1, ": HP"});
- itemTypes.push_back({1, ": DMG"});
- itemTypes.push_back({1, ": SPLL"});
- switch (rand_num<int>(1, 4)) // Picks a random number
- {
- case 2://this determines the number the difficulty level will be divided by that
- //determines the num of that item
- for (itemType i : itemTypes)
- {
- if (i.findString == ": HP")
- i.number = 2;
- else if (i.findString == ": DMG")
- i.number = 4;
- else if (i.findString == ": SPLL")
- i.number = 5;
- }
- case 3:
- for (itemType i : itemTypes)
- {
- if (i.findString == ": HP")
- i.number = 5;
- else if (i.findString == ": DMG")
- i.number = 4;
- else if (i.findString == ": SPLL")
- i.number = 2;
- }
- case 4:
- for (itemType i : itemTypes)
- {
- if (i.findString == ": HP")
- i.number = 4;
- else if (i.findString == ": DMG")
- i.number = 2;
- else if (i.findString == ": SPLL")
- i.number = 5;
- }
- }
- for (itemType j : itemTypes)
- {
- for (int k = 0; k < (m_difficultyLevel / j.number); k++) // This determines the amount of items it will use
- {
- for (const std::shared_ptr<Item> & i : m_offensiveEntity->GetItems()) // This block of code goes through all possible items
- {
- for (std::string j : names) // finds name that equals item name
- {
- if (j == i->GetName()) // if item name already in list continue
- continue;
- }
- if (i->GetName().find(j.findString)) // It finds an name that is in the specified Item
- {
- if ((i->Use() == std::make_shared<const Result>(0, 0, 0)) != false) // checks if item has no more durability
- {
- finalResults.push_back(std::make_shared<Result>(*(i->Use()))); // NOTE: Special is never specified because Weapon.Use() picks randomly
- names.push_back(i->GetName());
- }
- }
- }
- }
- }
- std::cout << "Its the enmies turn! The enemy attacks with items: \n";
- for (std::string i : names)
- std::cout << " : " << i;
- std::cout << "\n";
- return finalResults;
- }
- //PlayerInterface definitions
- std::vector < std::shared_ptr<const Result> > PlayerInterface::MakeNextDecisions() const
- {
- std::vector < std::shared_ptr<const Result> > finalResults; // vector that will be returned
- std::vector <std::string> names; // names of items currently using durring turn
- std::cout << "Its your turn! What items will you use?/n";
- std::cout << "Current items in pouch:\n";
- for (std::shared_ptr<Item> & i : m_offensiveEntity->GetItems()) // prints out all items
- std::cout << " : " << i->GetName();
- while (true)
- {
- std::cout << "Current energy: " << m_offensiveEntity->GetEnergy();
- if ((m_offensiveEntity->GetEnergy() == 0) || (m_offensiveEntity->GetEnergy() < 0)) // breaks if no more energy
- {
- std::cout << "No more energy";
- break;
- }
- std::cout << "\nCurrent items chosen: ";
- for (std::string i : names)
- {
- std::cout << " : " << i;
- }
- std::cout << " \nDo you want to use another item? 1 Yes and 2 No.\n";
- int input;
- std::cin >> input;
- if (input == 2)
- break;
- std::string temp;
- std::cout << "\n Enter Item: ";
- std::getline(std::cin, temp);
- for (std::string i : names) // loops through names. if name already there than continues to next prompt
- {
- if (temp == i)
- {
- std::cout << "Item already chosen";
- continue;
- }
- }
- for (const std::shared_ptr<Item> & i : m_offensiveEntity->GetItems())
- {
- if (i->GetName() == temp)
- {
- if (i->Use() == std::make_shared<const Result>(Result(0, 0, 0)))
- {
- std::cout << "Item broken. Try again.";
- break;
- }
- finalResults.push_back(std::make_shared<Result>(*(i->Use())));
- m_offensiveEntity->SubtractEnergy(i->Use()->m_energyCost);
- names.push_back(i->GetName());
- continue;
- }
- }
- std::cout << "Item not found. Please try again.";
- }
- int totalDamage = 0;
- std::cout << "Total damage: ";
- for (std::shared_ptr<const Result> & i : finalResults)
- {
- totalDamage += i->m_totalDamage;
- }
- std::cout << totalDamage;
- return finalResults;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement