四、实验结论
1.实验任务1
子任务1.2
Complex.hpp源码:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include<iostream> 5 6 using namespace std; 7 8 class Complex { 9 private: 10 double real; 11 double imag; 12 public: 13 Complex(double real0=0,double imag0=0):real {real0},imag {imag0} {} 14 Complex(const Complex &obj):real(obj.real),imag(obj.imag) {} 15 16 double get_real()const; 17 double get_imag()const; 18 19 Complex &operator+=(const Complex &obj); 20 21 friend Complex operator+(Complex obj1,Complex obj2); 22 friend bool operator==(const Complex &obj1,const Complex &obj2); 23 24 friend ostream &operator<<(ostream &out,const Complex &obj); 25 friend istream &operator>>(istream &in,Complex &obj); 26 }; 27 28 double Complex::get_real()const { 29 return real; 30 } 31 double Complex::get_imag()const { 32 return imag; 33 } 34 35 Complex &Complex::operator+=(const Complex &obj) { 36 real=real+obj.real; 37 imag=imag+obj.imag; 38 return *this; 39 } 40 41 Complex operator+(Complex obj1,Complex obj2) { 42 return Complex(obj1.real+obj2.real,obj1.imag+obj2.imag); 43 } 44 bool operator==(const Complex &obj1,const Complex &obj2) { 45 if(obj1.real==obj2.real&&obj1.imag==obj2.imag) return true; 46 else return false; 47 } 48 49 ostream &operator<<(ostream &out,const Complex &obj) { 50 out<<"("<<obj.real<<","<<obj.imag<<")"<<endl; 51 return out; 52 } 53 istream &operator>>(istream &in,Complex &obj) { 54 cout<<"input format:a+bi"<<endl; 55 double real0,imag0; 56 char sign,i; 57 in>>real0>>sign>>imag0>>i; 58 if(imag0==0) { 59 obj.imag=0; 60 } else { 61 sign=='-'?obj.imag=-imag0:obj.imag=imag0; 62 } 63 obj.real=real0; 64 return in; 65 } 66 #endif
Complex.cpp源码:
1 #include"Complex.hpp" 2 #include<iostream> 3 4 using namespace std; 5 6 int main() { 7 Complex c1(1,2);//构造函数 8 Complex c2(c1);//复制构造函数 9 10 cout<<"c1"<<c1;//<<已经重载 11 cout<<"c2"<<c2; 12 13 Complex c3; 14 cin>>c3;//>>已经重载 15 cout<<"c3"<<c3; 16 17 Complex c4; 18 c4=c1+c2;//+已经重载 19 cout<<endl<<"c4=c1+c2"<<endl<<"c4"<<c4; 20 21 c3+=c1; 22 cout<<endl<<"c3+=c1"<<endl<<"c3"<<c3;//+=已经重载 23 24 cout<<endl<<"c3==c4:"<<boolalpha<<(c3==c4);//==已经重载 25 }
测试结果:
子任务1.3
Complex_Template.hpp源码:
1 #ifndef COMPLEX_TEMPLATE 2 #define COMPLEX_TEMPLATE 3 4 #include<iostream> 5 6 using namespace std; 7 8 template<typename T> 9 class Complex { 10 private: 11 T real; 12 T imag; 13 public: 14 Complex(T real0=0,T imag0=0):real(real0),imag(imag0) {} 15 Complex(const Complex<T> &obj):real(obj.real),imag(obj.imag){} 16 17 T get_real() const {return real;} 18 T get_imag() const {return imag;} 19 20 Complex<T> operator+=(const Complex<T> &obj); 21 22 template<typename T1> 23 friend Complex<T1> operator+(const Complex<T1> &obj1,const Complex<T1> &obj2); 24 25 template<typename T1> 26 friend bool operator==(const Complex<T1> &obj1,const Complex<T1> &obj2); 27 28 template<typename T1> 29 friend ostream &operator<<(ostream &out,const Complex<T1> &obj); 30 31 template<typename T1> 32 friend istream &operator>>(istream &in,Complex<T1> &obj); 33 }; 34 35 template<typename T> 36 Complex<T> Complex<T>::operator+=(const Complex<T> &obj) { 37 real+=obj.real; 38 imag+=obj.imag; 39 } 40 41 template<typename T1> 42 Complex<T1> operator+(const Complex<T1> &obj1,const Complex<T1> &obj2) { 43 return Complex<T1>(obj1.real+obj2.real,obj1.imag+obj2.imag); 44 } 45 46 template<typename T1> 47 bool operator==(const Complex<T1> &obj1,const Complex<T1> &obj2) { 48 if(obj1.real==obj2.real&&obj1.imag==obj2.imag) 49 return true; 50 else 51 return false; 52 } 53 54 template<typename T1> 55 ostream &operator<<(ostream &out,const Complex<T1> &obj){ 56 out<<"("<<obj.real<<","<<obj.imag<<")"<<endl; 57 return out; 58 } 59 60 template<typename T1> 61 istream &operator>>(istream &in,Complex<T1> &obj){ 62 cout<<"format:a+bi"<<endl; 63 double a,b; 64 char sign,I; 65 in>>a>>sign>>b>>I; 66 if(b!=0){ 67 sign=='+'?obj.imag=b:obj.imag=-b; 68 } 69 obj.real=a; 70 return in; 71 } 72 73 #endif
Complex_Template.cpp源码:
1 #include"Complex_Template.hpp" 2 3 int main(){ 4 Complex<int> c1(1,1); 5 cout<<"c1"<<c1<<endl; 6 Complex<double> c2(1.5,1.5); 7 cout<<"c2"<<c2<<endl;//int,double都可以 8 9 Complex<double> c3; 10 cin>>c3; //>>可以用 11 cout<<"c3"<<c3<<endl;//<<可以用 12 13 Complex<double> c4; 14 c4=c2+c3; 15 cout<<"c4=c2+c3"<<endl; 16 cout<<"c4"<<c4<<endl;//+可以用 17 18 Complex<int> c5(c1); 19 cout<<"c5=c1"<<endl<<"c5"<<c5; 20 c1+=c5; 21 cout<<"c1+=c5"<<endl; 22 cout<<"c1"<<c1<<endl;//+=可以用 23 24 cout<<"c1==c5"<<endl<<boolalpha<<(c1==c5)<<endl<<endl;//==可以用 25 26 Complex<double> c6(3,3); 27 cout<<"c6"<<c6; 28 cout<<"c4==c6"<<endl<<boolalpha<<(c4==c6)<<endl; 29 }
测试结果:
2. 实验任务2
Person.hpp源码:
1 #ifndef PERSON_HPP 2 #define PERSON_HPP 3 4 #include<iostream> 5 #include<string> 6 #include<iomanip> 7 8 using namespace std; 9 10 class Person { 11 public: 12 string name; 13 string telephone; 14 string email; 15 Person(); 16 Person(string name0,string telephone0,string email0=""); 17 void change_telephone(); 18 void change_email(); 19 friend istream & operator>>(istream & in,Person & obj); 20 friend ostream & operator<<(ostream & out,const Person & obj); 21 friend bool operator==(const Person & obj1,const Person & obj2); 22 }; 23 24 Person::Person(){} 25 Person::Person(string name0,string telephone0,string email0/*=""*/):name(name0),telephone(telephone0),email(email0) {} 26 27 void Person::change_telephone() { 28 string new_telephone; 29 cin>>new_telephone; 30 this->telephone=new_telephone; 31 } 32 void Person::change_email() { 33 string new_email; 34 cin>>new_email; 35 this->email=new_email; 36 } 37 38 istream & operator>>(istream & in,Person & obj) { 39 cin>>ws; 40 getline(in,obj.name); 41 in>>obj.telephone; 42 in>>obj.email; 43 return in; 44 } 45 ostream & operator<<(ostream & out,const Person & obj) { 46 out<<setw(20)<<left<<obj.name<<setw(20)<<left<<obj.telephone<<setw(20)<<left<<obj.email; 47 return out; 48 } 49 bool operator==(const Person & obj1,const Person & obj2) { 50 if(obj1.name==obj2.name&&obj1.telephone==obj2.telephone) 51 return true; 52 else 53 return false; 54 } 55 56 #endif
task2.cpp源码:
1 #include <iostream> 2 #include <fstream> 3 #include <vector> 4 #include "Person.hpp" 5 6 int main() 7 { 8 using namespace std; 9 10 vector<Person> phone_book; 11 Person p; 12 13 while(cin>>p) 14 phone_book.push_back(p); 15 16 for(auto &i: phone_book) 17 cout << i << endl; 18 19 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 20 21 ofstream fout; 22 23 fout.open("phone_book.txt"); 24 25 if(!fout.is_open()) 26 { 27 cerr << "fail to open file phone_book.txt\n"; 28 return 1; 29 } 30 31 for(auto &i: phone_book) 32 fout << i << endl; 33 34 fout.close(); 35 }
测试结果:
答①:operator<<(cout,i);
答②:operator<<(fout,i);
3.实验任务3
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 //1_????????????? // Conditional compilation 9 #ifndef _CONTAINER 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 // 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 //2_???????? 47 numOfHeal--; 48 return 1; // use heal successfully 49 } 50 51 //use magic water 52 bool container::useMW() 53 { 54 numOfMW--; 55 return 1; // use magic water successfully 56 }
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<string> 15 16 using namespace std; 17 18 enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 19 sword man, archer, mage */ 20 class player 21 { 22 friend void showinfo(player &p1, player &p2); 23 friend class swordsman; 24 25 protected: 26 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 27 // General properties of all characters 28 string name; // character name 29 job role; /* character's job, one of swordman, archer and mage, 30 as defined by the enumerate type */ 31 container bag; // character's inventory 32 33 public: 34 virtual bool attack(player &p)=0; // normal attack 35 virtual bool specialatt(player &p)=0; //special attack 36 virtual void isLevelUp()=0; // level up judgement 37 /* Attention! 38 These three methods are called "Pure virtual functions". 39 They have only declaration, but no definition. 40 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 41 The detailed definition of these pure virtual functions will be given in subclasses. */ 42 43 void reFill(); // character's HP and MP resume 44 bool death(); // report whether character is dead 45 void isDead(); // check whether character is dead 46 bool useHeal(); // consume heal, irrelevant to job 47 bool useMW(); // consume magic water, irrelevant to job 48 void transfer(player &p); // possess opponent's items after victory 49 void showRole(); // display character's job 50 51 private: 52 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 53 }; 54 55 #endif
player.cpp源码:
1 //======================= 2 // player.cpp 3 //======================= 4 5 6 // character's HP and MP resume 7 #include<iostream> 8 #include"player.h" 9 #include"container.h" 10 11 void player::reFill() 12 { 13 HP=HPmax; // HP and MP fully recovered 14 MP=MPmax; 15 } 16 17 // report whether character is dead 18 bool player::death() 19 { 20 return playerdeath; 21 } 22 23 // check whether character is dead 24 void player::isDead() 25 { 26 if(HP<=0) // HP less than 0, character is dead 27 { 28 cout<<name<<" is Dead." <<endl; 29 system("pause"); 30 playerdeath=1; // give the label of death value 1 31 } 32 } 33 34 // consume heal, irrelevant to job 35 bool player::useHeal() 36 { 37 if(bag.nOfHeal()>0) 38 { 39 HP=HP+100; 40 if(HP>HPmax) // HP cannot be larger than maximum value 41 HP=HPmax; // so assign it to HPmax, if necessary 42 cout<<name<<" used Heal, HP increased by 100."<<endl; 43 bag.useHeal(); // use heal 44 system("pause"); 45 return 1; // usage of heal succeed 46 } 47 else // If no more heal in bag, cannot use 48 { 49 cout<<"Sorry, you don't have heal to use."<<endl; 50 system("pause"); 51 return 0; // usage of heal failed 52 } 53 } 54 55 // consume magic water, irrelevant to job 56 bool player::useMW() 57 { 58 if(bag.nOfMW()>0) 59 { 60 MP=MP+100; 61 if(MP>MPmax) 62 MP=MPmax; 63 cout<<name<<" used Magic Water, MP increased by 100."<<endl; 64 bag.useMW(); 65 system("pause"); 66 return 1; // usage of magic water succeed 67 } 68 else 69 { 70 cout<<"Sorry, you don't have magic water to use."<<endl; 71 system("pause"); 72 return 0; // usage of magic water failed 73 } 74 } 75 76 // possess opponent's items after victory 77 void player::transfer(player &p) 78 { 79 cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; 80 system("pause"); 81 //3_??????????? 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 //4_?????????????? 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 //======================= 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//5_????????? // 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 // constructor. default values don't need to be repeated here 6 #include"swordsman.h" 7 #include<iostream> 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 }
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 int tempJob; // temp choice for job selection 22 do 23 { 24 cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; 25 cin>>tempJob; 26 system("cls"); // clear the screen 27 switch(tempJob) 28 { 29 case 1: 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; //6_??????????? // 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;//7_????????? // 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 }
测试结果: