🎒Backpack

Scrivere una classe che rappresenti uno zaino di un personaggio di un videogame. II personaggio può usare oggetti di tipo Potion, Weapon e Scroll, attraverso il loro metodo use(); questi oggetti devono poter essere messi dentro lo zaino e sono caratterizzati dall'avere un certo peso. Lo zaino deve avere un numero massimo di oggetti che può contenere e così come un peso complessivo massimo degli oggetti contenuti; questi due parametri sono impostati nel costruttore dello zaino. 'inventario può avere una dimensione massima, es. può essere richiesto che l'inventario contenga un numero massimo di elementi, specificando un parametro del costruttore. Deve essere possibile inserire un elemento in coda nell'inventario o in una certa posizione. Deve essere possibile eliminare un elemento in una certa posizione o usare un'oggetto in una certa posizione (invocando il metodo use()). Se si cerca di inserire un elemento in una posizione oltre il limite o cancellare o prendere un elemento in una posizione non consentita si deve

Weight non richiesto. mi stava esplodendo il cervello fraaaaa

Risposta Accettata
main.cpp
#include <iostream>
#include <list>
using namespace std;

class Item{
    public:
    int weight=0;
    int size=0;
    virtual void ItemUse()=0;
    virtual int getWeight() const = 0;
};

class Potion : public Item{
private:
    int weight = 1;
    int size = 1;

public:

    int getWeight() const override {
        return weight;
    }

    void ItemUse() override {
        cout << "Potion used" << endl;
    }
};

class Weapon : public Item{
private:
    int weight = 2;
    int size = 1;

public:

    int getWeight() const override {
        return weight;
    }

    void ItemUse() override {
        cout << "Weapon used" << endl;
    }
};

class Scroll : public Item{
private:
    int weight = 0;
    int size = 1;

public:

    int getWeight() const override {
        return weight;
    }

    void ItemUse() override {
        cout << "Scroll used" << endl;
    }
};

class BackPack {
private:
    int maxWeight {10};
    int maxSize {10};
    int TotalWeight {0};

public:

    BackPack(int mS, int maxW) : maxSize(mS), maxWeight(maxW) {}

    list<Item *> Items;

    void addItem(Item *item) {

        if (TotalWeight + item->weight <= maxWeight || Items.size() < maxSize) {
            Items.push_back(item); // add the item to the end of the list
            TotalWeight = TotalWeight + item->getWeight(); // add the weight of the item to the total weight
        } else {
            cout << "Backpack is full" << endl; // print a message if the backpack is full
        }
    }

    void deleteItem(int index) {
        try {
            if (index > Items.size()) {
                throw std::runtime_error("Index out of bounds");
            } else {
                auto it = Items.begin();
                std::advance(it, index); // move the iterator to the correct position
                TotalWeight -= (*it)->weight; // subtract the weight of the item from the total weight
                Items.erase(it); // remove the item from the list
            }
        }
        catch (std::runtime_error &e) {
            cout << e.what() << endl;
        }
    }

    void useItem(int index) {
        try {
            if (index > Items.size()) {
                throw std::runtime_error("Index out"); // throw an exception if the index is out of bounds
            } else {
                auto it = Items.begin(); // create an iterator to the beginning of the list
                std::advance(it, index); // move the iterator to the correct position
                (*it)->ItemUse(); // call the ItemUse method of the item
            }
        }
        catch (std::runtime_error &e) {
            cout << e.what() << endl;
        }
    }

    void printStats() const {
        cout << "Total weight: " << TotalWeight <<"/"<< maxWeight << endl;
        cout << "Total size: " << Items.size() <<"/"<< maxSize << endl;
    }

};

int main() {
     BackPack bp(10, 10); // create a backpack with a maximum size of 10 and a maximum weight of 10
     Potion potion; // create a potion
     Weapon weapon; // create a weapon
     Scroll scroll; // create a scroll
     bp.addItem(&potion); // add the potion to the backpack
     bp.addItem(&weapon); // add the weapon to the backpack
     bp.addItem(&scroll); // add the scroll to the backpack
     bp.printStats(); // print the stats of the backpack

    return 0;
}

Last updated