首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >库存动态菜单

库存动态菜单
EN

Stack Overflow用户
提问于 2017-01-10 14:43:39
回答 2查看 248关注 0票数 0

我想要做一个动态菜单,其中它检测您是否有一个或多个耐用性在您的库存。如果您有一个或多个,它打印到菜单中。否则就不会了。

下面是代码:

代码语言:javascript
复制
#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;
}

我在这里的问题是,持久性会被归零并被删除,然后在放置另一个选项之后,控制台就会崩溃。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-05 09:54:13

回过头来看这个旧的岗位。我从boring32中修改了代码。我让那些刚开始编程的人更容易阅读。

代码语言:javascript
复制
#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)";
}

那时,当我读博林32的答案时,我无法阅读它,因为几乎没有任何注释和变量被命名为"a“、"b”和类名为"A“、"B”以及与代码无关的类。我回来是想修好它。虽然我不知道为什么有些人会习惯于在论坛/线程中保留半工作代码。

无意冒犯那些也这么做的人。

票数 0
EN

Stack Overflow用户

发布于 2017-01-10 15:24:02

尝试一种不同的方法,创建具有一些纯虚拟函数的父类武器。

  • weaponName()
  • 使用()
  • 攻击()

然后创建继承武器类的类,并相应地实现这些类。

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

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

更新:

代码语言:javascript
复制
#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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41571507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档