您的位置:首页 > 其它

第6周项目2-角色武器装备

2016-04-14 08:58 162 查看
/*
*Copyright(C) 2016,计算机与控制工程学院
*All rights reserved.
*文件名:test.cpp
*作者:张志新
*完成日期:2016年4月10日
*版本号:v1.0
*
*问题描述:带武器的游戏类。
*/
#include <iostream>
#include <string>
using namespace std;
class Weapon
{
public:
Weapon(string weap,int f);
int getForce();
private:
string wName;
int force;
};
Weapon::Weapon (string weap, int f):wName(weap),force(f)
{
}
int Weapon::getForce()
{
return force;
}
class Role
{
public:
Role(string nm,int xie,string w,int f);
~Role();
void show();
void attack(Role &r);
void eat(int food);
bool isAlived();
private:
Weapon weapon;
string name;
int blood;
bool life;
};
Role::Role(string nm,int xie,string weap,int f):name(nm),blood(xie),weapon(weap,f)
{
if(blood>0)
life=true;
else
life=false;
}
Role::~Role()
{
cout<<name<<"退出江湖!"<<endl;
}

bool Role::isAlived()
{
if(blood>0)
return true;
else
return false;
}
void Role::show()
{
cout<<name<<" blood is "<<blood<<" he is ";
if(isAlived())
{
cout<<"alived"<<endl;
}
else
{
cout<<"dead"<<endl;
}

}
void Role::attack(Role &r)
{
if(isAlived())
{
blood+=weapon.getForce();
r.blood-=weapon.getForce();
if(r.blood<=0)
r.life=false;
}
}
void Role::eat(int food)
{
if(isAlived())
blood=blood+food;
}
int main( )
{
Role qiangu("花千骨", 100, "断念",1000);
Role mantian("霓漫天", 100, "屠龙", 500);
qiangu .show();
mantian.show();
cout<<"霓漫天攻击花千骨"<<endl;
mantian.attack(qiangu);
qiangu.show();
mantian.show();
cout<<"花千骨攻击霓漫天"<<endl;
qiangu.attack(mantian);
qiangu.show();
mantian.show();
cout<<"end......"<<endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: