From 66ea61878933c272015757f25f26a6953e39f43b Mon Sep 17 00:00:00 2001 From: IoTcat Date: Wed, 10 Apr 2019 18:59:56 +0800 Subject: [PATCH] find change --- as2/{ => ex1}/Fraction.h | 9 ++ as2/{ => ex1}/ex1.cpp | 6 +- as2/ex2/archer.cpp | 283 +++++++++++++++++++++------------------ as2/ex2/archer.h | 25 ++-- as2/ex2/container.cpp | 4 +- as2/ex2/container.h | 4 +- as2/ex2/mage.cpp | 283 +++++++++++++++++++++------------------ as2/ex2/mage.h | 24 ++-- as2/ex2/main.cpp | 72 +++++++--- as2/ex2/player.cpp | 13 +- as2/ex2/player.h | 14 +- as2/ex2/swordsman.cpp | 185 +++++++++++++------------ as2/ex2/swordsman.h | 6 +- 13 files changed, 526 insertions(+), 402 deletions(-) rename as2/{ => ex1}/Fraction.h (98%) rename as2/{ => ex1}/ex1.cpp (98%) diff --git a/as2/Fraction.h b/as2/ex1/Fraction.h similarity index 98% rename from as2/Fraction.h rename to as2/ex1/Fraction.h index e95eefd..d4df07b 100644 --- a/as2/Fraction.h +++ b/as2/ex1/Fraction.h @@ -693,6 +693,15 @@ class iFraction: public Fraction { return o; }; + /* 提取整数 */ + inline int integer() const{ + return _get_integer(); + }; + + /* 提取分子 */ + inline int itop() const{ + return (int)_get_iTop(); + }; private: /* 获取带分数整数部分 */ diff --git a/as2/ex1.cpp b/as2/ex1/ex1.cpp similarity index 98% rename from as2/ex1.cpp rename to as2/ex1/ex1.cpp index 9ad4fb2..c45d205 100644 --- a/as2/ex1.cpp +++ b/as2/ex1/ex1.cpp @@ -15,16 +15,16 @@ int main() Fraction q(-3.2); - iFraction p(q); + iFraction p(-3.2); - p += q + p; + //p += q + p; //q = p; iFraction o; - cout << convertF(convertF(q)); + cout << p.integer(); /* diff --git a/as2/ex2/archer.cpp b/as2/ex2/archer.cpp index 67a1fba..c1b860a 100644 --- a/as2/ex2/archer.cpp +++ b/as2/ex2/archer.cpp @@ -1,155 +1,174 @@ //======================= -// swordsman.cpp +// 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 -swordsman::swordsman(int lv_in, string name_in) +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=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); + 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*(rand()%2), lv_in*(rand()%2)); } -void swordsman::isLevelUp() +void archer::isLevelUp() { - if(EXP>=LV*LV*75) - { - LV++; - AP+=4; - DP+=4; - HPmax+=8; - MPmax+=2; - speed+=2; - cout<=LV*LV*75) + { + LV++; + AP+=4; + DP+=4; + HPmax+=8; + MPmax+=2; + speed+=2; + bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV); + cout<p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 + { + HPtemp=(int)((attack)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance + 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< - // If speed smaller than opponent, the opponent has possibility to evade - if ((speed + system("pause"); + return 1; // Attack success } -bool swordsman::specialatt(player &p) +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(); - } - } - } + 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))) + // 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 index b7df1d1..3eac379 100644 --- a/as2/ex2/archer.h +++ b/as2/ex2/archer.h @@ -1,20 +1,21 @@ //======================= -// swordsman.h +// archer.h //======================= // Derived from base class player -// For the job Swordsman - +// For the job archer +#include #include "player.h" -class swordsman : 5_????????? // subclass swordsman publicly inherited from base player +class archer : public player // subclass archer 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 + 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/container.cpp b/as2/ex2/container.cpp index 620712e..f9586f5 100644 --- a/as2/ex2/container.cpp +++ b/as2/ex2/container.cpp @@ -1,7 +1,9 @@ //======================= // container.cpp //======================= +#include "container.h" +using namespace std; // default constructor initialise the inventory as empty container::container() { @@ -38,7 +40,7 @@ void container::display() //use heal bool container::useHeal() { - 2_???????? + numOfHeal--; //2_??????????? return 1; // use heal successfully } diff --git a/as2/ex2/container.h b/as2/ex2/container.h index a98dc00..1c3d2fe 100644 --- a/as2/ex2/container.h +++ b/as2/ex2/container.h @@ -5,9 +5,11 @@ // The so-called inventory of a player in RPG games // contains two items, heal and magic water -1_????????????? // Conditional compilation +#ifndef _CONTAINER //1_??????????? // Conditional compilation #define _CONTAINER +#include + class container // Inventory { protected: diff --git a/as2/ex2/mage.cpp b/as2/ex2/mage.cpp index 67a1fba..637b30e 100644 --- a/as2/ex2/mage.cpp +++ b/as2/ex2/mage.cpp @@ -1,155 +1,174 @@ //======================= -// swordsman.cpp +// 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 -swordsman::swordsman(int lv_in, string name_in) +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=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); + 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*(rand()%2), lv_in*(rand()%2)); } -void swordsman::isLevelUp() +void mage::isLevelUp() { - if(EXP>=LV*LV*75) - { - LV++; - AP+=4; - DP+=4; - HPmax+=8; - MPmax+=2; - speed+=2; - cout<=LV*LV*75) + { + LV++; + AP+=4; + DP+=4; + HPmax+=8; + MPmax+=2; + speed+=2; + bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV); + cout<p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 + { + HPtemp=(int)((attack)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance + 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< - // If speed smaller than opponent, the opponent has possibility to evade - if ((speed + system("pause"); + return 1; // Attack success } -bool swordsman::specialatt(player &p) +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(); - } - } - } + 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))) + // 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 index b7df1d1..92572db 100644 --- a/as2/ex2/mage.h +++ b/as2/ex2/mage.h @@ -1,20 +1,20 @@ //======================= -// swordsman.h +// mage.h //======================= // Derived from base class player -// For the job Swordsman - +// For the job mage +#include #include "player.h" -class swordsman : 5_????????? // subclass swordsman publicly inherited from base player +class mage : public player // subclass mage 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 + 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/main.cpp b/as2/ex2/main.cpp index 0a60ef9..5376083 100644 --- a/as2/ex2/main.cpp +++ b/as2/ex2/main.cpp @@ -6,11 +6,17 @@ #include #include +//>>>>>>> +#include +#include +//<<<<<<<< using namespace std; #include "swordsman.h" - - +//>>>>>>>> +#include "archer.h" +#include "mage.h" +//<<<<<<<< int main() { string tempName; @@ -18,6 +24,10 @@ 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 + +//>>>>>>>>>> + player *enemy; // use pointer of base class, convenience for polymorphism +//<<<<<<<<<< int tempJob; // temp choice for job selection do { @@ -30,6 +40,16 @@ int main() human=new swordsman(1,tempName); // create the character with user inputted name and job success=1; // operation succeed break; +//>>>>>>>>>>> + case 2: + human=new archer(1,tempName); // create the character with user inputted name and job + success=1; // operation succeed + break; + case 3: + human=new mage(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 +62,30 @@ int main() nOpp++; system("cls"); cout<<"STAGE" <>>>>>>>>>>>> + srand((unsigned int)time(NULL)); + short t_s = rand() % 3; + cout<<"Your opponent, a Level "<reFill(); // get HP/MP refill before start fight +//>>>>>>>>>> + if(t_s == 0) enemy = new swordsman(i, "Warrior"); + if(t_s == 1) enemy = new archer(i, "Warrior"); + if(t_s == 2) enemy = new mage(i, "Warrior"); +//<<<<<<<<<<< - while(!human->death() && !enemy.death()) // no died + human->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 +//>>>>>>> + showinfo(*human,*enemy); // show fighter's information +//<<<<<<<<<< cout<<"Please give command: "<>tempCom; @@ -66,15 +99,17 @@ int main() return 0; 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,24 +121,29 @@ 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()) { system("cls"); cout<>>>>>>>>>>>> + delete enemy; +//<<<<<<<<<<<<<<< } - 7_????????? // You win, program is getting to its end, what should we do here? + delete human;//7_????????? // You win, program is getting to its end, what should we do here? system("cls"); cout<<"Congratulations! You defeated all opponents!!"<bag.set(this->bag.nOfHeal() + p.bag.nOfHeal(), this->bag.nOfMW() + p.bag.nOfMW());// 3_???????????? // set the character's bag, get opponent's items } @@ -87,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; @@ -99,7 +102,7 @@ void player::showRole() // display character's job -4_?????????????? +void showinfo(player &p1, player &p2) // 4_?????????????????? { system("cls"); cout<<"##############################################################"< #include // use for setting field width #include // use for generating random factor +#include #include "container.h" enum job {sw, ar, mg}; /* define 3 jobs by enumerate type @@ -17,12 +19,17 @@ enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 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 +protected: + std::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 @@ -31,6 +38,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. @@ -42,8 +52,8 @@ public: 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 + void transfer(player &p); // possess opponent's items after victory private: bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited diff --git a/as2/ex2/swordsman.cpp b/as2/ex2/swordsman.cpp index 67a1fba..d75536b 100644 --- a/as2/ex2/swordsman.cpp +++ b/as2/ex2/swordsman.cpp @@ -1,7 +1,11 @@ //======================= // swordsman.cpp //======================= - +#include "swordsman.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 swordsman::swordsman(int lv_in, string name_in) { @@ -25,100 +29,115 @@ swordsman::swordsman(int lv_in, string name_in) void swordsman::isLevelUp() { - if(EXP>=LV*LV*75) - { - LV++; - AP+=4; - DP+=4; - HPmax+=8; - MPmax+=2; - speed+=2; - cout<=LV*LV*75) + { + LV++; + AP+=4; + DP+=4; + HPmax+=8; + MPmax+=2; + speed+=2; + bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV); + 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<p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 + { + HPtemp=(int)((attack)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance + cout< + cout< - // Normal attack - HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); - cout< + system("pause"); + return 1; // Attack success } bool swordsman::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< #include "player.h" -class swordsman : 5_????????? // subclass swordsman publicly inherited from base player +class swordsman : public player // subclass swordsman publicly inherited from base player { public: - swordsman(int lv_in=1, string name_in="Not Given"); + swordsman(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);