c++设计一个小猫钓鱼的游戏程序。基本需求如下:

(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);


}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值