设计模式作业1

【设计模式作业1】1.一个开宝箱游戏的基本描述为:游戏中有多种类型的人物(Role),如战士(Solider)、魔法师(Mage)等,主角的类型只能选择其中一种,且游戏中不再更改。游戏中还有各种宝箱(Box),如装有不同数目金钱的宝箱、装有毒物的宝箱等。当任一种类型的主角打开装有金钱的宝箱时,宝箱中的金钱会增加给主角,同时宝箱的金钱数目变成0;当战士打开装有毒物的宝箱时,战士的生命值(HP)会减少10%,但金钱(Money)增加20%;当魔法师打开装有毒物的宝箱时,魔法师的生命值(HP)会减少30%,但金钱(Money)增加40% 。
请根据上述描述,给出相应类的设计并完整实现,要求你的设计应具有良好的扩展性,如增加新角色类型及箱子种类时,不需要修改已有的设计及实现。

2.给出适当的类设计和相应的代码:有一个只能放进不能取出的盒子, 最多可放8个水果, 不一定一天放入。水果只是苹果和桔子两种, 它们放入盒子前的原始重量分别为50和30, 放入盒子后, 由于丢失水分, 它们的重量减轻, 苹果和桔子每天分别减轻4和3, 直到达到各自原始重量的3/5后, 不再减轻重量。盒子的功能有:输出盒子中苹果的数量; 输出盒子中桔子的数量; 输出一天来盒子中水果减轻的总重量; 输出当前水果的总重量。

1. class Box; class Role { public: Role(int theMoney,int theHP):money(theMoney),hp(theHP) { } virtual ~Role( ) {} int GetMoney( ) const { return money; } void SetMoney(int m) { money = m; } void Open (Box& aBox) { aBox.BeOpened(*this); } virtual void PoisonHurt( ) = 0; protected: int money; int hp; }; class Solider:public Role { public: Solider(int theMoney,int theHP):Role(theMoney,theHP) { } virtual void PoisonHurt( ) { hp *=0.9; money *=1.2; } }; class Mage:public Role { public: Mage (int theMoney,int theHP):Role(theMoney,theHP) { } virtual void PoisonHurt( ) { hp *=0.7; money *=1.4; } }; class Box { public: virtual ~Box( ) {} virtual void BeOpened(Role& role) = 0; }; class MoneyBox:public Box { public: MoneyBox(int m):boxMoney(m) { } virtual void BeOpened(Role& role) { role.SetMoney(role.GetMoney( ) + boxMoney ); boxMoney = 0; } protected: int boxMoney; }; class PoisonBox:public Box { public: virtual void BeOpened(Role& role) { role. PoisonHurt ( ); } };


2. class Fruit { public: Fruit(int mMax =0,int mMin=0,int mLose = 0,int mW = 0) :mMaxWeight(mMax),mMinWeight(mMin),mLoseWeight(mLose),mWeigth(mW) {} virtual ~Fruit() {}virtual Fruit * Clone( ) const = 0; virtual int ReduceWeight( ); virtual intWeight( ) const; protected: intmMaxWeight; intmMinWeight; intmLoseWeight; intmWeight; }; int Fruit::ReduceWeight( ) { int newWeight = mWeight - mLoseWeight; if (newWeight < mMinWeight ) newWeight = mMinWeight; int reduce =mWeight - newWeight; mWeight = newWeight; return reduce; }intFruit::Weight( ) const { returnmWeight; }class Apple:public Fruit { public: Apple():Fruit(50,50*3/5,4,50) { } virtual ~Apple( ) {} virtual Apple * Clone() const { return new Apple(*this); }// other }; class Orange:public Fruit { public: Orange():Fruit(30,30*3/5,3,30) { } virtual ~Orange( ) {} virtual Orange * Clone() const { return new Orange(*this); }// other }; class Box { public: Box():count(0) {for(int i=0; i<8; i++) fruit[i]=0; } ~Box() {for(int i=0; i<8; i++) delete fruit[i]; } void AddFruit(Fruit& one)//放入一个水果 {if (count<8) fruit[count++]=one.Clone(); }intApplesNum() const { int num=0; for(int i=0; i<8; i++) { Apple * p=dynamic_cast(fruit[i]); if (p) ++num; } return num; } intOrangesNum() const { int num=0; for(int i=0; i<8; i++) { Orange * p=dynamic_cast(fruit[i]); if(p) ++num; } return num; } intPassOneDay()//一天来失去的总重量 { int num=0; for(int i=0; i<8; i++) if(fruit[i]) num+=fruit[i]->ReduceWeight(); return num; } intTotalWeight() const { int num=0; for(int i=0; i<8; i++) if(fruit[i]) num+=fruit[i]->Weight(); return num; } void Show() const { cout<<"盒子里共有苹果="<


    推荐阅读