广告资源的动态菜单

时间:2017-01-10 14:43:39

标签: c++

我想制作一个动态菜单,用于检测您的广告资源中是否有一个或多个持久性。如果您有一个或多个,它会打印到菜单中。否则,它不会。

以下是代码:

#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;
}

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

2 个答案:

答案 0 :(得分:2)

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

  • weaponName()

  • 使用()

  • 攻击()

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

使用使用方法进行检查,如果结果是&gt; 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;
}

答案 1 :(得分:0)

回顾这篇老帖子。我修改了boring32的代码。对于那些刚接触编程的人来说,我更容易阅读。

#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)";
}

当我读到无聊的回答时,我无法阅读它,因为几乎没有任何评论和变量被命名为&#34; a&#34;,&#34; b&#34 ;和类名为&#34; A&#34;,&#34; B&#34;和与代码无关的类。我回来修理它。虽然我不确定为什么有些人习惯在论坛/帖子中留下半工作代码。

  

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