Advertisement
Guest User

Untitled

a guest
May 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <vector>
  5.  
  6. //DEKLARACE CLASSY
  7. class Item {
  8. private:
  9. std::string name;
  10. unsigned int max_amount;
  11. int id; //Kdybych potřeboval z itemu vytáhnout jeho ID (a ne naopak - jeho objekt)
  12.  
  13. public:
  14. std::string getName();
  15. unsigned int getMaxAmount();
  16. int getId();
  17.  
  18. Item();
  19. Item(std::string n, unsigned int m_a);
  20. };
  21.  
  22. //Deklarace a definice zvlášť aby jsi mohl udělat vector instancí ITEMU, by to neznalo že classa ITEM existuje bez rozdělení deklarace a definice protože v definici
  23. //používám ten vektor
  24. std::vector<Item> items; //Pro jednoduché číselné orientování v itemech, za sebou podle toho jak se přídávají
  25.  
  26. //DEFINICE CLASSY
  27.  
  28.  
  29. Item::Item() {
  30. this->name = "";
  31. this->max_amount = 1;
  32. this->id = -1;
  33. }
  34.  
  35. Item::Item(std::string n, unsigned int m_a)
  36. : name(n), max_amount(m_a) {
  37. this->id = items.size();
  38. items.push_back(*this);
  39. }
  40.  
  41. std::string Item::getName() { return this->name; }
  42. unsigned int Item::getMaxAmount() { return this->max_amount; }
  43. int Item::getId() { return this->id; }
  44.  
  45. Item nothing("", 1); //id = 0
  46. Item wood("Wood", 5); //id = 1
  47. Item stick("Stick", 15); //id = 2
  48. Item apple("Apple", 8); // id = 3
  49. Item pickaxe("Pickaxe", 1); // id = 4
  50.  
  51. class Inventory { //Classa pro každý inventářový slot
  52. private:
  53. Item item;
  54. unsigned int amount;
  55.  
  56. public:
  57. Inventory() {
  58. this->item = nothing; //nothing je objekt itemu
  59. this->amount = 0;
  60. }
  61.  
  62. void setItem(unsigned int id, unsigned int amount) {
  63. this->item = items[id];
  64.  
  65. if (id != 0) {
  66. this->amount = amount;
  67. }
  68. }
  69.  
  70. void setItem(Item item, unsigned int amount) {
  71. this->item = item;
  72. this->amount = amount;
  73. }
  74.  
  75. Item getItem() {
  76. return this->item;
  77. }
  78.  
  79. unsigned int getAmount() {
  80. return this->amount;
  81. }
  82. };
  83.  
  84. int main() {
  85. Inventory inv[10];
  86.  
  87. //inv[SLOT].(CO CHCEŠ O NĚM VĚDĚT) <- bez závorek ofc
  88.  
  89. srand(time(NULL));
  90.  
  91. for(unsigned int i = 0; i < 10; i++) {
  92. unsigned int pos = rand()%(items.size());
  93. unsigned int amount = rand()%(items[pos].getMaxAmount() - 1 + 1) + 1;
  94.  
  95. inv[i].setItem(pos, amount);
  96. }
  97.  
  98. for(unsigned int i = 0; i < 10; i++) {
  99. std::cout << i + 1 << ". " << inv[i].getItem().getName();
  100.  
  101. if (inv[i].getItem().getId() > 0) { //Jen na ukázání "využití"
  102. std::cout << " (" << inv[i].getItem().getId() << ")";
  103.  
  104. if (inv[i].getAmount() > 1) { //Nevypisovat zbytečně x1 protože to víme že to tam máme že.. XD
  105. std::cout << " x" << inv[i].getAmount();
  106. }
  107. }
  108.  
  109. std::cout << std::endl;
  110. }
  111.  
  112. std::cin.get();
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement