diff --git a/as2/ex2/archer.cpp b/as2/ex2/archer.cpp new file mode 100644 index 0000000..3625cee --- /dev/null +++ b/as2/ex2/archer.cpp @@ -0,0 +1,165 @@ +//======================= +// archer.cpp +//======================= +#include "archer.h" +#include // use for setting field width +#include // use for generating random factor +#include +using namespace std; +// constructor. default values don't need to be repeated here +archer::archer(int lv_in, string name_in) +{ + role=ar; // enumerate type of job + LV=lv_in; + name=name_in; + + // Initialising the character's properties, based on his level + HPmax=150+8*(LV-1); // HP increases 8 point2 per level + HP=HPmax; + MPmax=75+2*(LV-1); // MP increases 2 points per level + MP=MPmax; + AP=25+4*(LV-1); // AP increases 4 points per level + DP=25+4*(LV-1); // DP increases 4 points per level + speed=25+2*(LV-1); // speed increases 2 points per level + + playerdeath=0; + EXP=LV*LV*75; + bag.set(lv_in, lv_in); +} + +void archer::isLevelUp() +{ + if(EXP>=LV*LV*75) + { + LV++; + AP+=4; + DP+=4; + HPmax+=8; + MPmax+=2; + speed+=2; + cout<p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 + { + HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance + cout< + cout< + system("pause"); + return 1; // Attack success +} + +bool archer::specialatt(player &p) +{ + if(MP<40) + { + cout<<"You don't have enough magic points!"< + EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience + cout<0)&&(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>=40 && HP>0.5*HPmax && rand()%100<=30) + // AI has enough MP, it has 30% to make special attack + { + specialatt(p); + p.isDead(); // check whether player is dead + } + else + { + if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) + // Not enough MP && HP is safe && still has magic water + { + useMW(); + } + else + { + attack(p); // normal attack + p.isDead(); + } + } + } +} + diff --git a/as2/ex2/archer.h b/as2/ex2/archer.h new file mode 100644 index 0000000..3eac379 --- /dev/null +++ b/as2/ex2/archer.h @@ -0,0 +1,21 @@ +//======================= +// archer.h +//======================= + +// Derived from base class player +// For the job archer +#include +#include "player.h" +class archer : public player // subclass archer publicly inherited from base player +{ +public: + archer(int lv_in=1, std::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 + +}; diff --git a/as2/ex2/archer.h.gch b/as2/ex2/archer.h.gch new file mode 100644 index 0000000..09907be Binary files /dev/null and b/as2/ex2/archer.h.gch differ diff --git a/as2/ex2/container.h.gch b/as2/ex2/container.h.gch index 0236601..eea46dd 100644 Binary files a/as2/ex2/container.h.gch and b/as2/ex2/container.h.gch differ diff --git a/as2/ex2/mage.cpp b/as2/ex2/mage.cpp new file mode 100644 index 0000000..2bda79a --- /dev/null +++ b/as2/ex2/mage.cpp @@ -0,0 +1,165 @@ +//======================= +// mage.cpp +//======================= +#include "mage.h" +#include // use for setting field width +#include // use for generating random factor +#include +using namespace std; +// constructor. default values don't need to be repeated here +mage::mage(int lv_in, string name_in) +{ + role=mg; // enumerate type of job + LV=lv_in; + name=name_in; + + // Initialising the character's properties, based on his level + HPmax=150+8*(LV-1); // HP increases 8 point2 per level + HP=HPmax; + MPmax=75+2*(LV-1); // MP increases 2 points per level + MP=MPmax; + AP=25+4*(LV-1); // AP increases 4 points per level + DP=25+4*(LV-1); // DP increases 4 points per level + speed=25+2*(LV-1); // speed increases 2 points per level + + playerdeath=0; + EXP=LV*LV*75; + bag.set(lv_in, lv_in); +} + +void mage::isLevelUp() +{ + if(EXP>=LV*LV*75) + { + LV++; + AP+=4; + DP+=4; + HPmax+=8; + MPmax+=2; + speed+=2; + cout<p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 + { + HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance + cout< + cout< + system("pause"); + return 1; // Attack success +} + +bool mage::specialatt(player &p) +{ + if(MP<40) + { + cout<<"You don't have enough magic points!"< + EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience + cout<0)&&(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>=40 && HP>0.5*HPmax && rand()%100<=30) + // AI has enough MP, it has 30% to make special attack + { + specialatt(p); + p.isDead(); // check whether player is dead + } + else + { + if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) + // Not enough MP && HP is safe && still has magic water + { + useMW(); + } + else + { + attack(p); // normal attack + p.isDead(); + } + } + } +} + diff --git a/as2/ex2/mage.h b/as2/ex2/mage.h new file mode 100644 index 0000000..92572db --- /dev/null +++ b/as2/ex2/mage.h @@ -0,0 +1,20 @@ +//======================= +// mage.h +//======================= + +// Derived from base class player +// For the job mage +#include +#include "player.h" +class mage : public player // subclass mage publicly inherited from base player +{ +public: + mage(int lv_in=1, std::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 +}; diff --git a/as2/ex2/mage.h.gch b/as2/ex2/mage.h.gch new file mode 100644 index 0000000..d4ef493 Binary files /dev/null and b/as2/ex2/mage.h.gch differ diff --git a/as2/ex2/main.cpp b/as2/ex2/main.cpp index 8471c8e..da70dfa 100644 --- a/as2/ex2/main.cpp +++ b/as2/ex2/main.cpp @@ -6,10 +6,13 @@ #include #include +#include +#include using namespace std; #include "swordsman.h" - +#include "archer.h" +#include "mage.h" int main() { @@ -18,6 +21,11 @@ int main() cout <<"Please input player's name: "; 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 { @@ -30,6 +38,16 @@ int main() human=new swordsman(1,tempName); // create the character with user inputted name and job success=1; // operation succeed break; +// default: break; // In this case, success=0, character creation failed } @@ -42,17 +60,27 @@ int main() nOpp++; system("cls"); cout<<"STAGE" < 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 + while(!human->death() && !enemy->death()) // no died { success=0; while (success!=1) { - showinfo(*human,enemy); // show fighter's information + showinfo(*human,*enemy); // show fighter's information cout<<"Please give command: "<>tempCom; @@ -67,14 +95,14 @@ int main() else break; case 1: - success=human->attack(enemy); + success=human->attack(*enemy); human->isLevelUp(); - enemy.isDead(); + enemy->isDead(); break; case 2: - success=human->specialatt(enemy); + success=human->specialatt(*enemy); human->isLevelUp(); - enemy.isDead(); + enemy->isDead(); break; case 3: success=human->useHeal(); @@ -86,12 +114,12 @@ int main() break; } } - if(!enemy.death()) // If AI still alive - enemy.AI(*human); + if(!enemy->death()) // If AI still alive + enemy->AI(*human); else // AI died { cout<<"YOU WIN"<transfer(enemy); // player got all AI's items + human->transfer(*enemy); // player got all AI's items } if (human->death()) { @@ -102,6 +130,9 @@ int main() return 0; } } +// } delete human;//7_????????? // You win, program is getting to its end, what should we do here? system("cls"); diff --git a/as2/ex2/player.cpp b/as2/ex2/player.cpp index 11f5437..33ea0a9 100644 --- a/as2/ex2/player.cpp +++ b/as2/ex2/player.cpp @@ -90,10 +90,10 @@ void player::showRole() cout<<"Swordsman"; break; case ar: - cout<<"Archer"; + cout<<" Archer "; break; case mg: - cout<<"Mage"; + cout<<" Mage "; break; default: break; diff --git a/as2/ex2/player.h b/as2/ex2/player.h index d1e7353..e7ab34a 100644 --- a/as2/ex2/player.h +++ b/as2/ex2/player.h @@ -20,6 +20,8 @@ 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; @@ -34,6 +36,9 @@ public: virtual bool attack(player &p)=0; // normal attack virtual bool specialatt(player &p)=0; //special attack virtual void isLevelUp()=0; // level up judgement +// /* Attention! These three methods are called "Pure virtual functions". They have only declaration, but no definition. diff --git a/as2/ex2/player.h.gch b/as2/ex2/player.h.gch index a08ba5b..5cb5eaf 100644 Binary files a/as2/ex2/player.h.gch and b/as2/ex2/player.h.gch differ diff --git a/as2/ex2/swordsman.cpp b/as2/ex2/swordsman.cpp index 44c266f..858f3a2 100644 --- a/as2/ex2/swordsman.cpp +++ b/as2/ex2/swordsman.cpp @@ -83,9 +83,13 @@ bool swordsman::attack(player &p) HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); cout< cout< system("pause"); return 1; // Attack success } @@ -119,7 +123,9 @@ bool swordsman::specialatt(player &p) cout< system("pause"); } return 1; // special attack succeed diff --git a/as2/ex2/swordsman.h.gch b/as2/ex2/swordsman.h.gch index 4504020..f15f698 100644 Binary files a/as2/ex2/swordsman.h.gch and b/as2/ex2/swordsman.h.gch differ