实验任务4
pets.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 using std::string; 7 class MachinePets { 8 public: 9 MachinePets(const string s):nickname{s}{} 10 string get_nickname() const { return nickname; } 11 virtual string talk() = 0{ } 12 private: 13 string nickname; 14 }; 15 16 class PetCats :public MachinePets { 17 public: 18 PetCats(const string s):MachinePets(s){ } 19 string talk() { return "miao wu~"; } 20 }; 21 22 class PetDogs :public MachinePets { 23 public: 24 PetDogs(const string s):MachinePets(s) { } 25 string talk() { return "wang wang~"; } 26 };
task4.cpp
1 #include <iostream> 2 #include "pets.hpp" 3 4 void play(MachinePets& obj) { 5 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 6 } 7 8 void test() { 9 PetCats cat("miku"); 10 PetDogs dog("da huang"); 11 12 play(cat); 13 play(dog); 14 } 15 16 int main() { 17 test(); 18 }
运行测试结果:
实验任务5
Person.hpp
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 using namespace std; 6 7 class Person { 8 public: 9 Person(string n="0", string t="0", string e="0") :name{n}, telephone{t}, email{e} {} 10 Person(const Person& p); 11 void update_telephone(); 12 void update_email(); 13 friend std::ostream& operator<<(std::ostream& out, const Person& p); 14 friend std::istream& operator>>(std::istream& in, Person& p); 15 friend bool operator==(const Person& p1, const Person& p2); 16 private: 17 string name; 18 string telephone; 19 string email; 20 }; 21 Person::Person(const Person& p) { 22 name = p.name; 23 telephone = p.telephone; 24 email = p.email; 25 } 26 std::ostream& operator<<(std::ostream& out, const Person& p) { 27 out << left <<setfill(' ') << setw(18) << p.name << setw(18)<< p.telephone << setw(18) << p.email; 28 return out; 29 } 30 std::istream& operator>>(std::istream &in,Person &p) { 31 getline(in, p.name); 32 getline(in, p.telephone); 33 getline(in, p.email); 34 //getchar(); 35 cout << "\n"; 36 //cin.clear(); 37 return in; 38 } 39 40 bool operator==(const Person& p1, const Person& p2) { 41 if (p1.name == p2.name && p1.telephone == p2.telephone) 42 return true; 43 else 44 return false; 45 } 46 void Person::update_telephone() { 47 cout << "Enter the telephone number: "; 48 cin.clear(); 49 cin >> telephone; 50 cout << "telephone number has been updated..." << endl; 51 } 52 void Person::update_email() { 53 cout << "Enter the email address: "; 54 cin.clear(); 55 cin >> email; 56 cout << "email address has been updated..." << endl; 57 }
task5.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include "Person.hpp" 5 6 void test() { 7 using namespace std; 8 9 vector<Person> phone_book; 10 Person p; 11 12 cout << "Enter person's contact until press Ctrl + Z" << endl; 13 while (cin >> p) 14 phone_book.push_back(p); 15 16 cout << "\nupdate someone's contact: \n"; 17 phone_book.at(0).update_telephone(); 18 phone_book.at(0).update_email(); 19 20 cout << "\ndisplay all contacts' info\n"; 21 for (auto& phone : phone_book) 22 cout << phone << endl; 23 24 cout << "\ntest whether the same contact\n"; 25 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 26 } 27 28 int main() { 29 test(); 30 }
运行测试结果:
实验任务6
container.h
1 #pragma once 2 //======================= 3 // container.h 4 //======================= 5 6 // The so-called inventory of a player in RPG games 7 // contains two items, heal and magic water 8 9 #ifndef _CONTAINER // Conditional compilation 10 #define _CONTAINER 11 12 class container // Inventory 13 { 14 protected: 15 int numOfHeal; // number of heal 16 int numOfMW; // number of magic water 17 public: 18 container(); // constuctor 19 void set(int heal_n, int mw_n); // set the items numbers 20 int nOfHeal(); // get the number of heal 21 int nOfMW(); // get the number of magic water 22 void display(); // display the items; 23 bool useHeal(); // use heal 24 bool useMW(); // use magic water 25 }; 26 27 #endif
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 #include<iostream> 11 #include <iomanip> // use for setting field width 12 #include <time.h> // use for generating random factor 13 #include "container.h" 14 using namespace std; 15 enum job { sw, ar, mg }; /* define 3 jobs by enumerate type 16 sword man, archer, mage */ 17 class player 18 { 19 friend void showinfo(player& p1, player& p2); 20 friend class swordsman; 21 22 protected: 23 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 24 // General properties of all characters 25 string name; // character name 26 job role; /* character's job, one of swordman, archer and mage, 27 as defined by the enumerate type */ 28 container bag; // character's inventory 29 30 public: 31 virtual bool attack(player& p) = 0; // normal attack 32 virtual bool specialatt(player& p) = 0; //special attack 33 virtual void isLevelUp() = 0; // level up judgement 34 /* Attention! 35 These three methods are called "Pure virtual functions". 36 They have only declaration, but no definition. 37 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 38 The detailed definition of these pure virtual functions will be given in subclasses. */ 39 40 void reFill(); // character's HP and MP resume 41 bool death(); // report whether character is dead 42 void isDead(); // check whether character is dead 43 bool useHeal(); // consume heal, irrelevant to job 44 bool useMW(); // consume magic water, irrelevant to job 45 void transfer(player& p); // possess opponent's items after victory 46 void showRole(); // display character's job 47 48 private: 49 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 50 }; 51 52 #endif
swordsman.h
1 #pragma once 2 //======================= 3 // swordsman.h 4 //======================= 5 6 // Derived from base class player 7 // For the job Swordsman 8 9 #include "player.h" 10 class swordsman : public player // subclass swordsman publicly inherited from base player 11 { 12 public: 13 swordsman(int lv_in = 1, string name_in = "Not Given"); 14 // constructor with default level of 1 and name of "Not given" 15 void isLevelUp(); 16 bool attack(player& p); 17 bool specialatt(player& p); 18 /* These three are derived from the pure virtual functions of base class 19 The definition of them will be given in this subclass. */ 20 void AI(player& p); // Computer opponent 21 };
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 << "Your bag contains: " << endl; 36 cout << "Heal(HP+100): " << numOfHeal << endl; 37 cout << "Magic Water (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.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 #include<iostream> 5 #include<iomanip> 6 #include"player.h" 7 using namespace std; 8 // character's HP and MP resume 9 void player::reFill() 10 { 11 HP = HPmax; // HP and MP fully recovered 12 MP = MPmax; 13 } 14 15 // report whether character is dead 16 bool player::death() 17 { 18 return playerdeath; 19 } 20 21 // check whether character is dead 22 void player::isDead() 23 { 24 if (HP <= 0) // HP less than 0, character is dead 25 { 26 cout << name << " is Dead." << endl; 27 system("pause"); 28 playerdeath = 1; // give the label of death value 1 29 } 30 } 31 32 // consume heal, irrelevant to job 33 bool player::useHeal() 34 { 35 if (bag.nOfHeal() > 0) 36 { 37 HP = HP + 100; 38 if (HP > HPmax) // HP cannot be larger than maximum value 39 HP = HPmax; // so assign it to HPmax, if necessary 40 cout << name << " used Heal, HP increased by 100." << endl; 41 bag.useHeal(); // use heal 42 system("pause"); 43 return 1; // usage of heal succeed 44 } 45 else // If no more heal in bag, cannot use 46 { 47 cout << "Sorry, you don't have heal to use." << endl; 48 system("pause"); 49 return 0; // usage of heal failed 50 } 51 } 52 53 // consume magic water, irrelevant to job 54 bool player::useMW() 55 { 56 if (bag.nOfMW() > 0) 57 { 58 MP = MP + 100; 59 if (MP > MPmax) 60 MP = MPmax; 61 cout << name << " used Magic Water, MP increased by 100." << endl; 62 bag.useMW(); 63 system("pause"); 64 return 1; // usage of magic water succeed 65 } 66 else 67 { 68 cout << "Sorry, you don't have magic water to use." << endl; 69 system("pause"); 70 return 0; // usage of magic water failed 71 } 72 } 73 74 // possess opponent's items after victory 75 void player::transfer(player& p) 76 { 77 cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl; 78 system("pause"); 79 HP += p.bag.nOfHeal(); 80 MP += p.bag.nOfMW(); 81 // set the character's bag, get opponent's items 82 } 83 84 // display character's job 85 void player::showRole() 86 { 87 switch (role) 88 { 89 case sw: 90 cout << "Swordsman"; 91 break; 92 case ar: 93 cout << "Archer"; 94 break; 95 case mg: 96 cout << "Mage"; 97 break; 98 default: 99 break; 100 } 101 } 102 103 104 // display character's job 105 void showinfo(player& p1, player& p2) 106 { 107 system("cls"); 108 cout << "##############################################################" << endl; 109 cout << "# Player" << setw(10) << p1.name << " LV. " << setw(3) << p1.LV 110 << " # Opponent" << setw(10) << p2.name << " LV. " << setw(3) << p2.LV << " #" << endl; 111 cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999) 112 << " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999) 113 << " # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999) 114 << " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << " #" << endl; 115 cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999) 116 << " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999) 117 << " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999) 118 << " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999) 119 << " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999) 120 << " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << " #" << endl; 121 cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7); 122 p1.showRole(); 123 cout << " # EXP" << setw(7) << p2.EXP << " Job: " << setw(7); 124 p2.showRole(); 125 cout << " #" << endl; 126 cout << "--------------------------------------------------------------" << endl; 127 p1.bag.display(); 128 cout << "##############################################################" << endl; 129 }
swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 #include"swordsman.h" 5 #include<iostream> 6 7 using namespace std; 8 // constructor. default values don't need to be repeated here 9 swordsman::swordsman(int lv_in, string name_in) 10 { 11 role = sw; // enumerate type of job 12 LV = lv_in; 13 name = name_in; 14 15 // Initialising the character's properties, based on his level 16 HPmax = 150 + 8 * (LV - 1); // HP increases 8 point2 per level 17 HP = HPmax; 18 MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level 19 MP = MPmax; 20 AP = 25 + 4 * (LV - 1); // AP increases 4 points per level 21 DP = 25 + 4 * (LV - 1); // DP increases 4 points per level 22 speed = 25 + 2 * (LV - 1); // speed increases 2 points per level 23 24 playerdeath = 0; 25 EXP = LV * LV * 75; 26 bag.set(lv_in, lv_in); 27 } 28 29 void swordsman::isLevelUp() 30 { 31 if (EXP >= LV * LV * 75) 32 { 33 LV++; 34 AP += 4; 35 DP += 4; 36 HPmax += 8; 37 MPmax += 2; 38 speed += 2; 39 cout << name << " Level UP!" << endl; 40 cout << "HP improved 8 points to " << HPmax << endl; 41 cout << "MP improved 2 points to " << MPmax << endl; 42 cout << "Speed improved 2 points to " << speed << endl; 43 cout << "AP improved 4 points to " << AP << endl; 44 cout << "DP improved 5 points to " << DP << endl; 45 system("pause"); 46 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 47 } 48 } 49 50 bool swordsman::attack(player& p) 51 { 52 double HPtemp = 0; // opponent's HP decrement 53 double EXPtemp = 0; // player obtained exp 54 double hit = 1; // attach factor, probably give critical attack 55 srand((unsigned)time(NULL)); // generating random seed based on system time 56 57 // If speed greater than opponent, you have some possibility to do double attack 58 if ((speed > p.speed) && (rand() % 100 < (speed - p.speed))) // rand()%100 means generates a number no greater than 100 59 { 60 HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 61 cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl; 62 p.HP = int(p.HP - HPtemp); 63 EXPtemp = (int)(HPtemp * 1.2); 64 } 65 66 // If speed smaller than opponent, the opponent has possibility to evade 67 if ((speed < p.speed) && (rand() % 50 < 1)) 68 { 69 cout << name << "'s attack has been evaded by " << p.name << endl; 70 system("pause"); 71 return 1; 72 } 73 74 // 10% chance give critical attack 75 if (rand() % 100 <= 10) 76 { 77 hit = 1.5; 78 cout << "Critical attack: "; 79 } 80 81 // Normal attack 82 HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); 83 cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl; 84 EXPtemp = (int)(EXPtemp + HPtemp * 1.2); 85 p.HP = (int)(p.HP - HPtemp); 86 cout << name << " obtained " << EXPtemp << " experience." << endl; 87 EXP = (int)(EXP + EXPtemp); 88 system("pause"); 89 return 1; // Attack success 90 } 91 92 bool swordsman::specialatt(player& p) 93 { 94 if (MP < 40) 95 { 96 cout << "You don't have enough magic points!" << endl; 97 system("pause"); 98 return 0; // Attack failed 99 } 100 else 101 { 102 MP -= 40; // consume 40 MP to do special attack 103 104 //10% chance opponent evades 105 if (rand() % 100 <= 10) 106 { 107 cout << name << "'s leap attack has been evaded by " << p.name << endl; 108 system("pause"); 109 return 1; 110 } 111 112 double HPtemp = 0; 113 double EXPtemp = 0; 114 //double hit=1; 115 //srand(time(NULL)); 116 HPtemp = (int)(AP * 1.2 + 20); // not related to opponent's DP 117 EXPtemp = (int)(HPtemp * 1.5); // special attack provides more experience 118 cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl; 119 cout << name << " obtained " << EXPtemp << " experience." << endl; 120 p.HP = (int)(p.HP - HPtemp); 121 EXP = (int)(EXP + EXPtemp); 122 system("pause"); 123 } 124 return 1; // special attack succeed 125 } 126 127 // Computer opponent 128 void swordsman::AI(player& p) 129 { 130 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))) 131 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 132 { 133 useHeal(); 134 } 135 else 136 { 137 if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30) 138 // AI has enough MP, it has 30% to make special attack 139 { 140 specialatt(p); 141 p.isDead(); // check whether player is dead 142 } 143 else 144 { 145 if (MP < 40 && HP>0.5 * HPmax && bag.nOfMW()) 146 // Not enough MP && HP is safe && still has magic water 147 { 148 useMW(); 149 } 150 else 151 { 152 attack(p); // normal attack 153 p.isDead(); 154 } 155 } 156 } 157 }
main.cpp
1 //======================= 2 // main.cpp 3 //======================= 4 5 // main function for the RPG style game 6 7 #include <iostream> 8 #include <string> 9 #include "swordsman.h" 10 #include"swordsman.cpp" 11 using namespace std; 12 int main() 13 { 14 string tempName = "0"; 15 bool success = 0; //flag for storing whether operation is successful 16 cout << "Please input player's name: "; 17 cin >> tempName; // get player's name from keyboard input 18 player* human;// use pointer of base class, convenience for polymorphism 19 human = new swordsman(1, tempName); 20 int tempJob; // temp choice for job selection 21 do 22 { 23 cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl; 24 cin >> tempJob; 25 system("cls"); // clear the screen 26 switch (tempJob) 27 { 28 case 1: 29 delete human; 30 human = new swordsman(1, tempName); // create the character with user inputted name and job 31 success = 1; // operation succeed 32 break; 33 default: 34 break; // In this case, success=0, character creation failed 35 } 36 } while (success != 1); // so the loop will ask user to re-create a character 37 38 int tempCom; // temp command inputted by user 39 int nOpp = 0; // the Nth opponent 40 for (int i = 1; nOpp < 5; i += 2) // i is opponent's level 41 { 42 nOpp++; 43 system("cls"); 44 cout << "STAGE" << nOpp << endl; 45 cout << "Your opponent, a Level " << i << " Swordsman." << endl; 46 system("pause"); 47 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 48 human->reFill(); // get HP/MP refill before start fight 49 50 while (!human->death() && !enemy.death()) // no died 51 { 52 success = 0; 53 while (success != 1) 54 { 55 showinfo(*human, enemy); // show fighter's information 56 cout << "Please give command: " << endl; 57 cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl; 58 cin >> tempCom; 59 switch (tempCom) 60 { 61 case 0: 62 cout << "Are you sure to exit? Y/N" << endl; 63 char temp; 64 cin >> temp; 65 if (temp == 'Y' || temp == 'y') 66 return 0; 67 else 68 break; 69 case 1: 70 success = human->attack(enemy); 71 human->isLevelUp(); 72 enemy.isDead(); 73 break; 74 case 2: 75 success = human->specialatt(enemy); 76 human->isLevelUp(); 77 enemy.isDead(); 78 break; 79 case 3: 80 success = human->useHeal(); 81 break; 82 case 4: 83 success = human->useMW(); 84 break; 85 default: 86 break; 87 } 88 } 89 if (!enemy.death()) // If AI still alive 90 enemy.AI(*human); 91 else // AI died 92 { 93 cout << "YOU WIN" << endl; 94 human->transfer(enemy); // player got all AI's items 95 } 96 if (human->death()) 97 { 98 system("cls"); 99 cout << endl << setw(50) << "GAME OVER" << endl; 100 delete human; // player is dead, program is getting to its end, what should we do here? 101 system("pause"); 102 return 0; 103 } 104 } 105 } 106 delete human; // You win, program is getting to its end, what should we do here? 107 system("cls"); 108 cout << "Congratulations! You defeated all opponents!!" << endl; 109 system("pause"); 110 return 0; 111 }
运行测试结果:
实验总结
对类继承和多态掌握不熟练,在任务6编译中还遇到了链接器工具错误的问题,还需要大量的练习。
标签:string,继承,void,多态,player,int,实验,include,HP From: https://www.cnblogs.com/zxy0324/p/16929713.html