main.cpp主函数的实现
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Hero.h"
#include "Monster.h"
#include "Weapon.h"
#include "Knife.h"
#include "DragonSword.h"
#include <ctime>
using namespace std;
void play()
{
//创建怪物
Monster * monster = new Monster;
//创建英雄
Hero * hero = new Hero;
//创建武器
Weapon * kinfe = new Knife;
Weapon * dragon = new DragonSword;
//让用户选择 武器
cout << "请选择武器:" << endl;
cout << "1. 赤手空拳" << endl;
cout << "2. 小刀" << endl;
cout << "3. 屠龙刀" << endl;
int oper;
cin >> oper;
switch (oper)
{
case 1:
cout << "你真牛X,你还是太年轻了!" << endl;
break;
case 2:
hero->EquipWeapon(kinfe);
break;
case 3:
hero->EquipWeapon(dragon);
break;
}
getchar(); //输入缓冲区里有个回车 ,多获取一次值
int round = 1;
while (true)
{
getchar();
system("cls");
cout << "----- 当前第 " << round << " 回合开始 ------" << endl;
if (hero->m_Hp <= 0)
{
cout << "英雄" << hero->m_Name << "已挂 ,游戏结束" << endl;
break;
}
hero->Attack(monster);
if (monster->m_Hp <= 0)
{
cout << "怪物" << monster->m_Name << "已挂,顺利通关" << endl;
break;
}
monster->Attack(hero);
if (hero->m_Hp <= 0)
{
cout << "英雄" << hero->m_Name << "已挂 ,游戏结束" << endl;
break;
}
cout << "英雄" << hero->m_Name << "剩余血量:" << hero->m_Hp << endl;
cout << "怪物" << monster->m_Name << "剩余血量:" << monster->m_Hp << endl;
round++;
}
delete monster;
delete hero;
delete kinfe;
delete dragon;
}
int main(){
srand((unsigned int)time(NULL));
play();
system("pause");
return EXIT_SUCCESS;
}
Hero.cpp人类对象
#include "Hero.h"
Hero::Hero()
{
this->m_Hp = 500;
this->m_Atk = 50;
this->m_Def = 50;
this->m_Name = "刘法师";
this->weapon = NULL;
}
//装备武器
void Hero::EquipWeapon(Weapon * weapon)
{
this->weapon = weapon;
cout << "英雄:" << this->m_Name << " 装备了武器 << " << this->weapon->m_WeaponName << " >> " << endl;
}
//攻击
void Hero::Attack(Monster * monster)
{
int damage = 0;
int addHp = 0;
bool isHold = false;
bool isCrit = false;
if (this->weapon == NULL) //武器为空 没有加成
{
damage = this->m_Atk;
}
else
{
//基础伤害
damage = this->m_Atk + this->weapon->getBaseDamage();
//计算吸血
addHp = this->weapon->getSuckBlood();
//计算定身
isHold = this->weapon->getHold();
//计算暴击
isCrit = this->weapon->getCrit();
}
if (isCrit) //暴击 伤害 加成
{
damage = damage * 2;
cout << "英雄的武器触发了暴击效果,怪物收到了双倍的伤害,伤害值:" << damage << endl;
}
if (isHold)
{
cout << "英雄的武器触发了定身效果,怪物停止攻击一回合"<< endl;
}
if (addHp > 0)
{
cout << "英雄的武器触发了吸血效果,英雄增加的血量为" << addHp << endl;
}
//设置怪物定身
monster->m_Hold = isHold;
//计算真实伤害
int trueDamage = (damage - monster->m_Def) > 0 ? damage - monster->m_Def : 1;
monster->m_Hp -= trueDamage;
this->m_Hp += addHp;
cout << "英雄" << this->m_Name << "攻击了敌人 " << monster->m_Name << "造成了 伤害" << trueDamage << endl;
}
Hero.h人类对象的头文件
#pragma once
#include <iostream>
#include "Weapon.h"
#include <string>
#include "Monster.h"
using namespace std;
class Monster;
class Hero
{
public:
Hero();
string m_Name; //人名
int m_Atk; //攻击力
int m_Def; // 防御力
int m_Hp; //血量
Weapon * weapon; //武器
void EquipWeapon(Weapon * weapon);
void Attack( Monster * monster );
};
Knief.h
#pragma once
#include <iostream>
#include "Weapon.h"
#include <string>
using namespace std;
class Knife :public Weapon
{
public:
Knife();
//获取基础伤害
virtual int getBaseDamage();
//获取吸血
virtual int getSuckBlood();
//获取是否定身
virtual bool getHold();
//获取是否暴击
virtual bool getCrit();
};
Knief.cpp
#include "Knife.h"
Knife::Knife()
{
this->m_BaseDamage = 10;
this->m_WeaponName = "ะกตถ";
}
int Knife::getBaseDamage()
{
return this->m_BaseDamage;
}
int Knife::getSuckBlood()
{
return 0;
}
bool Knife::getHold()
{
return false;
}
bool Knife::getCrit()
{
return false;
}
DragonSword.cpp人类的武器对象
#include "DragonSword.h"
DragonSword::DragonSword()
{
this->m_BaseDamage = 20;
this->m_WeaponName = "屠龙宝刀";
this->suckRate = 20;
this->holdRate = 30;
this->critRate = 35;
}
int DragonSword::getBaseDamage()
{
return this->m_BaseDamage;
}
int DragonSword::getSuckBlood()
{
if (isTrigger(suckRate))
{
return this->m_BaseDamage * 0.5; //按照武器基础伤害一半吸血
}
return 0;
}
bool DragonSword::getHold()
{
if (isTrigger(holdRate))
{
return true;
}
return false;
}
bool DragonSword::getCrit()
{
if (isTrigger(critRate))
{
return true;
}
return false;
}
bool DragonSword::isTrigger(int rate)
{
//通过isTrigger判断是否触发特效
//随机 1~100的数字
int num = rand() % 100 + 1;
if (num < rate)
{
return true;
}
return false;
}
DragonSword.h人类的武器对象的头文件
#pragma once
#include <iostream>
#include "Weapon.h"
#include <string>
using namespace std;
class DragonSword:public Weapon
{
public:
DragonSword();
//获取基础伤害
virtual int getBaseDamage() ;
//获取吸血
virtual int getSuckBlood();
//获取是否定身
virtual bool getHold();
//获取是否暴击
virtual bool getCrit();
// 吸血率 定身率 暴力率
int suckRate;
int holdRate;
int critRate;
//传入概率 判断是否触发
bool isTrigger(int rate);
};
Monster.cpp怪兽对象
#include "Monster.h"
Monster::Monster()
{
this->m_Hp = 300;
this->m_Atk = 70;
this->m_Def = 40;
this->m_Hold = false;
this->m_Name = "比克大魔王";
}
void Monster::Attack(Hero * hero)
{
if (this->m_Hold)
{
cout << "怪物" << this->m_Name << "被定身了,本回合无法攻击" << endl;
return;
}
//计算攻击伤害
int damage = (this->m_Atk - hero->m_Def) > 0 ? this->m_Atk - hero->m_Def : 1;
hero->m_Hp -= damage;
cout << "怪物" << this->m_Name << "攻击了英雄 " << hero->m_Name << "造成了 伤害" << damage << endl;
}
Monster.h怪兽对象的头文件
#pragma once
#include <iostream>
#include "Weapon.h"
#include <string>
#include "Hero.h"
using namespace std;
class Hero;
class Monster
{
public:
Monster();
string m_Name;
int m_Hp;
int m_Atk;
int m_Def;
bool m_Hold;
void Attack(Hero * hero);
};
Weapon.h怪兽的武器对象的头文件,里面都没有实现
#pragma once
#include <iostream>
#include <string>
using namespace std;
//抽象类
class Weapon
{
public:
//获取基础伤害
virtual int getBaseDamage() = 0;
//获取吸血
virtual int getSuckBlood() = 0;
//获取是否定身
virtual bool getHold() = 0;
//获取是否暴击
virtual bool getCrit() = 0;
string m_WeaponName; //武器名称
int m_BaseDamage; //基础伤害
};