在接近一天的施工后,RPG游戏,基本施工完成,(4)修改 main 函数,随机生成不同角色的敌人,并保证程序正常运行。这一条会在过几天(也可能几周)的完全版发布。
我将原本的RPG游戏汉化了,并且加入了不少游戏的neta,看不懂也图个乐子( ̄▽ ̄)/
目录:
mian.cpp:主程序
container.h container.cpp:属性
player.h player.cpp:角色
swordsman.h swordsman.cpp: saber相关
archer.h archer.cpp: archer相关
mage.h mage.cpp: caster相关
main.cpp
1 //======================= 2 // main.cpp 3 //======================= 4 5 // main function for the RPG style game 6 7 #include <iostream> 8 #include <string> 9 using namespace std; 10 11 #include "swordsman.h" 12 #include"archer.h" 13 #include"mege.h" 14 15 int main() 16 { 17 string tempName; 18 bool success=0; //flag for storing whether operation is successful 19 cout << "圣杯,是源于基督的传说的奇迹之遗物" << endl << "在基督教圈内,也有颇多追寻圣杯的旅行者们的传说流传着。"<<endl<<"然后在出现的圣杯被圣堂教会判定为“真”的时候,理所当然会爆发它的争夺战。" << endl << "为了追求万能的许愿机——圣杯而发生的战斗,就是" << endl << "圣杯战争。" << endl; 20 system("pause"); 21 system("cls"); 22 cout <<"现在,你是一名御主,你的英灵名为: "; 23 cin >>tempName; // get player's name from keyboard input 24 player *human=NULL; // use pointer of base class, convenience for polymorphism 25 int tempJob; // temp choice for job selection 26 do 27 { 28 cout <<"其职阶为: 1 剑士(Saber), 2 弓兵(Archer), 3 法师(Caster)"<<endl; 29 cin>>tempJob; 30 system("cls"); // clear the screen 31 switch(tempJob) 32 { 33 case 1: 34 human=new swordsman(1,tempName); // create the character with user inputted name and job 35 success=1; // operation succeed 36 break; 37 case 2: 38 human = new archer(1, tempName); // create the character with user inputted name and job 39 success = 1; // operation succeed 40 break; 41 case 3: 42 human = new mege(1, tempName); // create the character with user inputted name and job 43 success = 1; // operation succeed 44 break; 45 default: 46 break; // In this case, success=0, character creation failed 47 } 48 }while(success!=1); // so the loop will ask user to re-create a character 49 50 int tempCom; // temp command inputted by user 51 int nOpp=0; // the Nth opponent 52 for(int i=1;nOpp<5;i+=2) // i is opponent's level 53 { 54 nOpp++; 55 system("cls"); 56 cout<<"回合" <<nOpp<<endl; 57 cout<<"你的敌人为, 一个等级为 "<<i<<" 的剑士."<<endl; 58 system("pause"); 59 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 60 human->reFill(); // get HP/MP refill before start fight 61 62 while(!human->death() && !enemy.death()) // no died 63 { 64 success=0; 65 while (success!=1) 66 { 67 showinfo(*human,enemy); // show fighter's information 68 cout<<"请御主下达指令: "<<endl; 69 cout<<"1 攻击; 2 宝具; 3 使用元素瓶; 4 使用元素灰瓶; 0 逃跑(退出游戏)"<<endl; 70 cin>>tempCom; 71 switch(tempCom) 72 { 73 case 0: 74 cout<<"你,抛弃你的荣耀 Y/N"<<endl; 75 char temp; 76 cin>>temp; 77 if(temp=='Y'||temp=='y') 78 return 0; 79 else 80 break; 81 case 1: 82 success=human->attack(enemy); 83 human->isLevelUp(); 84 enemy.isDead(); 85 break; 86 case 2: 87 success=human->specialatt(enemy); 88 human->isLevelUp(); 89 enemy.isDead(); 90 break; 91 case 3: 92 success=human->useHeal(); 93 break; 94 case 4: 95 success=human->useMW(); 96 break; 97 default: 98 break; 99 } 100 } 101 if(!enemy.death()) // If AI still alive 102 enemy.AI(*human); 103 else // AI died 104 { 105 cout<<"YOU WIN"<<endl; 106 human->transfer(enemy); // player got all AI's items 107 } 108 if (human->death()) 109 { 110 system("cls"); 111 cout<<endl<<setw(50)<<"GAME OVER"<<endl; 112 delete human; // player is dead, program is getting to its end, what should we do here? 113 system("pause"); 114 return 0; 115 } 116 } 117 } 118 delete human; // You win, program is getting to its end, what should we do here? 119 system("cls"); 120 cout<<"Congratulations! 你成为了大圣杯的拥有者!!"<<endl; 121 system("pause"); 122 return 0; 123 }
container.h
1 //======================= 2 // container.h 3 //======================= 4 5 // The so-called inventory of a player in RPG games 6 // contains two items, heal and magic water 7 8 #ifndef _CONTAINER // Conditional compilation 9 #define _CONTAINER 10 11 class container // Inventory 12 { 13 protected: 14 int numOfHeal; // number of heal 15 int numOfMW; // number of magic water 16 public: 17 container(); // constuctor 18 void set(int heal_n, int mw_n); // set the items numbers 19 int nOfHeal(); // get the number of heal 20 int nOfMW(); // get the number of magic water 21 void display(); // display the items; 22 bool useHeal(); // use heal 23 bool useMW(); // use magic water 24 }; 25 26 #endif
container.cpp
1 //======================= 2 // container.cpp 3 //======================= 4 #include "container.h" 5 #include <iostream> 6 using namespace std; 7 // default constructor initialise the inventory as empty 8 container::container() 9 { 10 set(0,0); 11 } 12 13 // set the item numbers 14 void container::set(int heal_n, int mw_n) 15 { 16 numOfHeal=heal_n; 17 numOfMW=mw_n; 18 } 19 20 // get the number of heal 21 int container::nOfHeal() 22 { 23 return numOfHeal; 24 } 25 26 // get the number of magic water 27 int container::nOfMW() 28 { 29 return numOfMW; 30 } 31 32 // display the items; 33 void container::display() 34 { 35 cout<<"你包裹中的物品: "<<endl; 36 cout<<"元素瓶(HP+100): "<<numOfHeal<<endl; 37 cout<<"元素灰瓶(MP+80): "<<numOfMW<<endl; 38 } 39 40 //use heal 41 bool container::useHeal() 42 { 43 numOfHeal--; 44 return 1; // use heal successfully 45 } 46 47 //use magic water 48 bool container::useMW() 49 { 50 numOfMW--; 51 return 1; // use magic water successfully 52 }
player.h
1 //======================= 2 // player.h 3 //======================= 4 5 // The base class of player 6 // including the general properties and methods related to a character 7 8 #ifndef _PLAYER 9 #define _PLAYER 10 11 #include <iomanip> // use for setting field width 12 #include <time.h> // use for generating random factor 13 #include "container.h" 14 #include <string> 15 #include <windows.h> 16 using namespace std; 17 18 enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 19 sword man, archer, mage */ 20 class player 21 { 22 friend void showinfo(player &p1, player &p2); 23 friend class swordsman; 24 friend class archer; 25 friend class mege; 26 27 protected: 28 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 29 // General properties of all characters 30 string name; // character name 31 job role; /* character's job, one of swordman, archer and mage, 32 as defined by the enumerate type */ 33 container bag; // character's inventory 34 35 public: 36 virtual bool attack(player &p)=0; // normal attack 37 virtual bool specialatt(player &p)=0; //special attack 38 virtual void isLevelUp()=0; // level up judgement 39 /* Attention! 40 These three methods are called "Pure virtual functions". 41 They have only declaration, but no definition. 42 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 43 The detailed definition of these pure virtual functions will be given in subclasses. */ 44 45 void reFill(); // character's HP and MP resume 46 bool death(); // report whether character is dead 47 void isDead(); // check whether character is dead 48 bool useHeal(); // consume heal, irrelevant to job 49 bool useMW(); // consume magic water, irrelevant to job 50 void transfer(player &p); // possess opponent's items after victory 51 void showRole(); // display character's job 52 53 private: 54 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 55 }; 56 57 #endif
player.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 #include<iostream> 5 #include"player.h" 6 using namespace std; 7 // character's HP and MP resume 8 void player::reFill() 9 { 10 HP=HPmax; // HP and MP fully recovered 11 MP=MPmax; 12 } 13 14 // report whether character is dead 15 bool player::death() 16 { 17 return playerdeath; 18 } 19 20 // check whether character is dead 21 void player::isDead() 22 { 23 if(HP<=0) // HP less than 0, character is dead 24 { 25 cout<<name<<" 阵亡." <<endl; 26 system("pause"); 27 playerdeath=1; // give the label of death value 1 28 } 29 } 30 31 // consume heal, irrelevant to job 32 bool player::useHeal() 33 { 34 if(bag.nOfHeal()>0) 35 { 36 HP=HP+100; 37 if(HP>HPmax) // HP cannot be larger than maximum value 38 HP=HPmax; // so assign it to HPmax, if necessary 39 cout<<name<<" 喝下元素瓶, HP 回复了 100."<<endl; 40 bag.useHeal(); // use heal 41 system("pause"); 42 return 1; // usage of heal succeed 43 } 44 else // If no more heal in bag, cannot use 45 { 46 cout<<"元素瓶数量不足"<<endl; 47 system("pause"); 48 return 0; // usage of heal failed 49 } 50 } 51 52 // consume magic water, irrelevant to job 53 bool player::useMW() 54 { 55 if(bag.nOfMW()>0) 56 { 57 MP=MP+100; 58 if(MP>MPmax) 59 MP=MPmax; 60 cout<<name<<" 喝下元素灰瓶, MP 回复了 100."<<endl; 61 bag.useMW(); 62 system("pause"); 63 return 1; // usage of magic water succeed 64 } 65 else 66 { 67 cout<<"元素灰瓶数量不足"<<endl; 68 system("pause"); 69 return 0; // usage of magic water failed 70 } 71 } 72 73 // possess opponent's items after victory 74 void player::transfer(player &p) 75 { 76 cout<<name<<" 获得"<<p.bag.nOfHeal()<<" 个元素瓶, 和 "<<p.bag.nOfMW()<<" 个元素灰瓶."<<endl; 77 system("pause"); 78 bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW()); 79 // set the character's bag, get opponent's items 80 } 81 82 // display character's job 83 void player::showRole() 84 { 85 switch(role) 86 { 87 case sw: 88 cout<<"剑士"; 89 break; 90 case ar: 91 cout<<"弓兵"; 92 break; 93 case mg: 94 cout<<"法师"; 95 break; 96 default: 97 break; 98 } 99 } 100 101 102 // display character's job 103 void showinfo(player &p1, player &p2) 104 { 105 system("cls"); 106 cout<<"##############################################################"<<endl; 107 cout<<"# 从者"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 108 <<" # 敌人"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 109 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 110 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 111 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 112 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 113 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 114 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 115 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 116 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 117 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 118 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 119 cout<<"# EXP"<<setw(7)<<p1.EXP<<" 职阶: "<<setw(7); 120 p1.showRole(); 121 cout<<" # EXP"<<setw(7)<<p2.EXP<<" 职阶: "<<setw(7); 122 p2.showRole(); 123 cout<<" #"<<endl; 124 cout<<"--------------------------------------------------------------"<<endl; 125 p1.bag.display(); 126 cout<<"##############################################################"<<endl; 127 }
swordsman.h
1 //======================= 2 // swordsman.h 3 //======================= 4 5 // Derived from base class player 6 // For the job Swordsman 7 8 #include "player.h" 9 class swordsman : public player // subclass swordsman publicly inherited from base player 10 { 11 public: 12 swordsman(int lv_in=1, string name_in="Not Given"); 13 // constructor with default level of 1 and name of "Not given" 14 void isLevelUp(); 15 bool attack (player &p); 16 bool specialatt(player &p); 17 /* These three are derived from the pure virtual functions of base class 18 The definition of them will be given in this subclass. */ 19 void AI(player &p); // Computer opponent 20 };
swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 #include<iostream> 5 #include"swordsman.h" 6 using namespace std; 7 // constructor. default values don't need to be repeated here 8 swordsman::swordsman(int lv_in, string name_in) 9 { 10 role=sw; // enumerate type of job 11 LV=lv_in; 12 name=name_in; 13 14 // Initialising the character's properties, based on his level 15 HPmax=150+8*(LV-1); // HP increases 8 point2 per level 16 HP=HPmax; 17 MPmax=75+2*(LV-1); // MP increases 2 points per level 18 MP=MPmax; 19 AP=25+4*(LV-1); // AP increases 4 points per level 20 DP=25+4*(LV-1); // DP increases 4 points per level 21 speed=25+2*(LV-1); // speed increases 2 points per level 22 23 playerdeath=0; 24 EXP=LV*LV*75; 25 bag.set(lv_in, lv_in); 26 } 27 28 void swordsman::isLevelUp() 29 { 30 if(EXP>=LV*LV*75) 31 { 32 LV++; 33 AP+=4; 34 DP+=4; 35 HPmax+=8; 36 MPmax+=2; 37 speed+=2; 38 cout<<name<<" 等级提升!"<<endl; 39 cout<<"HP 提升了 8 点 达到了 "<<HPmax<<endl; 40 cout<<"MP 提升了 2 点 达到了 "<<MPmax<<endl; 41 cout<<"Speed 提升了 2 点 达到了 "<<speed<<endl; 42 cout<<"AP 提升了 4 点 达到了 "<<AP<<endl; 43 cout<<"DP 提升了 5 点 达到了 "<<DP<<endl; 44 system("pause"); 45 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 46 } 47 } 48 49 bool swordsman::attack(player &p) 50 { 51 double HPtemp=0; // opponent's HP decrement 52 double EXPtemp=0; // player obtained exp 53 double hit=1; // attach factor, probably give critical attack 54 srand((unsigned)time(NULL)); // generating random seed based on system time 55 56 // If speed greater than opponent, you have some possibility to do double attack 57 if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 58 { 59 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+25)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 60 cout<<name<<"的攻击命中了 "<<p.name<<", "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl; 61 p.HP=int(p.HP-HPtemp); 62 EXPtemp=(int)(HPtemp*1.2); 63 } 64 65 // If speed smaller than opponent, the opponent has possibility to evade 66 if ((speed<p.speed) && (rand()%50<1)) 67 { 68 cout<<name<<"的攻击被 "<<p.name<<" 闪避了"<<endl; 69 system("pause"); 70 return 1; 71 } 72 73 // 10% chance give critical attack 74 if (rand()%100<=10) 75 { 76 hit=1.5; 77 cout<<"命中要害: "; 78 } 79 80 // Normal attack 81 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); 82 cout<<name<<" 使用 直刺, "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl; 83 EXPtemp=(int)(EXPtemp+HPtemp*1.2); 84 p.HP=(int)(p.HP-HPtemp); 85 cout<<name<<" 获得 "<<EXPtemp<<" 点经验值."<<endl; 86 EXP=(int)(EXP+EXPtemp); 87 system("pause"); 88 return 1; // Attack success 89 } 90 91 bool swordsman::specialatt(player &p) 92 { 93 if(MP<35) 94 { 95 cout<<"你没有足够的魔力发动宝具!"<<endl; 96 system("pause"); 97 return 0; // Attack failed 98 } 99 else 100 { 101 MP-=35; // consume 35 MP to do special attack 102 103 //10% chance opponent evades 104 if(rand()%100<=10) 105 { 106 cout<<name<<"的宝具被 "<<p.name<<" 规避"<<endl; 107 system("pause"); 108 return 1; 109 } 110 111 double HPtemp=0; 112 double EXPtemp=0; 113 //double hit=1; 114 //srand(time(NULL)); 115 HPtemp=(int)(AP*1.3+20); // not related to opponent's DP 116 EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience 117 cout<<name<<" 发动了————修罗邪光斩, "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl; 118 cout<<name<<" 获得 "<<EXPtemp<<" 点经验值."<<endl; 119 p.HP=(int)(p.HP-HPtemp); 120 EXP=(int)(EXP+EXPtemp); 121 system("pause"); 122 } 123 return 1; // special attack succeed 124 } 125 126 // Computer opponent 127 void swordsman::AI(player &p) 128 { 129 if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+100<=1.1*HPmax)&&(bag.nOfHeal()>0)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5))) 130 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 131 { 132 useHeal(); 133 } 134 else 135 { 136 if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) 137 // AI has enough MP, it has 30% to make special attack 138 { 139 specialatt(p); 140 p.isDead(); // check whether player is dead 141 } 142 else 143 { 144 if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) 145 // Not enough MP && HP is safe && still has magic water 146 { 147 useMW(); 148 } 149 else 150 { 151 attack(p); // normal attack 152 p.isDead(); 153 } 154 } 155 } 156 }
archer.h
1 #include "player.h" 2 class archer :public player 3 { 4 public: 5 archer(int lv_in = 1, string name_in = "Not Given"); 6 // constructor with default level of 1 and name of "Not given" 7 void isLevelUp(); 8 bool attack(player &p); 9 bool specialatt(player &p); 10 /* These three are derived from the pure virtual functions of base class 11 The definition of them will be given in this subclass. */ 12 void AI(player &p); // Computer opponent 13 };
archer.cpp
1 //======================= 2 // archer.cpp 3 //======================= 4 #include<iostream> 5 #include"archer.h" 6 using namespace std; 7 // constructor. default values don't need to be repeated here 8 archer::archer(int lv_in, string name_in) 9 { 10 role = sw; // enumerate type of job 11 LV = lv_in; 12 name = name_in; 13 14 // Initialising the character's properties, based on his level 15 HPmax = 135 + 5 * (LV - 1); // HP increases 5 point2 per level 16 HP = HPmax; 17 MPmax = 85 + 5 * (LV - 1); // MP increases 5 points per level 18 MP = MPmax; 19 AP = 25 + 4 * (LV - 1); // AP increases 4 points per level 20 DP = 25 + 4 * (LV - 1); // DP increases 4 points per level 21 speed = 40 + 8 * (LV - 1); // speed increases 8 points per level 22 23 playerdeath = 0; 24 EXP = LV*LV * 75; 25 bag.set(lv_in, lv_in); 26 } 27 28 void archer::isLevelUp() 29 { 30 if (EXP >= LV*LV * 75) 31 { 32 LV++; 33 AP += 4; 34 DP += 4; 35 HPmax += 5; 36 MPmax += 5; 37 speed += 8; 38 cout << name << " 等级提升!" << endl; 39 cout << "HP 提升了 5 点 达到了 " << HPmax << endl; 40 cout << "MP 提升了 5 点 达到了 " << MPmax << endl; 41 cout << "Speed 提升了 8 点 达到了 " << speed << endl; 42 cout << "AP 提升了 4 点 达到了 " << AP << endl; 43 cout << "DP 提升了 4 点 达到了 " << DP << endl; 44 system("pause"); 45 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 46 } 47 } 48 49 bool archer::attack(player &p) 50 { 51 double HPtemp = 0; // opponent's HP decrement 52 double EXPtemp = 0; // player obtained exp 53 double hit = 1; // attach factor, probably give critical attack 54 srand((unsigned)time(NULL)); // generating random seed based on system time 55 56 // If speed greater than opponent, you have some possibility to do double attack 57 if ((speed>p.speed) && (rand() % 100<(speed - p.speed))) // rand()%100 means generates a number no greater than 100 58 { 59 HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 15)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 60 cout << name << "的攻击命中了 " << p.name << ", " << p.name << "的 HP 减少了 " << HPtemp << endl; 61 p.HP = int(p.HP - HPtemp); 62 EXPtemp = (int)(HPtemp*1.2); 63 } 64 65 // If speed smaller than opponent, the opponent has possibility to evade 66 if ((speed<p.speed) && (rand() % 50<1)) 67 { 68 cout << name << "的攻击被 " << p.name << " 闪避了" << endl; 69 system("pause"); 70 return 1; 71 } 72 73 // 10% chance give critical attack 74 if (rand() % 100 <= 10) 75 { 76 hit = 1.5; 77 cout << "命中要害: "; 78 } 79 80 // Normal attack 81 HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); 82 cout << name << " 使用 三连射, " << p.name << "的 HP 减少了 " << HPtemp << endl; 83 EXPtemp = (int)(EXPtemp + HPtemp*1.2); 84 p.HP = (int)(p.HP - HPtemp); 85 cout << name << " 获得 " << EXPtemp << " 点经验值." << endl; 86 EXP = (int)(EXP + EXPtemp); 87 system("pause"); 88 return 1; // Attack success 89 } 90 91 bool archer::specialatt(player &p) 92 { 93 if (MP<40) 94 { 95 cout << "你没有足够的魔力发动宝具!" << endl; 96 system("pause"); 97 return 0; // Attack failed 98 } 99 else 100 { 101 MP -= 40; // consume 40 MP to do special attack 102 103 //10% chance opponent evades 104 if (rand() % 100 <= 10) 105 { 106 cout << name << "的宝具被 " << p.name << " 规避" << endl; 107 system("pause"); 108 return 1; 109 } 110 111 double HPtemp = 0; 112 double EXPtemp = 0; 113 //double hit=1; 114 //srand(time(NULL)); 115 HPtemp = (int)(AP*1.2 + 30); // not related to opponent's DP 116 EXPtemp = (int)(HPtemp*1.5); // special attack provides more experience 117 cout << name << " 发动了————流星一条(笑), " << p.name << "的 HP 减少了 " << HPtemp << endl; 118 cout << name << " 获得 " << EXPtemp << " 点经验值." << endl; 119 p.HP = (int)(p.HP - HPtemp); 120 EXP = (int)(EXP + EXPtemp); 121 system("pause"); 122 } 123 return 1; // special attack succeed 124 } 125 126 // Computer opponent 127 void archer::AI(player &p) 128 { 129 if ((HP<(int)((1.0*p.AP / DP)*p.AP*1.5)) && (HP + 100 <= 1.1*HPmax) && (bag.nOfHeal()>0) && (HP>(int)((1.0*p.AP / DP)*p.AP*0.5))) 130 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 131 { 132 useHeal(); 133 } 134 else 135 { 136 if (MP >= 40 && HP>0.5*HPmax && rand() % 100 <= 30) 137 // AI has enough MP, it has 30% to make special attack 138 { 139 specialatt(p); 140 p.isDead(); // check whether player is dead 141 } 142 else 143 { 144 if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) 145 // Not enough MP && HP is safe && still has magic water 146 { 147 useMW(); 148 } 149 else 150 { 151 attack(p); // normal attack 152 p.isDead(); 153 } 154 } 155 } 156 }
mege.h
1 #include "player.h" 2 class mege : public player 3 { 4 public: 5 mege(int lv_in = 1, string name_in = "Not Given"); 6 // constructor with default level of 1 and name of "Not given" 7 void isLevelUp(); 8 bool attack(player &p); 9 bool specialatt(player &p); 10 /* These three are derived from the pure virtual functions of base class 11 The definition of them will be given in this subclass. */ 12 void AI(player &p); // Computer opponent 13 };
mege.cpp
1 //======================= 2 // mege.cpp 3 //======================= 4 #include<iostream> 5 #include"mege.h" 6 using namespace std; 7 // constructor. default values don't need to be repeated here 8 mege::mege(int lv_in, string name_in) 9 { 10 role = sw; // enumerate type of job 11 LV = lv_in; 12 name = name_in; 13 14 // Initialising the character's properties, based on his level 15 HPmax = 100 + 2 * (LV - 1); // HP increases 2 point2 per level 16 HP = HPmax; 17 MPmax = 150 + 8 * (LV - 1); // MP increases 8 points per level 18 MP = MPmax; 19 AP = 25 + 5 * (LV - 1); // AP increases 2 points per level 20 DP = 25 + 3 * (LV - 1); // DP increases 6 points per level 21 speed = 30 + 3 * (LV - 1); // speed increases 3 points per level 22 23 playerdeath = 0; 24 EXP = LV*LV * 75; 25 bag.set(lv_in, lv_in); 26 } 27 28 void mege::isLevelUp() 29 { 30 if (EXP >= LV*LV * 75) 31 { 32 LV++; 33 AP += 5; 34 DP += 3; 35 HPmax += 2; 36 MPmax += 8; 37 speed += 3; 38 cout << name << " 等级提升!" << endl; 39 cout << "HP 提升了 2 点 达到了 " << HPmax << endl; 40 cout << "MP 提升了 8 点 达到了 " << MPmax << endl; 41 cout << "Speed 提升了 3 点 达到了 " << speed << endl; 42 cout << "AP 提升了 5 点 达到了 " << AP << endl; 43 cout << "DP 提升了 3 点 达到了 " << DP << endl; 44 system("pause"); 45 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 46 } 47 } 48 49 bool mege::attack(player &p) 50 { 51 double HPtemp = 0; // opponent's HP decrement 52 double EXPtemp = 0; // player obtained exp 53 double hit = 1; // attach factor, probably give critical attack 54 srand((unsigned)time(NULL)); // generating random seed based on system time 55 56 // If speed greater than opponent, you have some possibility to do double attack 57 if ((speed>p.speed) && (rand() % 100<(speed - p.speed))) // rand()%100 means generates a number no greater than 100 58 { 59 HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 60 cout << name << "的攻击命中了 " << p.name << ", " << p.name << "的 HP 减少了 " << HPtemp << endl; 61 p.HP = int(p.HP - HPtemp); 62 EXPtemp = (int)(HPtemp*1.2); 63 } 64 65 // If speed smaller than opponent, the opponent has possibility to evade 66 if ((speed<p.speed) && (rand() % 50<1)) 67 { 68 cout << name << "的攻击被 " << p.name << " 闪避了" << endl; 69 system("pause"); 70 return 1; 71 } 72 73 // 10% chance give critical attack 74 if (rand() % 100 <= 10) 75 { 76 hit = 1.5; 77 cout << "命中要害: "; 78 } 79 80 // Normal attack 81 HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); 82 cout << name << " 使用 封魔针, " << p.name << "的 HP 减少了 " << HPtemp << endl; 83 EXPtemp = (int)(EXPtemp + HPtemp*1.2); 84 p.HP = (int)(p.HP - HPtemp); 85 cout << name << " 获得 " << EXPtemp << " 点经验值." << endl; 86 EXP = (int)(EXP + EXPtemp); 87 system("pause"); 88 return 1; // Attack success 89 } 90 91 bool mege::specialatt(player &p) 92 { 93 if (MP<60) 94 { 95 cout << "你没有足够的魔力发动宝具!" << endl; 96 system("pause"); 97 return 0; // Attack failed 98 } 99 else 100 { 101 MP -= 60; // consume 60 MP to do special attack 102 103 //10% chance opponent evades 104 if (rand() % 100 <= 10) 105 { 106 cout << name << "的宝具被 " << p.name << " 规避" << endl; 107 system("pause"); 108 return 1; 109 } 110 111 double HPtemp = 0; 112 double EXPtemp = 0; 113 //double hit=1; 114 //srand(time(NULL)); 115 HPtemp = (int)(AP*2.0 + 20); // not related to opponent's DP 116 EXPtemp = (int)(HPtemp*1.5); // special attack provides more experience 117 cout << name << " 发动了————梦想封印, " << p.name << "的 HP 减少了 " << HPtemp << endl; 118 cout << name << " 获得 " << EXPtemp << " 点经验值." << endl; 119 p.HP = (int)(p.HP - HPtemp); 120 EXP = (int)(EXP + EXPtemp); 121 system("pause"); 122 } 123 return 1; // special attack succeed 124 } 125 126 // Computer opponent 127 void mege::AI(player &p) 128 { 129 if ((HP<(int)((1.0*p.AP / DP)*p.AP*1.5)) && (HP + 100 <= 1.1*HPmax) && (bag.nOfHeal()>0) && (HP>(int)((1.0*p.AP / DP)*p.AP*0.5))) 130 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 131 { 132 useHeal(); 133 } 134 else 135 { 136 if (MP >= 60 && HP>0.5*HPmax && rand() % 100 <= 30) 137 // AI has enough MP, it has 30% to make special attack 138 { 139 specialatt(p); 140 p.isDead(); // check whether player is dead 141 } 142 else 143 { 144 if (MP<60 && HP>0.5*HPmax && bag.nOfMW()) 145 // Not enough MP && HP is safe && still has magic water 146 { 147 useMW(); 148 } 149 else 150 { 151 attack(p); // normal attack 152 p.isDead(); 153 } 154 } 155 } 156 }