task4:
源代码:
1 #include <iostream> 2 #include<cstring> 3 using namespace std; 4 class MachinePets{ 5 private: 6 string nickname; 7 public: 8 MachinePets(const string s) 9 { 10 nickname=s; 11 } 12 string get_nickname() const 13 { 14 return nickname; 15 } 16 virtual string talk()=0; 17 }; 18 class PetCats:public MachinePets{ 19 public: 20 PetCats(const string s):MachinePets(s){ 21 } 22 string talk(){ 23 return "miaodayo!"; 24 } 25 26 }; 27 class PetDogs:public MachinePets{ 28 public: 29 PetDogs(const string s):MachinePets(s){ 30 } 31 string talk(){ 32 return "wang wang"; 33 } 34 35 };
运行结果:
task5:
源代码:
1 #include<cstring> 2 #include <iostream> 3 using namespace std; 4 class Person{ 5 private: 6 string name,telephone,email; 7 public: 8 Person(){ 9 } 10 Person(string a,string b,string c="") 11 { 12 name=a; 13 telephone=b; 14 email=c; 15 } 16 Person(const Person &ss) 17 { 18 name=ss.name; 19 telephone=ss.telephone; 20 email=ss.email; 21 } 22 void update_telephone(){ 23 cin.clear(); 24 string t1; 25 cout << "Enter the telephone number:"; 26 cin >> t1; 27 this->telephone = t1; 28 cout << "telephone number has been updated..." << endl; 29 } 30 void update_email(){ 31 cin.clear(); 32 string e1; 33 cout << "Enter the email address:"; 34 cin >> e1; 35 this->email = e1; 36 cout << "email address has been updated..." << endl; 37 } 38 friend std::ostream& operator<<(std::ostream &out,const Person &c); 39 friend std::istream& operator>>(std::istream &in,Person &c); 40 friend bool operator==(const Person &yi,const Person &er); 41 }; 42 bool operator==(const Person &yi,const Person &er) 43 { 44 return yi.name==er.name&&yi.telephone==er.telephone&&yi.email==er.email; 45 } 46 std::ostream& operator<<(std::ostream &out,const Person &c){ 47 out << c.name <<"\t" << c.telephone << "\t" << c.email; 48 return out; 49 } 50 std::istream& operator>>(std::istream &in,Person &c){ 51 in >> c.name >> c.telephone >> c.email; 52 return in; 53 }
运行结果:
task6:
源代码:
container.cpp
1 //======================= 2 // container.cpp 3 //======================= 4 5 // default constructor initialise the inventory as empty 6 #include "container.h" 7 #include <iostream> 8 9 using namespace std; 10 11 container::container() 12 { 13 set(0,0); 14 } 15 16 // set the item numbers 17 void container::set(int heal_n, int mw_n) 18 { 19 numOfHeal=heal_n; 20 numOfMW=mw_n; 21 } 22 23 // get the number of heal 24 int container::nOfHeal() 25 { 26 return numOfHeal; 27 } 28 29 // get the number of magic water 30 int container::nOfMW() 31 { 32 return numOfMW; 33 } 34 35 // display the items; 36 void container::display() 37 { 38 cout<<"Your bag contains: "<<endl; 39 cout<<"Heal(HP+100): "<<numOfHeal<<endl; 40 cout<<"Magic Water (MP+80): "<<numOfMW<<endl; 41 } 42 43 //use heal 44 bool container::useHeal() 45 { 46 numOfHeal--; 47 return 1; // use heal successfully 48 } 49 50 //use magic water 51 bool container::useMW() 52 { 53 numOfMW--; 54 return 1; // use magic water successfully 55 }
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
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 int main() 12 { 13 string tempName; 14 bool success=0; //flag for storing whether operation is successful 15 cout <<"Please input player's name: "; 16 cin >>tempName; // get player's name from keyboard input 17 player *human; // use pointer of base class, convenience for polymorphism 18 int tempJob; // temp choice for job selection 19 do 20 { 21 cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; 22 cin>>tempJob; 23 system("cls"); // clear the screen 24 switch(tempJob) 25 { 26 case 1: 27 human=new swordsman(1,tempName); // create the character with user inputted name and job 28 success=1; // operation succeed 29 break; 30 default: 31 break; // In this case, success=0, character creation failed 32 } 33 }while(success!=1); // so the loop will ask user to re-create a character 34 35 int tempCom; // temp command inputted by user 36 int nOpp=0; // the Nth opponent 37 for(int i=1;nOpp<5;i+=2) // i is opponent's level 38 { 39 nOpp++; 40 system("cls"); 41 cout<<"STAGE" <<nOpp<<endl; 42 cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl; 43 system("pause"); 44 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 45 human->reFill(); // get HP/MP refill before start fight 46 47 while(!human->death() && !enemy.death()) // no died 48 { 49 success=0; 50 while (success!=1) 51 { 52 showinfo(*human,enemy); // show fighter's information 53 cout<<"Please give command: "<<endl; 54 cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl; 55 cin>>tempCom; 56 switch(tempCom) 57 { 58 case 0: 59 cout<<"Are you sure to exit? Y/N"<<endl; 60 char temp; 61 cin>>temp; 62 if(temp=='Y'||temp=='y') 63 return 0; 64 else 65 break; 66 case 1: 67 success=human->attack(enemy); 68 human->isLevelUp(); 69 enemy.isDead(); 70 break; 71 case 2: 72 success=human->specialatt(enemy); 73 human->isLevelUp(); 74 enemy.isDead(); 75 break; 76 case 3: 77 success=human->useHeal(); 78 break; 79 case 4: 80 success=human->useMW(); 81 break; 82 default: 83 break; 84 } 85 } 86 if(!enemy.death()) // If AI still alive 87 enemy.AI(*human); 88 else // AI died 89 { 90 cout<<"YOU WIN"<<endl; 91 human->transfer(enemy); // player got all AI's items 92 } 93 if (human->death()) 94 { 95 system("cls"); 96 cout<<endl<<setw(50)<<"GAME OVER"<<endl; 97 delete human; // player is dead, program is getting to its end, what should we do here? 98 system("pause"); 99 return 0; 100 } 101 } 102 } 103 delete human; // You win, program is getting to its end, what should we do here? 104 system("cls"); 105 cout<<"Congratulations! You defeated all opponents!!"<<endl; 106 system("pause"); 107 return 0; 108 } 109
player.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 5 6 // character's HP and MP resume 7 #include "player.h" 8 #include <iostream> 9 10 using namespace std; 11 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 bag.set(bag.nOfHeal()+p.bag.nOfHeal(),bag.nOfMW()+p.bag.nOfMW()); 83 // set the character's bag, get opponent's items 84 } 85 86 // display character's job 87 void player::showRole() 88 { 89 switch(role) 90 { 91 case sw: 92 cout<<"Swordsman"; 93 break; 94 case ar: 95 cout<<"Archer"; 96 break; 97 case mg: 98 cout<<"Mage"; 99 break; 100 default: 101 break; 102 } 103 } 104 105 106 // display character's job 107 void showinfo(player &p1, player &p2) 108 { 109 system("cls"); 110 cout<<"##############################################################"<<endl; 111 cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 112 <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 113 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 114 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 115 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 116 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 117 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 118 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 119 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 120 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 121 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 122 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 123 cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); 124 p1.showRole(); 125 cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); 126 p2.showRole(); 127 cout<<" #"<<endl; 128 cout<<"--------------------------------------------------------------"<<endl; 129 p1.bag.display(); 130 cout<<"##############################################################"<<endl; 131 }
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 <string> 14 #include "container.h" 15 16 using namespace std; 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 void reFill(); // character's HP and MP resume 43 bool death(); // report whether character is dead 44 void isDead(); // check whether character is dead 45 bool useHeal(); // consume heal, irrelevant to job 46 bool useMW(); // consume magic water, irrelevant to job 47 void transfer(player &p); // possess opponent's items after victory 48 void showRole(); // display character's job 49 50 private: 51 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 52 }; 53 54 #endif
swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 // constructor. default values don't need to be repeated here 5 #include "swordsman.h" 6 #include <iostream> 7 8 using namespace std; 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 }
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 };
运行结果:
标签:string,int,HP,player,实验,include,void From: https://www.cnblogs.com/git-porn-hub/p/16937239.html