Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <string>
- #include <vector>
- //DEKLARACE CLASSY
- class Item {
- private:
- std::string name;
- unsigned int max_amount;
- int id; //Kdybych potřeboval z itemu vytáhnout jeho ID (a ne naopak - jeho objekt)
- public:
- std::string getName();
- unsigned int getMaxAmount();
- int getId();
- Item();
- Item(std::string n, unsigned int m_a);
- };
- //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
- //používám ten vektor
- std::vector<Item> items; //Pro jednoduché číselné orientování v itemech, za sebou podle toho jak se přídávají
- //DEFINICE CLASSY
- Item::Item() {
- this->name = "";
- this->max_amount = 1;
- this->id = -1;
- }
- Item::Item(std::string n, unsigned int m_a)
- : name(n), max_amount(m_a) {
- this->id = items.size();
- items.push_back(*this);
- }
- std::string Item::getName() { return this->name; }
- unsigned int Item::getMaxAmount() { return this->max_amount; }
- int Item::getId() { return this->id; }
- Item nothing("", 1); //id = 0
- Item wood("Wood", 5); //id = 1
- Item stick("Stick", 15); //id = 2
- Item apple("Apple", 8); // id = 3
- Item pickaxe("Pickaxe", 1); // id = 4
- class Inventory { //Classa pro každý inventářový slot
- private:
- Item item;
- unsigned int amount;
- public:
- Inventory() {
- this->item = nothing; //nothing je objekt itemu
- this->amount = 0;
- }
- void setItem(unsigned int id, unsigned int amount) {
- this->item = items[id];
- if (id != 0) {
- this->amount = amount;
- }
- }
- void setItem(Item item, unsigned int amount) {
- this->item = item;
- this->amount = amount;
- }
- Item getItem() {
- return this->item;
- }
- unsigned int getAmount() {
- return this->amount;
- }
- };
- int main() {
- Inventory inv[10];
- //inv[SLOT].(CO CHCEŠ O NĚM VĚDĚT) <- bez závorek ofc
- srand(time(NULL));
- for(unsigned int i = 0; i < 10; i++) {
- unsigned int pos = rand()%(items.size());
- unsigned int amount = rand()%(items[pos].getMaxAmount() - 1 + 1) + 1;
- inv[i].setItem(pos, amount);
- }
- for(unsigned int i = 0; i < 10; i++) {
- std::cout << i + 1 << ". " << inv[i].getItem().getName();
- if (inv[i].getItem().getId() > 0) { //Jen na ukázání "využití"
- std::cout << " (" << inv[i].getItem().getId() << ")";
- if (inv[i].getAmount() > 1) { //Nevypisovat zbytečně x1 protože to víme že to tam máme že.. XD
- std::cout << " x" << inv[i].getAmount();
- }
- }
- std::cout << std::endl;
- }
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement