/*
*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;
}
第6周项目2-角色武器装备
最新推荐文章于 2024-09-09 00:24:05 发布