C++ 0730作业

2:使用策略模式+简单工厂模式实现以下功能
有一个英雄类,拥有私有成员:hp,atk,dep
英雄可以打怪掉落武器:怪物可以掉落3种武器:
        戒指,长剑,圆盾
英雄装备戒指,获得20点hp
英雄装备长剑,获得11点atk
英雄装备圆盾,获得14点def
英雄装备不同的武器,使用策略模式去实现
注意:测试的时候,英雄在更换武器的时候,记得使用策略模式,将英雄之间装备的武器属性扣除后,再增加新属性
打败怪物掉落什么武器,自己设计,但是要求怪物掉落武器是一个简单工厂模式 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Weapon;

class Hero{
private:
    int hp = 0;
    int atk = 0;
    int def = 0;
    Weapon* w = nullptr;
public:
    void setHp(int h){hp = h;}
    void setAtk(int a){atk = a;}
    void setDef(int d){def = d;}

    int getHp(){return hp;}
    int getAtk(){return atk;}
    int getDef(){return def;}
    
    void choice_weapon(Weapon* w);
    void equip_weapon();
    void show(){
        cout << "Hp = " << hp <<endl;
        cout << "Atk = " << atk <<endl;
        cout << "Def = " << def <<endl;
        cout << "=====================" <<endl;
    }
};

class Weapon{
private:

public:
    virtual void equip(Hero& hero) = 0;
    virtual void take_off_equip(Hero& hero) = 0;
    virtual ~Weapon(){}
};

class Ring:public Weapon{
private:

public:
    void equip(Hero& hero){
        cout << "Hero 装备了戒指" << endl;
        int hero_new_hp = hero.getHp() + 20;
        hero.setHp(hero_new_hp);
    }
    
    void take_off_equip(Hero& hero){
        int hero_new_hp = hero.getHp() - 20;
        hero.setHp(hero_new_hp);
    }
};

class Sword:public Weapon{
private:

public:
    void equip(Hero& hero){
        cout << "Hero 装备了长剑" << endl;
        int hero_new_Atk = hero.getAtk() + 11;
        hero.setAtk(hero_new_Atk);
    }

    void take_off_equip(Hero& hero){
        int hero_new_Atk = hero.getAtk() - 11;
        hero.setAtk(hero_new_Atk);
    }
};

class Shield:public Weapon{
private:

public:
    void equip(Hero& hero){
        cout << "Hero 装备了圆盾" << endl;
        int hero_new_Def = hero.getDef() + 14;
        hero.setDef(hero_new_Def);
    } 

    void take_off_equip(Hero& hero){
        int hero_new_Def = hero.getDef() - 14;
        hero.setDef(hero_new_Def);
    }
};

class Monster{
private:

public:
    Weapon* createWeapon(){
        int val = rand() % 3;
        if(val == 0){
            return new Ring;
        }
        else if(val == 1){
            return new Sword;
        }
        else if(val == 2){
            return new Shield;
        }
        return nullptr;
    }
};

void Hero::choice_weapon(Weapon* w){
    if(this->w != nullptr){
        this->w->take_off_equip(*this);
        delete this->w;
    }
    this->w = w;
}

void Hero::equip_weapon(){
    this->w->equip(*this);
}

int main(int argc,const char** argv)
{
	srand(time(0));
    Hero hero;
    Monster mon;
    while(1){
        Weapon* w = mon.createWeapon();
        hero.choice_weapon(w);
        hero.equip_weapon();
        hero.show();
        sleep(1);
    }
	return 0;
}


将鸟类改成观察者模式

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Bird{
private:

public:
    virtual void run(){
        cout << "小鸟 被放生了" << endl;
    }

    virtual void add(){
        cout << "放进一只 小鸟" << endl;
    }
};

class Penguin:public Bird{
private:

public:
    void run(){
        cout << "企鹅 被放生了" << endl;
    }

    void add(){
        cout << "放进一只 企鹅" << endl;
    }
};

class Ostrich:public Bird{
private:

public:
    void run(){
        cout << "鸵鸟 被放生了" << endl;
    }

    void add(){
        cout << "放进一只 鸵鸟" << endl;
    }
};

class Eagle:public Bird{
private:

public:
    void run(){
        cout << "老鹰 被放生了" << endl;
    }

    void add(){
        cout << "放进一只 老鹰" << endl;
    }
};

class User{
	Bird* list[10];
	int len = 0;
public:
	void add_list(Bird* niao){
		list[len] = niao;
		len ++;
	}

	User& operator<<(Bird& r){
		add_list(&r);
		return *this;
	}

    void show(){
        for(int i=0;i<len;i++){
            list[i]->add();
        }
        cout << "现在有鸟类数量:" << getLen() << endl;
    }

    void dsub(){
        int count = len;
        for(int i=0;i<count;i++){
            list[i]->run();
            len --;
        }
        cout << "现在有鸟类数量:" << getLen() << endl;
    }

    int getLen(){return len;}
};

int main(int argc,const char** argv)
{
	Bird b;
    Penguin p;
    Ostrich o;
    Eagle e;

    User user;

    user << b << p << o << e << b;

	user.show();

    user.dsub();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值