Task 4:
- pets.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 7 //机器宠物(+猫、狗)定义 8 class MachinePets { 9 public: 10 MachinePets(const string s); 11 string get_nickname() const; 12 virtual string talk() = 0;//纯虚函数! 13 private: 14 string nickname; 15 }; 16 17 class PetCats :public MachinePets { 18 public: 19 PetCats(const string s); 20 string talk(); 21 }; 22 23 class PetDogs :public MachinePets { 24 public: 25 PetDogs(const string s); 26 string talk(); 27 }; 28 29 //实现 30 MachinePets::MachinePets(const string s) :nickname{ s } {} 31 string MachinePets::get_nickname() const { return nickname; } 32 PetCats::PetCats(const string s) :MachinePets(s) {} 33 string PetCats::talk() { return "Miao wu~"; } 34 PetDogs::PetDogs(const string s) :MachinePets(s) {} 35 string PetDogs::talk() { return "Wang wang~"; } 36 37 //多态:虚函数,当指针指向基类对象时,就做基类的事,使用基类的成员,当指针指向派生类对象时,就使用派生类的成员。 38 //继承:虚基类,钻石结构。 39 // 虚函数、纯虚函数( = 0) 40 // 派生类的构造函数
- 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 }
- 运行结果:
Task 5:
- Person.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 6 using namespace std; 7 8 class Person { 9 public: 10 Person(){} 11 Person(const string n, const string t, const string e = ""); 12 Person(const Person& p1); 13 ~Person() = default; 14 void update_telephone(); 15 void update_email(); 16 friend ostream& operator<<(ostream& out, const Person p); 17 friend istream& operator>>(istream& in, Person& p); 18 friend bool operator==(Person p1, Person p2); 19 20 string name; 21 string telephone; 22 string email; 23 }; 24 Person::Person(const string n, const string t, const string e ) :name{ n }, email{ e },telephone { t } {} 25 Person::Person(const Person& p1) :name{ p1.name }, email{ p1.email }, telephone{ p1.telephone } {} 26 void Person::update_telephone() { 27 cin.clear(); 28 cin.sync(); 29 cout << "Enter the telephone number: "; 30 string new_tele; 31 cin >> new_tele; 32 telephone = new_tele; 33 cout << "telephone number has been updated...\n"; 34 } 35 void Person::update_email() { 36 cin.clear(); 37 cin.sync(); 38 cout << "Enter the email address: "; 39 string new_email; 40 cin >> new_email; 41 email = new_email; 42 cout << "email address has been updated...\n"; 43 } 44 45 ostream& operator<<(ostream& out, const Person p) { 46 out << left << setw(16) << p.name 47 << setw(16) << p.telephone 48 << p.email; 49 return out; 50 } 51 istream& operator>>(istream& in, Person& p) { 52 if (in.peek() == '\n') 53 in.ignore(); 54 //in.clear(); in.sync(); 55 getline(in, p.name); 56 getline(in, p.telephone); 57 getline(in, p.email); 58 return in; 59 } 60 bool operator==(Person p1, Person p2) { 61 return (p1.name == p2.name) && ( p1.telephone == p2.telephone); 62 } 63 64 //getline()重载函数有输入流的成员函数和string类的函数,函数的形参也不同
- task5.cpp
1 #include<iostream> 2 #include<fstream> 3 #include<vector> 4 #include<vector> 5 #include"Person.hpp" 6 7 void test() { 8 using namespace std; 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 }
- 运行结果
Task 6:
- container.h
1 //======================= 2 // container.h 3 //======================= 4 5 // The so-called inventory of a player in RPG games 6 // contains two items, heal and magic waterRPG游戏中玩家的所谓库存包含两个物品,治疗和魔法水 7 8 #ifndef _CONTAINER // Conditional compilation 条件编译,避免声明冲突 9 #define _CONTAINER 10 11 class container // Inventory 库 12 { 13 protected: 14 int numOfHeal; // number of heal 治疗包数量 15 int numOfMW; // number of magic water 药水数量 16 public: 17 container(); // constuctor 构造 18 void set(int heal_n, int mw_n); // set the items numbers 设置库存数量 19 int nOfHeal(); // get the number of heal 获得数量 20 int nOfMW(); // get the number of magic water 获得数量 21 void display(); // display the items; 展示物品 22 bool useHeal(); // use heal 使用治疗包 23 bool useMW(); // use magic water 使用药水 24 }; 25 26 #endif
- 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.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 11 #include <iomanip> // use for setting field width 12 #include <time.h> // use for generating random factor生成随机因子 13 #include "container.h" 14 #include<iostream> 15 using namespace std; 16 17 enum job { sw, ar, mg }; /* define 3 jobs by enumerate type 18 sword man刺客, archer射手, mage法师 */ 19 class player 20 { 21 friend void showinfo(player& p1, player& p2); 22 friend class swordsman; 23 24 protected: 25 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 26 // General properties of all characters 27 string name; // character name 28 job role; /* character's job, one of swordman, archer and mage, 29 as defined by the enumerate type */ 30 container bag; // character's inventory 库 31 32 public: 33 virtual bool attack(player& p) = 0; // normal attack 34 virtual bool specialatt(player& p) = 0; //special attack 35 virtual void isLevelUp() = 0; // level up judgement 36 /* Attention! 37 These three methods are called "Pure virtual functions". 38 They have only declaration, but no definition. 39 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 40 The detailed definition of these pure virtual functions will be given in subclasses.这三种方法称为“纯虚函数”。 41 他们只有声明,但没有定义。 42 具有纯虚函数的类称为“抽象类”,它只能用于继承对象,而不能用于构造函数对象。 43 这些纯虚函数的详细定义将在子类中给出 */ 44 45 void reFill(); // character's HP生命值 and MP(蓝条?。) resume恢复 46 bool death(); // report whether character is dead 通报死亡 47 void isDead(); // check whether character is dead 检查是否死亡 48 bool useHeal(); // consume heal, irrelevant to job 使用治疗血包?(与位置无关) 49 bool useMW(); // consume magic water, irrelevant to job 使用治疗? 50 void transfer(player& p); // possess opponent's items after victory 胜利后拥有对方的物品 51 void showRole(); // display character's job 展示角色职位 52 53 private: 54 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 无论角色是否死亡,都不需要访问或继承 55 }; 56 57 #endif
- player.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 #include"player.h" 5 #include<iomanip> 6 #include<iostream> 7 using namespace std; 8 9 // character's HP and MP resume 10 void player::reFill() 11 { 12 HP = HPmax; // HP and MP fully recovered 13 MP = MPmax; 14 } 15 16 // report whether character is dead 17 bool player::death() 18 { 19 return playerdeath; 20 } 21 22 // check whether character is dead 23 void player::isDead() 24 { 25 if (HP <= 0) // HP less than 0, character is dead 26 { 27 cout << name << " is Dead." << endl; 28 system("pause"); 29 playerdeath = 1; // give the label of death value 1 30 } 31 } 32 33 // consume heal, irrelevant to job 34 bool player::useHeal() 35 { 36 if (bag.nOfHeal() > 0) 37 { 38 HP = HP + 100; 39 if (HP > HPmax) // HP cannot be larger than maximum value 40 HP = HPmax; // so assign it to HPmax, if necessary 41 cout << name << " used Heal, HP increased by 100." << endl; 42 bag.useHeal(); // use heal 43 system("pause"); 44 return 1; // usage of heal succeed 45 } 46 else // If no more heal in bag, cannot use 47 { 48 cout << "Sorry, you don't have heal to use." << endl; 49 system("pause"); 50 return 0; // usage of heal failed 51 } 52 } 53 54 // consume magic water, irrelevant to job 55 bool player::useMW() 56 { 57 if (bag.nOfMW() > 0) 58 { 59 MP = MP + 100; 60 if (MP > MPmax) 61 MP = MPmax; 62 cout << name << " used Magic Water, MP increased by 100." << endl; 63 bag.useMW(); 64 system("pause"); 65 return 1; // usage of magic water succeed 66 } 67 else 68 { 69 cout << "Sorry, you don't have magic water to use." << endl; 70 system("pause"); 71 return 0; // usage of magic water failed 72 } 73 } 74 75 // possess opponent's items after victory 76 void player::transfer(player& p) 77 { 78 cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl; 79 system("pause"); 80 bag.set(p.bag.nOfHeal(), 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.h
1 //======================= 2 // swordsman.h 3 //======================= 4 5 // Derived from base class player 6 // For the job Swordsman 7 8 #include "player.h" 9 class swordsman : public player // subclass swordsman publicly inherited from base player 10 { 11 public: 12 swordsman(int lv_in = 1, string name_in = "Not Given"); 13 // constructor with default level of 1 and name of "Not given" 14 void isLevelUp(); 15 bool attack(player& p); 16 bool specialatt(player& p); 17 /* These three are derived from the pure virtual functions of base class 18 The definition of them will be given in this subclass. */ 19 void AI(player& p); // Computer opponent 20 };
- swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 5 #include"swordsman.h" 6 #include<iostream> 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 #include "swordsman.h" 10 using namespace std; 11 12 13 int main() 14 { 15 string tempName; 16 bool success = 0; //flag for storing whether operation is successful 17 cout << "Please input player's name: "; 18 cin >> tempName; // get player's name from keyboard input 19 player* human = NULL; // use pointer of base class, convenience for polymorphism 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 human = new swordsman(1, tempName); // create the character with user inputted name and job 30 success = 1; // operation succeed 31 break; 32 default: 33 break; // In this case, success=0, character creation failed 34 } 35 } while (success != 1); // so the loop will ask user to re-create a character 36 37 int tempCom; // temp command inputted by user 38 int nOpp = 0; // the Nth opponent 39 for (int i = 1; nOpp < 5; i += 2) // i is opponent's level 40 { 41 nOpp++; 42 system("cls"); 43 cout << "STAGE" << nOpp << endl; 44 cout << "Your opponent, a Level " << i << " Swordsman." << endl; 45 system("pause"); 46 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 47 human->reFill(); // get HP/MP refill before start fight 48 49 while (!human->death() && !enemy.death()) // no died 50 { 51 success = 0; 52 while (success != 1) 53 { 54 showinfo(*human, enemy); // show fighter's information 55 cout << "Please give command: " << endl; 56 cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl; 57 cin >> tempCom; 58 switch (tempCom) 59 { 60 case 0: 61 cout << "Are you sure to exit? Y/N" << endl; 62 char temp; 63 cin >> temp; 64 if (temp == 'Y' || temp == 'y') 65 return 0; 66 else 67 break; 68 case 1: 69 success = human->attack(enemy); 70 human->isLevelUp(); 71 enemy.isDead(); 72 break; 73 case 2: 74 success = human->specialatt(enemy); 75 human->isLevelUp(); 76 enemy.isDead(); 77 break; 78 case 3: 79 success = human->useHeal(); 80 break; 81 case 4: 82 success = human->useMW(); 83 break; 84 default: 85 break; 86 } 87 } 88 if (!enemy.death()) // If AI still alive 89 enemy.AI(*human); 90 else // AI died 91 { 92 cout << "YOU WIN" << endl; 93 human->transfer(enemy); // player got all AI's items 94 } 95 if (human->death()) 96 { 97 system("cls"); 98 cout << endl << setw(50) << "GAME OVER" << endl; 99 delete human; // player is dead, program is getting to its end, what should we do here? 100 system("pause"); 101 return 0; 102 } 103 } 104 } 105 delete human; // You win, program is getting to its end, what should we do here? 106 system("cls"); 107 cout << "Congratulations! You defeated all opponents!!" << endl; 108 system("pause"); 109 return 0; 110 }
- 运行截图
实验总结
- 熟悉了最近所学内容,加深了印象,掌握了格式、一般用在什么地方,掌握了基本的使用。
- 了解到学长对类的理解,大概明白制作一个小游戏要怎么划分类和功能。还需要进一步实践来深化理解。
- 上网搜索了有关实现播放声音的方法,尚未进行实践(待更新╥﹏╥...)