实验四
pets.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 class MachinePets { 6 public: 7 MachinePets()=default; 8 MachinePets(const string s) :nickname { s }{} 9 string get_nickname() const { return nickname; } 10 virtual string talk()=0; 11 private: 12 string nickname; 13 }; 14 class PetCats :public MachinePets { 15 using MachinePets::MachinePets; 16 public: 17 //PetCats(const string s):MachinePets{s}{} 18 string talk() { return "miao wu~"; } 19 }; 20 class PetDogs :public MachinePets { 21 using MachinePets::MachinePets; 22 public: 23 //PetDogs(const string s) :MachinePets{ s }{} 24 string talk() { return "wang wang"; } 25 };
task4.cpp
1 #include <iostream> 2 #include "pets.hpp" 3 void play(MachinePets& obj) { 4 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 5 } 6 void test() { 7 PetCats cat("miku"); 8 PetDogs dog("da huang"); 9 play(cat); 10 play(dog); 11 } 12 int main() { 13 test(); 14 }
实验截图
实验五
Person.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 class Person { 7 public: 8 Person(){} 9 Person(string n, string t, string e="") :name{n}, telephone{t}, email{e}{} 10 Person(const Person&t):name{t.name},telephone{t.telephone},email{t.email}{} 11 void update_telephone(); 12 void update_email(); 13 friend ostream& operator<<(ostream& cout, const Person& t); 14 friend istream& operator>>(istream& cin, Person& t); 15 friend bool operator==(Person& t1,Person &t2); 16 private: 17 string name; 18 string telephone; 19 string email; 20 }; 21 ostream& operator<<(ostream& cout, const Person& t) 22 { 23 cout <<left<<setw(15)<< t.name << setw(15) << t.telephone << setw(15) << t.email << endl; 24 return cout; 25 } 26 istream& operator>>(istream& cin, Person& t) 27 { 28 getline(cin, t.name); 29 getline(cin, t.telephone); 30 getline(cin, t.email); 31 cout << endl; 32 return cin; 33 } 34 bool operator==(Person& t1, Person& t2) 35 { 36 if (t1.name == t2.name && t1.telephone == t2.telephone) return true; 37 else return false; 38 } 39 void Person::update_email() { 40 cout << "Enter the email address:"; 41 cin.clear(); 42 cin >> email; 43 44 cout << "email address has been updated..." << endl; 45 } 46 void Person::update_telephone() 47 { 48 cout << "Enter the telephone number:"; 49 cin.clear(); 50 cin >> telephone; 51 52 cout << "telephone number has been updated..." << endl; 53 }
task5.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include "Person.hpp" 5 void test() { 6 using namespace std; 7 vector<Person> phone_book; 8 Person p; 9 cout << "Enter person's contact until press Ctrl + Z" << endl; 10 while (cin >> p) 11 phone_book.push_back(p); 12 cout << "\nupdate someone's contact: \n"; 13 phone_book.at(0).update_telephone(); 14 phone_book.at(0).update_email(); 15 cout << "\ndisplay all contacts' info\n"; 16 for (auto& phone : phone_book) 17 cout << phone << endl; 18 cout << "\ntest whether the same contact\n"; 19 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 20 } 21 int main() { 22 test(); 23 }
实验截图
实验六
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 water 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
play.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<string> 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 #include<string> 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 }
play.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 #include<iostream> 5 #include<iomanip> 6 #include"player.h" 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 HP += p.bag.nOfHeal(); 81 MP += p.bag.nOfMW(); 82 // set the character's bag, get opponent's items 83 } 84 85 // display character's job 86 void player::showRole() 87 { 88 switch(role) 89 { 90 case sw: 91 cout<<"Swordsman"; 92 break; 93 case ar: 94 cout<<"Archer"; 95 break; 96 case mg: 97 cout<<"Mage"; 98 break; 99 default: 100 break; 101 } 102 } 103 104 105 // display character's job 106 void showinfo(player& p1, player& p2) 107 { 108 system("cls"); 109 cout<<"##############################################################"<<endl; 110 cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 111 <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 112 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 113 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 114 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 115 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 116 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 117 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 118 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 119 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 120 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 121 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 122 cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); 123 p1.showRole(); 124 cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); 125 p2.showRole(); 126 cout<<" #"<<endl; 127 cout<<"--------------------------------------------------------------"<<endl; 128 p1.bag.display(); 129 cout<<"##############################################################"<<endl; 130 }
swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 #include<iostream> 5 #include"swordsman.h" 6 using namespace std; 7 // constructor. default values don't need to be repeated here 8 swordsman::swordsman(int lv_in, string name_in) 9 { 10 role=sw; // enumerate type of job 11 LV=lv_in; 12 name=name_in; 13 14 // Initialising the character's properties, based on his level 15 HPmax=150+8*(LV-1); // HP increases 8 point2 per level 16 HP=HPmax; 17 MPmax=75+2*(LV-1); // MP increases 2 points per level 18 MP=MPmax; 19 AP=25+4*(LV-1); // AP increases 4 points per level 20 DP=25+4*(LV-1); // DP increases 4 points per level 21 speed=25+2*(LV-1); // speed increases 2 points per level 22 23 playerdeath=0; 24 EXP=LV*LV*75; 25 bag.set(lv_in, lv_in); 26 } 27 28 void swordsman::isLevelUp() 29 { 30 if(EXP>=LV*LV*75) 31 { 32 LV++; 33 AP+=4; 34 DP+=4; 35 HPmax+=8; 36 MPmax+=2; 37 speed+=2; 38 cout<<name<<" Level UP!"<<endl; 39 cout<<"HP improved 8 points to "<<HPmax<<endl; 40 cout<<"MP improved 2 points to "<<MPmax<<endl; 41 cout<<"Speed improved 2 points to "<<speed<<endl; 42 cout<<"AP improved 4 points to "<<AP<<endl; 43 cout<<"DP improved 5 points to "<<DP<<endl; 44 system("pause"); 45 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 46 } 47 } 48 49 bool swordsman::attack(player &p) 50 { 51 double HPtemp=0; // opponent's HP decrement 52 double EXPtemp=0; // player obtained exp 53 double hit=1; // attach factor, probably give critical attack 54 srand((unsigned)time(NULL)); // generating random seed based on system time 55 56 // If speed greater than opponent, you have some possibility to do double attack 57 if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 58 { 59 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 60 cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl; 61 p.HP=int(p.HP-HPtemp); 62 EXPtemp=(int)(HPtemp*1.2); 63 } 64 65 // If speed smaller than opponent, the opponent has possibility to evade 66 if ((speed<p.speed) && (rand()%50<1)) 67 { 68 cout<<name<<"'s attack has been evaded by "<<p.name<<endl; 69 system("pause"); 70 return 1; 71 } 72 73 // 10% chance give critical attack 74 if (rand()%100<=10) 75 { 76 hit=1.5; 77 cout<<"Critical attack: "; 78 } 79 80 // Normal attack 81 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); 82 cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 83 EXPtemp=(int)(EXPtemp+HPtemp*1.2); 84 p.HP=(int)(p.HP-HPtemp); 85 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 86 EXP=(int)(EXP+EXPtemp); 87 system("pause"); 88 return 1; // Attack success 89 } 90 91 bool swordsman::specialatt(player &p) 92 { 93 if(MP<40) 94 { 95 cout<<"You don't have enough magic points!"<<endl; 96 system("pause"); 97 return 0; // Attack failed 98 } 99 else 100 { 101 MP-=40; // consume 40 MP to do special attack 102 103 //10% chance opponent evades 104 if(rand()%100<=10) 105 { 106 cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; 107 system("pause"); 108 return 1; 109 } 110 111 double HPtemp=0; 112 double EXPtemp=0; 113 //double hit=1; 114 //srand(time(NULL)); 115 HPtemp=(int)(AP*1.2+20); // not related to opponent's DP 116 EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience 117 cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 118 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 119 p.HP=(int)(p.HP-HPtemp); 120 EXP=(int)(EXP+EXPtemp); 121 system("pause"); 122 } 123 return 1; // special attack succeed 124 } 125 126 // Computer opponent 127 void swordsman::AI(player &p) 128 { 129 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))) 130 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 131 { 132 useHeal(); 133 } 134 else 135 { 136 if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) 137 // AI has enough MP, it has 30% to make special attack 138 { 139 specialatt(p); 140 p.isDead(); // check whether player is dead 141 } 142 else 143 { 144 if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) 145 // Not enough MP && HP is safe && still has magic water 146 { 147 useMW(); 148 } 149 else 150 { 151 attack(p); // normal attack 152 p.isDead(); 153 } 154 } 155 } 156 }
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 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 } 113
实验截图
标签:string,int,void,player,实验,include,HP From: https://www.cnblogs.com/lee404error/p/16936429.html