Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. typedef unsigned uint;
  5.  
  6. using namespace std;
  7.  
  8. class Player
  9. {
  10. private:
  11.     const static uint PLAYER_BAG_SIZE = 50;
  12.     string m_strPlayerName;
  13.     int bag[PLAYER_BAG_SIZE];
  14. public:
  15.     Player(string NAME)
  16.     {
  17.         m_strPlayerName = NAME;
  18.         for (uint i = 0; i < 50; ++i)
  19.         {
  20.             bag[i] = 0;
  21.         }
  22.     }
  23.  
  24.     void WriteBagContent()
  25.     {
  26.         cout << GetPlayerName() << "'s bag: " << endl;
  27.         for (uint i = 0; i < 50; ++i)
  28.         {
  29.             cout << "Slot: " << i << " contains: " << bag[i] << endl;
  30.         }
  31.     }
  32.  
  33.     void AddItem(int nItemID)
  34.     {
  35.         for (uint i = 0; i < 50; ++i)
  36.         {
  37.             while (bag[i] != 0)
  38.             {
  39.                 i++;
  40.             }
  41.             if (bag[i] == 0)
  42.             {
  43.                 bag[i] = nItemID;
  44.                 break;
  45.             }
  46.         }
  47.     }
  48.  
  49.  
  50.     void CraftItem(int nItemID)
  51.     {
  52.         //Troche głupi ale działający handler poszczególnych itemów do craftingu
  53.         if (nItemID == 1)//np. pochodnia (ID = 1)
  54.         {
  55.             if (HasItemCount(10, 4) && HasItemCount(11, 1))
  56.             {
  57.                 AddItem(1);
  58.             }
  59.             else return;
  60.  
  61.             //patyki (ID = 10), węgiel (ID = 11)
  62.         }
  63.         else if (nItemID == 2)//np. włócznia
  64.         {
  65.             if (HasItemCount(10, 2) && HasItemCount(12, 1))
  66.             {
  67.                 AddItem(2);
  68.             }
  69.             else return;
  70.  
  71.             //znów patyki (ID = 10), żelazo (ID = 13)
  72.         }
  73.         cout << "Player: " << GetPlayerName() << " has crafted item: " << nItemID << endl;
  74.         //Na nic lepszego nie wpadłem :(
  75.     }
  76.  
  77.     bool HasItemCount(int nItemID, int count)
  78.     {
  79.         int tempcount = 0;
  80.         for (uint i = 0; i < 50; ++i)
  81.         {
  82.             if (bag[i] == nItemID)
  83.             {
  84.                 tempcount++;
  85.             }
  86.         }
  87.         if (tempcount == count || tempcount > count)
  88.             return true;
  89.         else return false;
  90.  
  91.         return false;
  92.     }
  93.  
  94.     string GetPlayerName() const
  95.     {
  96.         return m_strPlayerName;
  97.     }
  98.  
  99. };
  100.  
  101.  
  102. int main()
  103. {
  104.     Player* tester = new Player("Tester");
  105.  
  106.     cout << "Player's name: " << tester->GetPlayerName() << endl;
  107.  
  108.     tester->WriteBagContent();
  109.     cout << endl;
  110.  
  111.     tester->CraftItem(1);
  112.     //Nie powiedzie się = brak przedmiotów aby stworzyć pochodnię
  113.    
  114.     tester->WriteBagContent();
  115.  
  116.     cout << endl;
  117.  
  118.     tester->AddItem(10);
  119.     tester->AddItem(10);
  120.     tester->AddItem(10);
  121.     tester->AddItem(10);
  122.     tester->AddItem(11);
  123.  
  124.     tester->WriteBagContent();
  125.  
  126.     cout << endl;
  127.  
  128.     tester->CraftItem(1);
  129.     //Powinno się powieść
  130.  
  131.     tester->WriteBagContent();
  132.     cout << endl;
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement