7月30日作业
1:将鸟笼放飞所有鸟类的题,改成观察者模式
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;//基类:鸟类
class Bird{
public:virtual void run(){cout << "鸟飞走了" << endl;}
};//派生类1:企鹅类
class Penguin:public Bird{
public:void run(){cout << "企鹅跑掉了" << endl;}
};//派生类2:鸵鸟类
class Ostrich:public Bird{
public:void run(){cout << "鸵鸟跑掉了" << endl;}
};//派生类3:老鹰类
class Eagle:public Bird{
public:void run(){cout << "老鹰飞走了" << endl;}
};//鸟笼类
class Bird_cage{
private:Bird* list[20];int count=0; //鸟数
public://将鸟关进笼子void insert(Bird* b){list[count]=b;count++;}//可以重载>>算术符便于将鸟加入笼子Bird_cage& operator>>(Bird& b){this->insert(&b);return *this;}//释放所有鸟void open_cage(){for(int i=0;i<count;i++){list[i]->run();}}
};
/*
void open_cage(Bird** cage){for(int i=0;i<4;i++){cage[i]->run();}
}*/int main(int argc,const char** argv){Bird b;Penguin p;Ostrich o;Eagle e;Bird_cage cage;cage>>b>>p>>o>>e;cage.open_cage();return 0;
}
2:使用 策略模式 + 简单工厂模式实现以下功能
有一个英雄类,拥有私有成员: hp ,atk,dep 英雄可以打怪掉落武器:怪物可以掉落3种武器: 长剑,匕首,斧头 英雄装备长剑,获得2点hp 英雄装备匕首,获得2点atk 英雄装备斧头,获得2点def 英雄装备不同的武器,使用策略模式去实现 注意:测试的时候,英雄在更换武器的时候,记得使用策略模式,将英雄之间装备的武器属性扣除后,再增加新属性 打败怪物掉落什么武器,自己设计,但是要求怪物掉落武器是一个简单工厂模式
代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;class Hero{
private:string name; // 创建英雄的名字int hp; //血量int atk; //攻击力int dep; //武器攻击力加成
public:Hero(string name="新英雄",int hp=100,int atk=5,int dep=0):name(name),hp(hp),atk(atk),dep(dep){}void showStatus(){cout << name <<":" <<endl <<"血量:"<<hp<<" 攻击力:"<<atk<<" 武器攻击力加成:"<<dep<<endl;}//设置血量void setHp(int hp=100){this->hp=hp;}//获取血量int getHp(){return this->hp;}//设置攻击力void setAtk(int atk=5){this->atk=atk;}//获取当前攻击力int getAtk(){return this->atk;}//设置武器攻击力加成void setDep(int dep=0){this->dep=dep;}//获取当前攻击力加成int getDep(){return this->dep;}
};//武器装备基类
class Weapon{
public:virtual void equip(Hero& hero)=0; //纯虚函数virtual ~Weapon(){};
};//装备武器长剑,继承于Weapon武器基类
class Longsword:public Weapon{
public:Longsword(){}void equip(Hero& hero){int new_hp=hero.getHp()+6;hero.setHp(new_hp); //长剑血量加成 6cout<<"血量增加 6"<<endl;}
};//装备武器匕首,继承于Weapon武器基类
class Dagger:public Weapon{
public:Dagger(){}void equip(Hero& hero){int new_atk=hero.getAtk()+5;hero.setDep(new_atk); //匕首攻击力加成 5cout<<"攻击力增加 5"<<endl;}
};//装备武器斧头,继承于Weapon武器基类
class Axe:public Weapon{
public:Axe(){}void equip(Hero& hero){hero.setDep(3); //斧头攻击力武器加成 3cout<<"获得武器攻击力加成 3"<<endl;}
};class Monster{
private:int hp;
public:Monster(int hp=100):hp(hp){}void atkHp(int hp){if(hp<0) hp=0; this->hp-=hp;cout<<"怪物受到 "<<hp<<" 点攻击,剩余血量: "<<this->hp<<endl;}//判断怪物是否死亡bool isDead(){return this->hp<=0;}int getHp(){return this->hp;}virtual ~Monster(){}virtual Weapon* Montowea()=0;
};class Monster_1:public Monster{
public:Monster_1():Monster(100){}Weapon* Montowea(){cout<<"恭喜获得武器 - 长剑:";return new Longsword;}
};class Monster_2:public Monster{
public:Monster_2():Monster(105){}Weapon* Montowea(){cout<<"恭喜获得武器 - 匕首:";return new Dagger;}
};class Monster_3:public Monster{
public:Monster_3():Monster(110){}Weapon* Montowea(){cout<<"恭喜获得武器 - 斧头:";return new Axe;}
};int main(int argc,const char** argv){Hero hero1("xiaohua",800,100);Monster_1 m;while(1){int choose;cout<<"请选择要打的怪物:"<<endl<<"[1]怪物1 [2]怪物2 [3]怪物3"<<endl;cout<<"请输入选择";cin>>choose;Monster* m;switch (choose){case 1:m=new Monster_1;break;case 2:m=new Monster_2;break;case 3:m=new Monster_3;break;default:cout<<"输入有误"<<endl;continue;}while(!m->isDead()){int damage=hero1.getAtk()+hero1.getDep(); //攻击力加武器加成m->atkHp(damage);hero1.showStatus();if(m->isDead()){cout<<"恭喜你!怪物已被击败"<<endl;Weapon* weapon=m->Montowea(); //怪物掉落武器weapon->equip(hero1);delete weapon;}sleep(1);}hero1.showStatus();}return 0;
}