库存的动态菜单

Dynamic menu for inventory

本文关键字:菜单 动态      更新时间:2023-10-16

我想制作一个动态菜单,在其中检测您的库存中是否有一个或多个耐久度。如果您有一个或多个,它会打印到菜单中。否则,它不会。

代码如下:

#include <iostream>
#include <vector>
using namespace std;
class A {
protected:
    int durability = 3;
public:
    virtual void attack() { };
    virtual int usage() { return 1; };
    virtual string weaponName() { return "Sword x"; };
};
class B : public A
{
public:
    void attack() { durability--; cout << "B Attack" << endl; cout << durability; };
    string weaponName() { return "Sword B"; };
};
class C : public A
{
public:
    int usage() { return durability; };
    void attack() { durability--; cout << "C Attack" << endl;cout << durability; };
    string weaponName() { return "Sword C"; };
};
class D : public A
{
public:
    void attack() { durability--;  cout << "D Attack" << endl;cout << durability; };
    string weaponName() { return "Sword D"; };
};
int main(void)
{
B * b = new B;
C * c = new C;
D * d = new D;
    int k = 10;
    vector <A*> tableOfAs;
    tableOfAs.push_back(b);
    tableOfAs.push_back(c);
    tableOfAs.push_back(d);
    while (--k>0)
    {
        int i = 0;
        vector <A*> options;
        for (i = 0; i < tableOfAs.size(); i++)
        {
            if (tableOfAs[i]->usage() > 0){
                options.push_back(tableOfAs[i]);
        } else { delete tableOfAs[i]; }
        }
        if (options.size() == 0)
            break;
        cout << "Attack Options:" << endl;
            for (i = 0; i < options.size(); i++)
            cout << i << ". " << options[i]->weaponName().c_str() << endl;
        int choise;
        cin >> choise;
        if (choise<0 || choise > options.size()-1)
            cout << "Wrong option" << endl;
        else
            options[choise]->attack();
    }
    return 1;
}

我在这里的问题是耐用性归零并被删除,然后在放置另一个选择后,控制台崩溃。

尝试不同的方法创建一个具有一些纯虚拟功能的父类武器。

  • 武器名称()

  • 用法()

  • 攻击()

然后创建从 Weapon 类继承这些内容的类,并相应地实现。

使用

使用方法进行检查,如果结果> 0,则将其添加到类武器的指针表中。然后使用 weaponName 函数在列出选项期间打印名称,然后在选择时使用表索引中的对象的 attack() 方法。因此,如果stick_sword位于表的索引 1 中,并且您调用weaponInv[1].attack()它将调用stick_sword攻击。

以下是建议逻辑的简单演示:

更新:

#include <iostream>
#include <vector>
class A {
public:
    virtual void attack() {};
    virtual int usage() { return 1; };
    virtual std::string weaponName() { return "Sword x"; };
};
class B : public A
{
public:
    void attack() { std::cout << "B Attack" << std::endl; };
    std::string weaponName() { return "Sword B"; };
};
class C : public A
{
private:
    int durability;
public:
    C() :durability(3) {};
    int usage() { return durability; };
    void attack() { durability--; std::cout << "C Attack" << std::endl; };
    std::string weaponName() { return "Sword C"; };
};
class D : public A
{
public:
    void attack() { std::cout << "D Attack" << std::endl; };
    std::string weaponName() { return "Sword D"; };
};
int main(void)
{
    B b;
    C c;
    D d;
    int k = 10;
    std::vector <A*> tableOfAs;
    tableOfAs.push_back(&b);
    tableOfAs.push_back(&c);
    tableOfAs.push_back(&d);
    while(--k>0)
    {
        int i = 0;
        std::vector <A*> options;
        for (i = 0; i < tableOfAs.size(); i++)
        {
            if (tableOfAs[i]->usage() > 0)
                options.push_back(tableOfAs[i]);
        }
        if (options.size() == 0)
            break;
        std::cout << "Attack Options:" << std::endl;
        for (i = 0; i < options.size(); i++)
            std::cout << i << ". " << options[i]->weaponName().c_str() << std::endl;
        int choise;
        std::cin >> choise;
        if (choise<0 || choise > options.size() - 1)
            std::cout << "Wrong option" << std::endl;
        else
            options[choise]->attack();
    }
    return 1;
}

回顾这篇旧帖子。我从无聊32修改了代码。我使那些刚接触编程的人更容易阅读。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class baseClass {
public:
    virtual void attack() {};
    virtual int usage() { return 1; }
    virtual string weaponName() { return "Sword x"; }
};
class Sword : public baseClass
{
private:
    int durability;
    string name;
public:
    /*Sets the name and the durability of the sword*/
    Sword(string nameToSet, int dur) :name(nameToSet),durability(dur) {}
    int usage() { return durability; }
    /*Decreases durability by one and tells the name of the sword used in the attack*/
    void attack() { 
        durability--; 
        cout << name <<" Sword Attack" << endl;
    }
    /*returns weapon name*/
    string weaponName() { return name +" Sword"; }
};
int main()
{
    Sword sworda("One", 1), swordb("Two", 2), swordc("Three", 3);
    int loop = 10;
    int choice;
    /*Add swords into a holding container first*/
    vector <baseClass*> holdingContainer;
    holdingContainer.push_back(&sworda);
    holdingContainer.push_back(&swordb);
    holdingContainer.push_back(&swordc);
    while (--loop>0)
    {
        /*Decide whether swords in the holding container has a durability of one or more
          before adding them into the menu*/
        vector <baseClass*> swordWithDurabilityContainer;
        for (int i = 0; i < holdingContainer.size(); i++)
        {
            if (holdingContainer[i]->usage() > 0) {
                swordWithDurabilityContainer.push_back(holdingContainer[i]);
            }
        }
        /*Check if there's any items in the swordWithDurabilityContainer otherwise break out of the loop*/
        if (swordWithDurabilityContainer.size() == 0) { break; }
        /*Print the items*/
        cout << "Attack Options:" << endl;
        for (int i = 0; i < swordWithDurabilityContainer.size(); i++) {
            cout << i << ". " << swordWithDurabilityContainer[i]->weaponName().c_str() << endl;
        }
        /*Ask for user input*/
        cin >> choice;
        /*Check if the input is valid*/
        if (choice<0 || choice > swordWithDurabilityContainer.size() - 1) {
            cout << "Wrong option" << endl;
        }
        else {
            swordWithDurabilityContainer[choice]->attack();
        }
    }
    /*Notify the user that the loop has ended*/
    cout << "No more items in the list(swordWithDurabilityContainer variable)";
}

当时当我阅读boring32的答案时,我无法阅读它,因为几乎没有任何注释和变量命名为"a","b"和名为"A","B"的类以及与代码无关的类。我回来修理它。虽然我不确定为什么有些人习惯在论坛/线程中留下半工作代码。

没有冒犯那些也这样做的人。