【经典】C++&RPG对战游戏

博文背景:

  还记大二上学期的时候看的这个C++&RPG游戏(博主大一下学期自学的php,涵盖oop内容),一个外校的同学他们大一学的C++,大二初期C++实训要求做一个程序填空,就是这个 RPG 对战游戏,有几处空需要填上,问我可以不,我就信誓旦旦的说OK,最后“万般无奈多方考究”才给将程序补充完整。最近在做毕业设计,用到C++&QT oop部分,重新温习了下C++ oop,就想起来大二的时候还“捣鼓”过这么个游戏,现重新看了下,进行整理总结,写下该博文。

--------------------华丽的分割线,开始正题-----------------

本RPG游戏介绍:

  用户创建一个玩家,与AI进行PK,PK中,可以普通攻击(随机暴击、躲闪)、技能攻击、加血、加蓝。本游戏中指定了5个对手,任务失败或者打败所有对手游戏结束。

代码架构:

mian.cpp:控制游戏流程.

container.h container.cpp:物品类.

player.h player.cpp:角色类.

swordsman.h swordsman.cpp:剑士角色.

archer.h archer.cpp:弓箭手角色.

mage.h mage.cpp:魔法师角色.

每个角色都需要物品,每个角色都继承与角色类。

全部代码如下:

 //=======================
// main.cpp
//======================= // main function for the RPG style game #include <iostream>
#include <string>
using namespace std; #include "swordsman.h"
#include "archer.h"
#include "mage.h" int main()
{
string tempName;
bool success=; //flag for storing whether operation is successful
cout <<"请输入玩家姓名:";
cin >>tempName; // get player's name from keyboard input
player *human; // use pointer of base class, convenience for polymorphism
int tempJob; // temp choice for job selection
do
{
cout <<"请选择职业: 1 战士, 2 弓箭手, 3 法师"<<endl;
cin>>tempJob;
system("cls"); // clear the screen
switch(tempJob)
{
case :
human=new swordsman(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
case :
human=new archer(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
case :
human=new mage(,tempName); // create the character with user inputted name and job
success=; // operation succeed
break;
default:
break; // In this case, success=0, character creation failed
}
}
while(success!=); // so the loop will ask user to re-create a character int tempCom; // temp command inputted by user
int nOpp=; // the Nth opponent
for(int i=; nOpp<; i+=) // i is opponent's level
{
nOpp++;
system("cls");
cout<<"STAGE" <<nOpp<<endl;
cout<<"你的对手, 一个 Lv. "<<i<<" 战士"<<endl;
system("pause");
swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
human->reFill(); // get HP/MP refill before start fight while(!human->death() && !enemy.death()) // no died
{
success=;
while (success!=)
{
showinfo(*human,enemy); // show fighter's information
cout<<"请选择: "<<endl;
cout<<"1 攻击; 2 技能; 3 使用体力药水; 4 使用魔法药水; 0 离开游戏"<<endl;
cin>>tempCom;
switch(tempCom)
{
case :
cout<<"确定退出吗? Y/N"<<endl;
char temp;
cin>>temp;
if(temp=='Y'||temp=='y')
return ;
else
break;
case :
success=human->attack(enemy);
human->isLevelUp();
enemy.isDead();
break;
case :
success=human->specialatt(enemy);
human->isLevelUp();
enemy.isDead();
break;
case :
success=human->useHeal();
break;
case :
success=human->useMW();
break;
default:
break;
}
}
if(!enemy.death()) // If AI still alive
enemy.AI(*human);
else // AI died
{
cout<<"YOU WIN"<<endl;
human->transfer(enemy); // player got all AI's items
}
if (human->death())
{
system("cls");
cout<<endl<<setw()<<"GAME OVER"<<endl;
delete human;//6_??????????? // player is dead, program is getting to its end, what should we do here?
system("pause");
return ;
}
}
}
delete human;//7_????????? // You win, program is getting to its end, what should we do here?
system("cls");
cout<<"恭喜你! 你击败了所有对手!!"<<endl;
system("pause");
return ;
}
 //=======================
// container.h
//======================= // The so-called inventory of a player in RPG games
// contains two items, heal and magic water #ifndef _CONTAINER//1_????????????? // Conditional compilation
#define _CONTAINER class container // Inventory
{
protected:
int numOfHeal; // number of heal
int numOfMW; // number of magic water
public:
container(); // constuctor
void set(int heal_n, int mw_n); // set the items numbers
int nOfHeal(); // get the number of heal
int nOfMW(); // get the number of magic water
void display(); // display the items;
bool useHeal(); // use heal
bool useMW(); // use magic water
}; #endif
 //=======================
// container.cpp
//=======================
#include "container.h"
#include <iostream>
using namespace std;
// default constructor initialise the inventory as empty
container::container()
{
set(,);
} // set the item numbers
void container::set(int heal_n, int mw_n)
{
numOfHeal=heal_n;
numOfMW=mw_n;
} // get the number of heal
int container::nOfHeal()
{
return numOfHeal;
} // get the number of magic water
int container::nOfMW()
{
return numOfMW;
} // display the items;
void container::display()
{
cout<<"你的物品: "<<endl;
cout<<"恢复(HP+100): "<<numOfHeal<<endl;
cout<<"魔法药水 (MP+80): "<<numOfMW<<endl;
} //use heal
bool container::useHeal()
{
numOfHeal--;//2_????????
return ; // use heal successfully
} //use magic water
bool container::useMW()
{
numOfMW--;
return ; // use magic water successfully
}
 //=======================
// player.h
//======================= // The base class of player
// including the general properties and methods related to a character #ifndef _PLAYER
#define _PLAYER #include <iomanip> // use for setting field width
#include <time.h> // use for generating random factor
#include "container.h"
#include <string>
#include <windows.h>
using namespace std; enum job {sw, ar, mg}; /* define 3 jobs by enumerate type
sword man, archer, mage */
class player
{
friend void showinfo(player &p1, player &p2);
friend class swordsman;
friend class archer;
friend class mage; protected:
int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
// General properties of all characters
string name; // character name
job role; /* character's job, one of swordman, archer and mage,
as defined by the enumerate type */
container bag; // character's inventory public:
virtual bool attack(player &p)=; // normal attack
virtual bool specialatt(player &p)=; //special attack
virtual void isLevelUp()=; // level up judgement
/* Attention!
These three methods are called "Pure virtual functions".
They have only declaration, but no definition.
The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
The detailed definition of these pure virtual functions will be given in subclasses. */ void reFill(); // character's HP and MP resume
bool death(); // report whether character is dead
void isDead(); // check whether character is dead
bool useHeal(); // consume heal, irrelevant to job
bool useMW(); // consume magic water, irrelevant to job
void transfer(player &p); // possess opponent's items after victory
void showRole(); // display character's job private:
bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
}; #endif
 //=======================
// player.cpp
//======================= #include "player.h"
#include <iostream>
//using namespace std;
// character's HP and MP resume
void player::reFill()
{
HP=HPmax; // HP and MP fully recovered
MP=MPmax;
} // report whether character is dead
bool player::death()
{
return playerdeath;
} // check whether character is dead
void player::isDead()
{
if(HP<=) // HP less than 0, character is dead
{
cout<<name<<" 死亡." <<endl;
system("pause");
playerdeath=; // give the label of death value 1
}
} // consume heal, irrelevant to job
bool player::useHeal()
{
if(bag.nOfHeal()>)
{
HP=HP+;
if(HP>HPmax) // HP cannot be larger than maximum value
HP=HPmax; // so assign it to HPmax, if necessary
cout<<name<<" 使用恢复, HP 增加 100."<<endl;
bag.useHeal(); // use heal
system("pause");
return ; // usage of heal succeed
}
else // If no more heal in bag, cannot use
{
cout<<"对不起, 你没有足够的恢复."<<endl;
system("pause");
return ; // usage of heal failed
}
} // consume magic water, irrelevant to job
bool player::useMW()
{
if(bag.nOfMW()>)
{
MP=MP+;
if(MP>MPmax)
MP=MPmax;
cout<<name<<" 使用魔法药水, MP 增加 100."<<endl;
bag.useMW();
system("pause");
return ; // usage of magic water succeed
}
else
{
cout<<"对不起,你没有足够的魔法药水."<<endl;
system("pause");
return ; // usage of magic water failed
}
} // possess opponent's items after victory
void player::transfer(player &p)
{
cout<<name<<" 获得"<<p.bag.nOfHeal()<<" 恢复, 和 "<<p.bag.nOfMW()<<" 魔法药水."<<endl;
system("pause");
bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());//3_???????????
// set the character's bag, get opponent's items
} // display character's job
void player::showRole()
{
switch(role)
{
case sw:
cout<<"战士";
break;
case ar:
cout<<"弓箭手";
break;
case mg:
cout<<"法师";
break;
default:
break;
}
} // display character's job
void showinfo(player &p1, player &p2)//4_??????????????
{
system("cls");
cout<<"##############################################################"<<endl;
cout<<"# 玩家"<<setw()<<p1.name<<" LV. "<<setw() <<p1.LV
<<" # 对手"<<setw()<<p2.name<<" LV. "<<setw() <<p2.LV<<" #"<<endl;
cout<<"# HP "<<setw()<<(p1.HP<=?p1.HP:)<<'/'<<setw()<<(p1.HPmax<=?p1.HPmax:)
<<" | MP "<<setw()<<(p1.MP<=?p1.MP:)<<'/'<<setw()<<(p1.MPmax<=?p1.MPmax:)
<<" # HP "<<setw()<<(p2.HP<=?p2.HP:)<<'/'<<setw()<<(p2.HPmax<=?p2.HPmax:)
<<" | MP "<<setw()<<(p2.MP<=?p2.MP:)<<'/'<<setw()<<(p2.MPmax<=?p2.MPmax:)<<" #"<<endl;
cout<<"# AP "<<setw()<<(p1.AP<=?p1.AP:)
<<" | DP "<<setw()<<(p1.DP<=?p1.DP:)
<<" | speed "<<setw()<<(p1.speed<=?p1.speed:)
<<" # AP "<<setw()<<(p2.AP<=?p2.AP:)
<<" | DP "<<setw()<<(p2.DP<=?p2.DP:)
<<" | speed "<<setw()<<(p2.speed<=?p2.speed:)<<" #"<<endl;
cout<<"# EXP"<<setw()<<p1.EXP<<" 职业: "<<setw();
p1.showRole();
cout<<" # EXP"<<setw()<<p2.EXP<<" 职业: "<<setw();
p2.showRole();
cout<<" #"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
p1.bag.display();
cout<<"##############################################################"<<endl;
}
 //=======================
// swordsman.h
//======================= // Derived from base class player
// For the job Swordsman #include "player.h"
class swordsman :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
swordsman(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// swordsman.cpp
//=======================
#include "swordsman.h"
#include <iostream> // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void swordsman::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 4 变成 "<<AP<<endl;
cout<<"DP 增加 5 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool swordsman::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)((1.0*AP/p.DP)*AP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)((1.0*AP/p.DP)*AP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool swordsman::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void swordsman::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
 //=======================
// archer.h
//======================= // Derived from base class player
// For the job archer #include "player.h"
class archer :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
archer(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// archer.cpp
//=======================
#include "archer.h"
#include <iostream>
#include <cstdio>
#include <windows.h>
// constructor. default values don't need to be repeated here
archer::archer(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void archer::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 2 变成 "<<AP<<endl;
cout<<"DP 增加 6 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool archer::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)(DP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)(DP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool archer::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void archer::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
 //=======================
// mage.h
//======================= // Derived from base class player
// For the job mage #include "player.h"
class mage :public player// 5_????????? // subclass swordsman publicly inherited from base player
{
public:
mage(int lv_in=, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};
 //=======================
// mage.cpp
//=======================
#include "mage.h"
#include <iostream> // constructor. default values don't need to be repeated here
mage::mage(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=+*(LV-); // HP increases 8 point2 per level
HP=HPmax;
MPmax=+*(LV-); // MP increases 2 points per level
MP=MPmax;
AP=+*(LV-); // AP increases 4 points per level
DP=+*(LV-); // DP increases 4 points per level
speed=+*(LV-); // speed increases 2 points per level playerdeath=;
EXP=(LV-)*(LV-)*;
bag.set(lv_in, lv_in);
} void mage::isLevelUp()
{
if(EXP>=LV*LV*)
{
LV++;
AP+=;
DP+=;
HPmax+=;
MPmax+=;
speed+=;
cout<<name<<" Level UP!"<<endl;
cout<<"HP 增加 8 变成 "<<HPmax<<endl;
cout<<"MP 增加 2 变成 "<<MPmax<<endl;
cout<<"Speed 增加 2 变成 "<<speed<<endl;
cout<<"AP 增加 4 变成 "<<AP<<endl;
cout<<"DP 增加 5 变成 "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool mage::attack(player &p)
{
double HPtemp=; // opponent's HP decrement
double EXPtemp=; // player obtained exp
double hit=; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)(AP*/(rand()%+)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"的攻击伤害了 "<<p.name<<", "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%<))
{
cout<<name<<"的攻击被 "<<p.name<<"躲避"<<endl;
system("pause");
return ;
} // 10% chance give critical attack
if (rand()%<=)
{
hit=1.5;
cout<<"关键的攻击: ";
} // Normal attack
HPtemp=(int)(AP*/(rand()%+));
cout<<name<<"使用猛击, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return ; // Attack success
} bool mage::specialatt(player &p)
{
if(MP<)
{
cout<<"你没有足够的魔法值!"<<endl;
system("pause");
return ; // Attack failed
}
else
{
MP-=; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%<=)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return ;
} double HPtemp=;
double EXPtemp=;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" 使用技能, "<<p.name<<"的 HP 下降 "<<HPtemp<<endl;
cout<<name<<" 获得 "<<EXPtemp<<" 经验."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return ; // special attack succeed
} // Computer opponent
void mage::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+<=1.1*HPmax)&&(bag.nOfHeal()>)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>= && HP>0.5*HPmax && rand()%<=)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP< && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
上一篇:蓝牙的L2CAP协议


下一篇:Java基础--二维数组