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