(1)每个小猫有自己的等级(level)和经验分(exp),每累计获得500经验分,就升一级,同时经验分清0;
(2)小猫每次只能钓一条鱼,如果钓上一条章鱼(Octopus),经验分的增加值为 2*章鱼的重量;如果钓上一条鲸鱼(Whale),经验分增加200;如果钓上一个金龟(Turtle),则等级直接升一级;如果钓上来一条鲨鱼(Shark),则在等级不变的前提下减少经验分(至多减至0),减少值为5*鲨鱼的重量。考虑到游戏的趣味性,将来可能还要增加其他类型的鱼以及相应的奖励或惩罚方法。
请根据上面的模型描述,制定合理的设计方案,请完整定义并实现小猫类,其中类的成员至少要有一个成员函数CatchFish,用来体现小猫钓鱼的行为过程。同时设计Fish类,使用继承机制从其派生出具体的鱼类。
#include<iostream>
#include<cmath>
#define _CRT_SECURE_NO_WARNINGS //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)
using namespace std;
class Fish
{
public:
Fish(int w)
{
weight = w;
point = 0;
}
virtual int Point() const
{
return 0;
}
protected:
int weight;
int point;
};
class Octopus :public Fish
{
public:
Octopus(int w) :Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Octopus" << endl;
return weight * 2;
}
private:
};
class Turtle :public Fish
{
public:
Turtle(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Turtle" << endl;
return 500;
}
private:
private:
};
class Whale :public Fish
{
public:
Whale(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Whale" << endl;
return 200;
}
private:
};
class Shark :public Fish
{
public:
Shark(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Shark" << endl;
return -(weight * 5);
}
};
class Cat
{
public:
Cat(int le = 0, int e = 0) {
level = le;
exp = e;
}
void CatchFish(Fish& f) //虚函数调用传入对象确定怎么积累经验升级
{
int t = f.Point();
if (t > 0) {
level += t / 500;
exp = t % 500;
}
else
{
exp += t;
if (exp < 0)exp = 0;
}
cout << "level:" << level << ' ' << "exp" << exp << endl;
}
private:
int level;
int exp;
};
int main()
{
Cat c;
Turtle t(20);
Octopus o(60);
Shark s(20);
c.CatchFish(t);
c.CatchFish(o);
c.CatchFish(s);
}