Advertisement
chopical

Testing Code

Mar 4th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. ////////////////////// Game.cpp //////////////////////
  2. #include <../Head.hpp>
  3.  
  4. void Start(Item &Root){
  5.     Root.Retrieve("Exit Status").SetDescription("Ok!");
  6. }
  7.  
  8. ////////////////////// Engine.cpp //////////////////////
  9. #include <../Head.hpp>
  10.  
  11. void Start(Item &Root);
  12.  
  13. int main( int argc, char* args[] ){
  14.     Item Root("Root");
  15.     Item Tmp("Exit Status");
  16.     Tmp.SetDescription("?");
  17.     Root.Add(Tmp);
  18.     Start(Root);
  19.     cout << Root.Retrieve("Exit Status").GetDescription() << endl;
  20. return 0;}
  21.  
  22. ////////////////////// Head.hpp //////////////////////
  23. #include <iostream>
  24. #include <string>
  25. #include <vector>
  26. #include <Item.h>
  27. #define string std::string
  28. #define cout std::cout
  29. #define cin std::cin
  30. #define endl std::endl
  31. #define cerr std::cerr
  32. #define string std::string
  33. #define vector std::vector
  34.  
  35. ////////////////////// Item.h //////////////////////
  36. #ifndef ITEM_H
  37. #define ITEM_H
  38.  
  39. #include "../Head.hpp"
  40.  
  41. class Item
  42. {
  43.     vector<string> Names;
  44.     vector<Item> Contents;
  45.     float X=0,Y=0,Quantity=1,Weight=1;
  46.     string Name="Root",Description;
  47.     void Increment(int index);
  48.     void Decrement(int index);
  49. public:
  50.     Item(string nName,float nX=1,float nY=1,float nWeight=1);
  51.     void Add(Item item);
  52.     void Remove(Item item);
  53.     int Find(string Item_Name);
  54.     string GetName();
  55.     float GetWeight();
  56.     string GetDescription();
  57.     void SetDescription(string Item_Name);
  58.     void SetName(string Item_Name);
  59.     Item Retrieve(string Item_Name);
  60. };
  61.  
  62. #endif // ITEM_H
  63.  
  64. ////////////////////// Item.cpp //////////////////////
  65. #include "Item.h"
  66.  
  67. Item::Item(string nName,float nX,float nY,float nWeight){
  68.     Name=nName;
  69.     Weight=nWeight;
  70.     X=nX;
  71.     Y=nY;
  72. }
  73.  
  74. Item Item::Retrieve(string Item_Name){
  75.     int found = Find(Item_Name);
  76.     return Contents[found];
  77. }
  78.  
  79. string Item::GetName(){
  80.     return Name;
  81. }
  82.  
  83. float Item::GetWeight(){
  84.     return Weight;
  85. }
  86.  
  87. string Item::GetDescription(){
  88.     return Description;
  89. }
  90. void Item::SetDescription(string Item_Name){
  91.     Description = Item_Name;
  92. }
  93.  
  94. void Item::SetName(string Item_Name){
  95.     Name=Item_Name;
  96. }
  97.  
  98. void Item::Add(Item item){
  99.     int found = Find(item.Name);
  100.         if(!found){
  101.             Names.push_back(item.Name);
  102.             Contents.push_back(item);
  103.             Weight = Weight + item.Weight;
  104.         }else{
  105.             Contents[found].Increment(found);
  106.         }
  107. }
  108.  
  109. void Item::Remove(Item item){
  110.     int found = Find(item.Name);
  111.     if(found){
  112.         if(found > 1){
  113.             Contents[found].Decrement(found);
  114.         }else{
  115.             vector<Item> tmplist;
  116.             for(int a=0;a<Contents.size();++a){
  117.                 if(Contents[a].Name != Contents[found].Name){
  118.                     tmplist.push_back(Contents[a]);
  119.                 }
  120.             }
  121.             Contents.clear();
  122.             for(int a=0;a<tmplist.size();++a){
  123.                     Contents.push_back(tmplist[a]);
  124.                 }
  125.             }
  126.         }
  127. }
  128.  
  129. int Item::Find(string Item_Name){
  130.     int result=0;
  131.     for(int a=0;a < Contents.size();++a){
  132.         if(Contents[a].Name == Item_Name){
  133.             result = a;
  134.         }
  135.     }
  136.     return result;
  137. }
  138.  
  139. void Item::Increment(int index){
  140.     Contents[index].Quantity+1;
  141. }
  142. void Item::Decrement(int index){
  143.     Contents[index].Quantity-1;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement