From e83e9bb1b577212af409a72f1edd4b489ae79afb Mon Sep 17 00:00:00 2001 From: IoTcat Date: Sun, 7 Apr 2019 00:49:55 +0800 Subject: [PATCH] coommit --- as2/Fraction.h | 22 +++++- as2/ex1.cpp | 4 +- as2/ex2/container.cpp | 50 ++++++++++++++ as2/ex2/container.h | 26 +++++++ as2/ex2/main.cpp | 113 ++++++++++++++++++++++++++++++ as2/ex2/player.cpp | 127 ++++++++++++++++++++++++++++++++++ as2/ex2/player.h | 52 ++++++++++++++ as2/ex2/swordsman.cpp | 155 ++++++++++++++++++++++++++++++++++++++++++ as2/ex2/swordsman.h | 20 ++++++ 9 files changed, 566 insertions(+), 3 deletions(-) create mode 100644 as2/ex2/container.cpp create mode 100644 as2/ex2/container.h create mode 100644 as2/ex2/main.cpp create mode 100644 as2/ex2/player.cpp create mode 100644 as2/ex2/player.h create mode 100644 as2/ex2/swordsman.cpp create mode 100644 as2/ex2/swordsman.h diff --git a/as2/Fraction.h b/as2/Fraction.h index a2feb4a..e95eefd 100644 --- a/as2/Fraction.h +++ b/as2/Fraction.h @@ -619,7 +619,7 @@ void Fraction::repeated_decimal_to_fraction(double d) /** * 带分数运算 * - * @author yimian LIU + * @author Yimian LIU * @category eee102-as2 * @package Fraction.h */ @@ -707,6 +707,26 @@ class iFraction: public Fraction { }; +/** + * 分数类型转换 + * 多态1 - 假分数转带分数 + * + * @param Fraction f 待转假分数 + * @return iFraction 带分数 + */ +inline iFraction convertF(Fraction f){ + return (iFraction)f; +} +/** + * 分数类型转换 + * 多态2 - 带分数转假分数 + * + * @param iFraction f 待转带分数 + * @return Fraction 假分数 + */ +inline Fraction convertF(iFraction f){ + return (Fraction)f; +} #endif \ No newline at end of file diff --git a/as2/ex1.cpp b/as2/ex1.cpp index bf927aa..9ad4fb2 100644 --- a/as2/ex1.cpp +++ b/as2/ex1.cpp @@ -19,12 +19,12 @@ int main() p += q + p; - q = p; + //q = p; iFraction o; - cout << q; + cout << convertF(convertF(q)); /* diff --git a/as2/ex2/container.cpp b/as2/ex2/container.cpp new file mode 100644 index 0000000..620712e --- /dev/null +++ b/as2/ex2/container.cpp @@ -0,0 +1,50 @@ +//======================= +// container.cpp +//======================= + +// default constructor initialise the inventory as empty +container::container() +{ + set(0,0); +} + +// 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<<"Your bag contains: "< +#include +using namespace std; + +#include "swordsman.h" + + +int main() +{ + string tempName; + bool success=0; //flag for storing whether operation is successful + 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 + { + cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<>tempJob; + system("cls"); // clear the screen + switch(tempJob) + { + case 1: + 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 + } + }while(success!=1); // so the loop will ask user to re-create a character + + int tempCom; // temp command inputted by user + int nOpp=0; // the Nth opponent + for(int i=1;nOpp<5;i+=2) // i is opponent's level + { + nOpp++; + system("cls"); + cout<<"STAGE" <reFill(); // get HP/MP refill before start fight + + while(!human->death() && !enemy.death()) // no died + { + success=0; + while (success!=1) + { + showinfo(*human,enemy); // show fighter's information + cout<<"Please give command: "<>tempCom; + switch(tempCom) + { + case 0: + cout<<"Are you sure to exit? Y/N"<>temp; + if(temp=='Y'||temp=='y') + return 0; + else + break; + case 1: + success=human->attack(enemy); + human->isLevelUp(); + enemy.isDead(); + break; + case 2: + success=human->specialatt(enemy); + human->isLevelUp(); + enemy.isDead(); + break; + case 3: + success=human->useHeal(); + break; + case 4: + success=human->useMW(); + break; + default: + break; + } + } + if(!enemy.death()) // If AI still alive + enemy.AI(*human); + else // AI died + { + cout<<"YOU WIN"<transfer(enemy); // player got all AI's items + } + if (human->death()) + { + system("cls"); + cout<0) + { + HP=HP+100; + if(HP>HPmax) // HP cannot be larger than maximum value + HP=HPmax; // so assign it to HPmax, if necessary + cout<0) + { + MP=MP+100; + if(MP>MPmax) + MP=MPmax; + cout< // use for setting field width +#include // use for generating random factor +#include "container.h" + +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; + +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)=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. + 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 diff --git a/as2/ex2/swordsman.cpp b/as2/ex2/swordsman.cpp new file mode 100644 index 0000000..67a1fba --- /dev/null +++ b/as2/ex2/swordsman.cpp @@ -0,0 +1,155 @@ +//======================= +// swordsman.cpp +//======================= + +// 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=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 swordsman::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<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/swordsman.h b/as2/ex2/swordsman.h new file mode 100644 index 0000000..b7df1d1 --- /dev/null +++ b/as2/ex2/swordsman.h @@ -0,0 +1,20 @@ +//======================= +// swordsman.h +//======================= + +// Derived from base class player +// For the job Swordsman + +#include "player.h" +class swordsman : 5_????????? // subclass swordsman publicly inherited from base player +{ +public: + swordsman(int lv_in=1, 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 +};