#include<iostream>
#include<string>
using namespace std;
enum DamageType { Physical, Magic, Heal };
class Mage
{
public:
string name;
int maxhp, hp;
double physicalResistance, magicResistance;
Mage(string name, int hp) :name(name), maxhp(hp), hp(hp)
{
physicalResistance = 0;
magicResistance = 0.3;
}
void showIntro()
{
cout << name << " HP: " << hp << endl;
}
bool isAlive()
{
if (hp <= 0)
return false;
else
return true;
}
void destroy()
{
hp = 0;
cout << name << " is destroyed." << endl;
}
template<typename T> void reduceHP(T& source, int damage, DamageType dt)
{
int realDamage = damage;
if (dt == DamageType::Magic)
{
realDamage = (int)(realDamage * (1 - magicResistance) + 0.5);
}
else if (dt == DamageType::Physical)
{
realDamage = (int)(realDamage * (1 - physicalResistance) + 0.5);
}
hp -= realDamage;
cout << name << " receive " << realDamage << " damage from " << source.name << "." << endl;
if (!isAlive())
{
destroy();
}
}
void increaseHP(int healpoint)
{
hp += healpoint;
if (hp > maxhp)
{
hp = maxhp;
}
}
template<typename T> void fireball(T& target)
{
attack(target, "Fireball", DamageType::Magic, 1000);
}
template<typename T> void pyroblast(T& target)
{
attack(target, "Pyroblast", DamageType::Magic, 5000);
}
template<typename T> void attack(T& target, string attackname, DamageType dt, int damage)
{
cout << name << " casts " << attackname << ", dealing " << damage << " damage to " << target.name << "." << endl;
target.reduceHP(*this, damage, dt);
}
};
//Paladin类的定义
class Paladin
{
public:
string name;
int maxhp, hp;
double physicalResistance, magicResistance;
Paladin(string name, int hp) :name(name), maxhp(hp), hp(hp)
{
physicalResistance = 0.25;
magicResistance = 0.1;
}
void showIntro()
{
cout << name << " HP: " << hp << endl;
}
bool isAlive()
{
if (hp <= 0)
{
return false;
}
else return true;
}
void destory()
{
hp = 0;
cout << name << " is destroyed." << endl;
}
template<typename T>
void reduceHP(T& source, int damage, DamageType dt)
{
int realDamage = damage;
if (dt == DamageType::Magic)
{
realDamage = (int)(realDamage * (1 - magicResistance) + 0.5);
}
else if (dt == DamageType::Physical)
{
realDamage = (int)(realDamage * (1 - physicalResistance) + 0.5);
}
hp -= realDamage;
cout << name << " receive " << realDamage << " damage from " << source.name << "." << endl;
if (!isAlive())
{
destory();
}
}
void increaseHP(int healpoint)
{
hp += healpoint;
if (hp > maxhp)
{
hp = maxhp;
}
}
template<typename T>
void verdict(T& target)
{
attack(target, "Templar's Verdict", DamageType::Physical, 3000);
}
void flash()
{
heal(*this, "Flash of Light", DamageType::Heal, 1500);
}
template<typename T>
void attack(T& target, string attackname, DamageType dt, int damage)
{
cout << name << " casts " << attackname << ", dealing " << damage << " damage to " << target.name << "." << endl;
target.reduceHP(*this, damage, dt);
}
template<typename T>
void heal(T& target, string healname, DamageType dt, int healpoint)
{
cout << name << " heals " << target.name << " " << healpoint << " Points." << endl;
target.increaseHP(healpoint);
}
};
int main()
{
Mage mag("Jiana", 20000);
Paladin pal("Arthus", 25000);
mag.showIntro();
pal.showIntro();
while (mag.isAlive() && pal.isAlive())
{
cout << "----------" << endl;
if (mag.isAlive())
{
mag.fireball(pal);
}
if (pal.isAlive())
{
pal.flash();
}
cout << "----------" << endl;
mag.showIntro();
pal.showIntro();
if (!mag.isAlive() || !pal.isAlive())
{
break;
}
cout << "----------" << endl;
if (mag.isAlive())
{
mag.pyroblast(pal);
}
if (pal.isAlive())
{
pal.verdict(mag);
}
cout << "----------" << endl;
mag.showIntro();
pal.showIntro();
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
第7次(类模板、成员函数模板).zip
共3个文件
cpp:3个
需积分: 0 0 下载量 60 浏览量
2024-05-10
13:23:31
上传
评论
收藏 2KB ZIP 举报
温馨提示
第7次(类模板、成员函数模板).zip
资源推荐
资源详情
资源评论
收起资源包目录
第7次(类模板、成员函数模板).zip (3个子文件)
第7次(类模板、成员函数模板)
类模板-1.CPP 859B
成员函数模板-2.CPP 4KB
成员函数模板-1.CPP 786B
共 3 条
- 1
资源评论
yyyyujun
- 粉丝: 0
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功