用C++实现老鹰捉小鸡的小游戏之一

今天突发奇想,让文心一言生成老鹰捉小鸡的游戏代码。麻烦它真的给实现出来了。具体如下


#include 
#include 

using namespace std;

// 游戏角色类
class Role {
public:
    Role(string n) : name(n) {}
    virtual void protect() = 0;
    void sayName() {
        cout << name << ":";
    }
private:
    string name;
};

// 老鹰类
class Eagle extends Role {
public:
    Eagle(string n) : Role(n) {}
    void protect() override {
        cout << "老鹰:" << name << "来啦!" << endl;
    }
};

// 小鸡类
class Chicken extends Role {
public:
    Chicken(string n) : Role(n) {}
    void protect() override {
        cout << "小鸡:" << name << "快跑!" << endl;
    }
};

// 游戏类
class Game {
public:
    Game(Eagle* e, Chicken* c) : eagle(e), chicken(c) {}
    void play() {
        eagle->sayName();
        chicken->protect();
        cout << endl;
        chicken->sayName();
        eagle->protect();
        cout << endl;
    }
private:
    Eagle* eagle;
    Chicken* chicken;
};

int main() {
    Eagle* eagle = new Eagle("老鹰");
    Chicken* chicken = new Chicken("小鸡");
    Game game(eagle, chicken);
    game.play();
    delete eagle;
    delete chicken;
    return 0;
}

你可能感兴趣的:(c/c++,c++,开发语言,文心一言)