实验六
Posted zhaoluolong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验六相关的知识,希望对你有一定的参考价值。
实验六
chip类
#include<iostream> using namespace std; class base { private: int a, b; public: base(int x, int y) :a(x), b(y) {} int add() const { return a + b; } }; class A :public base { private: int a, b; public: A(int x, int y) :base(x, y), a(x), b(y) {} int minus() const { return a - b; } }; class B :public base { private: int a, b; public: B(int x, int y) :base(x, y), a(x), b(y) {} int multiply() const { return a * b; } }; class C :public base { private: int a, b; public: C(int x, int y) :base(x, y), a(x), b(y) {} double devide() const { return (double)a / (double)b; } }; int main() { base p1(1, 2); A p2(4, 1); B p3(2, 4); C p4(8, 5); cout << "--base--" << endl; cout << "1+2=" << p1.add() << endl; cout << "---A---" << endl; cout << "4+1=" << p2.add() << endl; cout << "4-1=" << p2.minus() << endl; cout << "---B---" << endl; cout << "2+4=" << p3.add() << endl; cout << "2*4=" << p3.multiply() << endl; cout << "---C---" << endl; cout << "8+5=" << p4.add() << endl; cout << "8/5=" << p4.devide() << endl; system("pause"); return 0; }
vehicle类
vehicle.h
#pragma once class vehicle { public: int maxspeed, weight; vehicle(int x, int y); void run(); void stop(); ~vehicle(); }; class bicycle :virtual public vehicle { public: int height; bicycle(int x, int y, int z); ~bicycle(); }; class motorcar :virtual public vehicle { public: int seatnum; motorcar(int x, int y, int z); ~motorcar(); }; class motorcycle :virtual public bicycle, virtual public motorcar { public: motorcycle(int x, int y, int z, int a); ~motorcycle(); };
vechicle.cpp
#include<iostream> #include"vehicle.h" using namespace std; vehicle::vehicle(int x, int y) : maxspeed(x), weight(y) { cout << "vehicle constructed" << endl; } void vehicle::run() { cout << "run" << endl; } void vehicle::stop() { cout << "stop" << endl; } vehicle::~vehicle() { cout << "vehicle destructed" << endl; } bicycle::bicycle(int x, int y, int z) :vehicle(x, y), height(z) { cout << "bicycle constructed" << endl; } bicycle::~bicycle() { cout << "bicycle destructed" << endl; } motorcar::motorcar(int x, int y, int z) :vehicle(x, y), seatnum(z) { cout << "motorcar constructed" << endl; } motorcar::~motorcar() { cout << "motorcar destructed" << endl; } motorcycle::motorcycle(int x, int y, int z, int a) :bicycle(x, y, z), motorcar(x, y, a), vehicle(x, y) { cout << "motorcycle constructed" << endl; } motorcycle::~motorcycle() { cout << "motorcycle destructed" << endl; }
main.cpp
#include<iostream> #include"vehicle.h" using namespace std; int main() { vehicle p1(2, 4); bicycle p2(1, 2, 3); motorcar p3(2, 3, 1); motorcycle p4(1, 4, 5, 7); p1.run(); p1.stop(); p2.run(); p2.stop(); p3.run(); p3.stop(); p4.run(); p4.stop(); cout << "------------------------" << endl; return 0; }
Fraction类
Fraction.h
#pragma once class Fraction { public: Fraction(int x, int y); Fraction(); Fraction(int x); Fraction(const Fraction &p); void pri();//输入 void showF();//输出 void transform();//转换小数 Fraction operator +(const Fraction &a); Fraction operator -(const Fraction &a); Fraction operator *(const Fraction &a); Fraction operator /(const Fraction &a); int ptop(); int pbottom(); private: int top; int bottom; };
Fraction.cpp
#include <iostream> #include <cmath> #include "Fraction.h" using namespace std; /*----------Fraction-----------*/ Fraction::Fraction(int x, int y) :top(x), bottom(y) { } Fraction::Fraction() : top(0), bottom(1) {} Fraction::Fraction(int x) : top(x), bottom(1) {} Fraction::Fraction(const Fraction &p) : top(p.top), bottom(p.bottom) {} void Fraction::pri() { cout << "请输入分子与分母:"; cin >> top >> bottom; } void Fraction::showF() { int s = top; while (!(top%s == 0 && bottom%s == 0)) { s--; } bottom /= s; top /= s;//约分 if (bottom<0) { bottom = fabs(bottom); top = -top; } if (bottom != 1) cout << top << ‘/‘ << bottom << endl; else cout << top << endl; } void Fraction::transform() { float i; i = (float)top / (float)bottom; cout << i << endl; } Fraction Fraction::operator+(const Fraction &a) { int s, i, j; for (s = a.bottom; !(s%a.bottom == 0 && s%bottom == 0); s++) { }; i = s / a.bottom; j = s / bottom; return Fraction(a.top * i + top * j, s); } Fraction Fraction::operator-(const Fraction &a) { int s, i, j; for (s = a.bottom; !(s%a.bottom == 0 && s%bottom == 0); s++) { }; i = s / a.bottom; j = s / bottom;//通分 return Fraction(top * j - a.top * i, s); } Fraction Fraction::operator*(const Fraction &a) { return Fraction(top*a.top,bottom*a.bottom); } Fraction Fraction::operator/(const Fraction &a) { return Fraction(top*a.bottom, bottom*a.top); } int Fraction::ptop(){ if (bottom < 0) return -top; else return top; } int Fraction::pbottom() { return fabs(bottom); }
iFraction.h
#pragma once class iFraction :public Fraction { private: int i; Fraction ip; public: iFraction(const Fraction &xp); iFraction(const iFraction &xip); void showI(); friend void convertF(iFraction &x); };
iFraction.cpp
#include<iostream> #include"Fraction.h" #include"iFraction.h" using namespace std; /*----------------iFraction-------------------*/ iFraction::iFraction(const Fraction &xp) :i(0), ip(xp) {} iFraction::iFraction(const iFraction &xip) : ip(xip) {} void iFraction::showI() { if (ip.ptop() < 0) cout << "-"; if (i != 0) cout << i << " "; ip.showF(); } void convertF(iFraction &x) { while (x.ip.ptop()>x.ip.pbottom()) { x.i++; x.ip = Fraction(x.ip.ptop() - x.ip.pbottom(), x.ip.pbottom()); } }
main.cpp
#include<iostream> #include"Fraction.h" #include"iFraction.h" using namespace std; int main() { Fraction a(8, 3); Fraction b(4, 5); cout << "a="; a.showF(); cout << "b="; b.showF(); Fraction e; e = a + b; cout << "a+b="; e.showF(); cout << "a-b="; e = a - b; e.showF(); cout << "a*b="; e = a * b; e.showF(); cout << "a/b="; e = a / b; e.showF(); iFraction c(a); c.showI(); convertF(c); c.showI(); return 0; }
ex4类
emmm,这个游戏的数据设置的还是不太平衡,法师弄得有点强了。而且发现好像怪物升级比较快,用的战士弓箭手都没有通关。
container.h
//======================= // container.h //======================= // The so-called inventory of a player in RPG games // contains two items, heal and magic water #ifndef _CONTAINER // Conditional compilation #define _CONTAINER class container // Inventory { protected: int numOfHeal; // number of heal int numOfMW; // number of magic water public: container(); // constuctor void set(int heal_n, int mw_n); // set the items numbers int nOfHeal(); // get the number of heal int nOfMW(); // get the number of magic water void display(); // display the items; bool useHeal(); // use heal bool useMW(); // use magic water }; #endif
container.cpp
//======================= // container.cpp //======================= #include<iostream> #include"container.h" using namespace std; // default constructor initialise the inventory as empty container::container() { set(0,0); } // set the item numbers void container::set(int heal_n, int mw_n) { numOfHeal=heal_n; numOfMW=mw_n; } // get the number of heal int container::nOfHeal() { return numOfHeal; } // get the number of magic water int container::nOfMW() { return numOfMW; } // display the items; void container::display() { cout<<"Your bag contains: "<<endl; cout<<"Heal(HP+100): "<<numOfHeal<<endl; cout<<"Magic Water (MP+80): "<<numOfMW<<endl; } //use heal bool container::useHeal() { numOfHeal--; return 1; // use heal successfully } //use magic water bool container::useMW() { numOfMW--; return 1; // use magic water successfully }
player.h
//======================= // player.h //======================= // The base class of player // including the general properties and methods related to a character #ifndef _PLAYER #define _PLAYER #include<string> #include <iomanip> // use for setting field width #include <time.h> // use for generating random factor #include "container.h" enum job {sw, ar, mg}; /* define 3 jobs by enumerate type sword man, archer, mage */ class player { friend void showinfo(player &p1, player &p2); friend class swordsman; friend class archer; friend class mage; protected: int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; // General properties of all characters string name; // character name job role; /* character‘s job, one of swordman, archer and mage, as defined by the enumerate type */ container bag; // character‘s inventory public: virtual bool attack(player &p)=0; // normal attack virtual bool specialatt(player &p)=0; //special attack virtual void isLevelUp()=0; // level up judgement virtual void AI(player &p) = 0; /* Attention! These three methods are called "Pure virtual functions". They have only declaration, but no definition. The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. The detailed definition of these pure virtual functions will be given in subclasses. */ void reFill(); // character‘s HP and MP resume bool death(); // report whether character is dead void isDead(); // check whether character is dead bool useHeal(); // consume heal, irrelevant to job bool useMW(); // consume magic water, irrelevant to job void transfer(player &p); // possess opponent‘s items after victory void showRole(); // display character‘s job private: bool playerdeath; // whether character is dead, doesn‘t need to be accessed or inherited }; #endif
player,cpp
//======================= // player.cpp //======================= #include<iostream> #include<string> using namespace std; #include"player.h" // character‘s HP and MP resume void player::reFill() { HP=HPmax; // HP and MP fully recovered MP=MPmax; } // report whether character is dead bool player::death() { return playerdeath; } // check whether character is dead void player::isDead() { if(HP<=0) // HP less than 0, character is dead { cout<<name<<" is Dead." <<endl; system("pause"); playerdeath=1; // give the label of death value 1 } } // consume heal, irrelevant to job bool player::useHeal() { if(bag.nOfHeal()>0) { HP=HP+100; if(HP>HPmax) // HP cannot be larger than maximum value HP=HPmax; // so assign it to HPmax, if necessary cout<<name<<" used Heal, HP increased by 100."<<endl; bag.useHeal(); // use heal system("pause"); return 1; // usage of heal succeed } else // If no more heal in bag, cannot use { cout<<"Sorry, you don‘t have heal to use."<<endl; system("pause"); return 0; // usage of heal failed } } // consume magic water, irrelevant to job bool player::useMW() { if(bag.nOfMW()>0) { MP=MP+100; if(MP>MPmax) MP=MPmax; cout<<name<<" used Magic Water, MP increased by 100."<<endl; bag.useMW(); system("pause"); return 1; // usage of magic water succeed } else { cout<<"Sorry, you don‘t have magic water to use."<<endl; system("pause"); return 0; // usage of magic water failed } } // possess opponent‘s items after victory void player::transfer(player &p) { cout<<name<<" got "<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; system("pause"); bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW()); // set the character‘s bag, get opponent‘s items } // display character‘s job void player::showRole() { switch(role) { case sw: cout<<"Swordsman"; break; case ar: cout<<"Archer"; break; case mg: cout<<"Mage"; break; default: break; } } // display character‘s job void showinfo(player &p1, player &p2){ system("cls"); cout<<"##############################################################"<<endl; cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<‘/‘<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<‘/‘<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<‘/‘<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<‘/‘<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); p1.showRole(); cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); p2.showRole(); cout<<" #"<<endl; cout<<"--------------------------------------------------------------"<<endl; p1.bag.display(); cout<<"##############################################################"<<endl; }
swordmans.h
//======================= // swordsman.h //======================= // Derived from base class player // For the job Swordsman #include "player.h" class swordsman : public player // subclass swordsman publicly inherited from base player { public: swordsman(int lv_in=1, string name_in="Not Given"); // constructor with default level of 1 and name of "Not given" void isLevelUp(); bool attack (player &p); bool specialatt(player &p); /* These three are derived from the pure virtual functions of base class The definition of them will be given in this subclass. */ void AI(player &p); // Computer opponent };
swordmans.cpp
//======================= // swordsman.cpp //======================= #include<iostream> using namespace std; #include"swordsman.h" // constructor. default values don‘t need to be repeated here swordsman::swordsman(int lv_in, string name_in) { role=sw; // enumerate type of job LV=lv_in; name=name_in; // Initialising the character‘s properties, based on his level HPmax=150+8*(LV-1); // HP increases 8 point2 per level HP=HPmax; MPmax=75+2*(LV-1); // MP increases 2 points per level MP=MPmax; AP=25+4*(LV-1); // AP increases 4 points per level DP=25+4*(LV-1); // DP increases 4 points per level speed=25+2*(LV-1); // speed increases 2 points per level playerdeath=0; EXP=LV*LV*75; bag.set(lv_in, lv_in); } void swordsman::isLevelUp() { if(EXP>=LV*LV*75) { LV++; AP+=4; DP+=4; HPmax+=8; MPmax+=2; speed+=2; cout<<name<<" Level UP!"<<endl; cout<<"HP improved 8 points to "<<HPmax<<endl; cout<<"MP improved 2 points to "<<MPmax<<endl; cout<<"Speed improved 2 points to "<<speed<<endl; cout<<"AP improved 4 points to "<<AP<<endl; cout<<"DP improved 5 points to "<<DP<<endl; system("pause"); isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp } } bool swordsman::attack(player &p) { double HPtemp=0; // opponent‘s HP decrement double EXPtemp=0; // player obtained exp double hit=1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 { HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent‘s HP decrement calculated based their AP/DP, and uncertain chance cout<<name<<"‘s quick strike hit "<<p.name<<", "<<p.name<<"‘s HP decreased "<<HPtemp<<endl; p.HP=int(p.HP-HPtemp); EXPtemp=(int)(HPtemp*1.2); } // If speed smaller than opponent, the opponent has possibility to evade if ((speed<p.speed) && (rand()%50<1)) { cout<<name<<"‘s attack has been evaded by "<<p.name<<endl; system("pause"); return 1; } // 10% chance give critical attack if (rand()%100<=10) { hit=1.5; cout<<"Critical attack: "; } // Normal attack HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); cout<<name<<" uses bash, "<<p.name<<"‘s HP decreases "<<HPtemp<<endl; EXPtemp=(int)(EXPtemp+HPtemp*1.2); p.HP=(int)(p.HP-HPtemp); cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; EXP=(int)(EXP+EXPtemp); system("pause"); return 1; // Attack success } bool swordsman::specialatt(player &p) { if(MP<40) { cout<<"You don‘t have enough magic points!"<<endl; system("pause"); return 0; // Attack failed } else { MP-=40; // consume 40 MP to do special attack //10% chance opponent evades if(rand()%100<=10) { cout<<name<<"‘s leap attack has been evaded by "<<p.name<<endl; system("pause"); return 1; } double HPtemp=0; double EXPtemp=0; //double hit=1; //srand(time(NULL)); HPtemp=(int)(AP*1.2+20); // not related to opponent‘s DP EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience cout<<name<<" uses leap attack, "<<p.name<<"‘s HP decreases "<<HPtemp<<endl; cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; p.HP=(int)(p.HP-HPtemp); EXP=(int)(EXP+EXPtemp); system("pause"); } return 1; // special attack succeed } // Computer opponent void swordsman::AI(player &p) { 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))) // AI‘s HP cannot sustain 3 rounds && not too lavish && still has heal && won‘t be killed in next round { useHeal(); } else { if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) // AI has enough MP, it has 30% to make special attack { specialatt(p); p.isDead(); // check whether player is dead } else { if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) // Not enough MP && HP is safe && still has magic water { useMW(); } else { attack(p); // normal attack p.isDead(); } } } }
archer.h
#pragma once #include "player.h" class archer : public player // subclass swordsman publicly inherited from base player { public: archer(int lv_in = 1, string name_in = "Not Given"); // constructor with default level of 1 and name of "Not given" void isLevelUp(); bool attack(player &p); bool specialatt(player &p); /* These three are derived from the pure virtual functions of base class The definition of them will be given in this subclass. */ void AI(player &p); // Computer opponent };
archer.cpp
//======================= // swordsman.cpp //======================= #include<iostream> using namespace std; #include"archer.h" // constructor. default values don‘t need to be repeated here archer::archer(int lv_in, string name_in) { role = ar; // enumerate type of job LV = lv_in; name = name_in; // Initialising the character‘s properties, based on his level HPmax = 120 + 6 * (LV - 1); // HP increases 8 point2 per level HP = HPmax; MPmax = 90 + 2 * (LV - 1); // MP increases 2 points per level MP = MPmax; AP = 25 + 5 * (LV - 1); // AP increases 4 points per level DP = 25 + 2 * (LV - 1); // DP increases 4 points per level speed = 25 + 4 * (LV - 1); // speed increases 2 points per level playerdeath = 0; EXP = LV * LV * 75; bag.set(lv_in, lv_in); } void archer::isLevelUp() { if (EXP >= LV * LV * 75) { LV++; AP += 5; DP += 2; HPmax += 6; MPmax += 2; speed += 4; cout << name << " Level UP!" << endl; cout << "HP improved 8 points to " << HPmax << endl; cout << "MP improved 2 points to " << MPmax << endl; cout << "Speed improved 2 points to " << speed << endl; cout << "AP improved 4 points to " << AP << endl; cout << "DP improved 5 points to " << DP << endl; system("pause"); isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp } } bool archer::attack(player &p) { double HPtemp = 0; // opponent‘s HP decrement double EXPtemp = 0; // player obtained exp double hit = 1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand() % 100<(speed - p.speed))) // rand()%100 means generates a number no greater than 100 { HPtemp = (int)((1.2*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); // opponent‘s HP decrement calculated based their AP/DP, and uncertain chance cout << name << "‘s quick strike hit " << p.name << ", " << p.name << "‘s HP decreased " << HPtemp << endl; p.HP = int(p.HP - HPtemp); EXPtemp = (int)(HPtemp*1.2); } // If speed smaller than opponent, the opponent has possibility to evade if ((speed<p.speed) && (rand() % 50<1)) { cout << name << "‘s attack has been evaded by " << p.name << endl; system("pause"); return 1; } // 10% chance give critical attack if (rand() % 100 <= 10) { hit = 1.5; cout << "Critical attack: "; } // Normal attack HPtemp = (int)((1.2*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); cout << name << " uses bash, " << p.name << "‘s HP decreases " << HPtemp << endl; EXPtemp = (int)(EXPtemp + HPtemp * 1.2); p.HP = (int)(p.HP - HPtemp); cout << name << " obtained " << EXPtemp << " experience." << endl; EXP = (int)(EXP + EXPtemp); system("pause"); return 1; // Attack success } bool archer::specialatt(player &p) { if (MP<40) { cout << "You don‘t have enough magic points!" << endl; system("pause"); return 0; // Attack failed } else { MP -= 40; // consume 40 MP to do special attack //10% chance opponent evades if (rand() % 100 <= 10) { cout << name << "‘s leap attack has been evaded by " << p.name << endl; system("pause"); return 1; } double HPtemp = 0; double EXPtemp = 0; //double hit=1; //srand(time(NULL)); HPtemp = (int)(AP*1.2 + 20); // not related to opponent‘s DP EXPtemp = (int)(HPtemp*1.5); // special attack provides more experience cout << name << " uses leap attack, " << p.name << "‘s HP decreases " << HPtemp << endl; cout << name << " obtained " << EXPtemp << " experience." << endl; p.HP = (int)(p.HP - HPtemp); EXP = (int)(EXP + EXPtemp); system("pause"); } return 1; // special attack succeed } // Computer opponent void archer::AI(player &p) { if ((HP<(int)((1.2*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))) // AI‘s HP cannot sustain 3 rounds && not too lavish && still has heal && won‘t be killed in next round { useHeal(); } else { if (MP >= 40 && HP>0.5*HPmax && rand() % 100 <= 30) // AI has enough MP, it has 30% to make special attack { specialatt(p); p.isDead(); // check whether player is dead } else { if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) // Not enough MP && HP is safe && still has magic water { useMW(); } else { attack(p); // normal attack p.isDead(); } } } }
mage.h
#pragma once #pragma once #include "player.h" class mage : public player // subclass swordsman publicly inherited from base player { public: mage(int lv_in = 1, string name_in = "Not Given"); // constructor with default level of 1 and name of "Not given" void isLevelUp(); bool attack(player &p); bool specialatt(player &p); /* These three are derived from the pure virtual functions of base class The definition of them will be given in this subclass. */ void AI(player &p); // Computer opponent };
mage.cpp
//======================= // swordsman.cpp //======================= #include<iostream> using namespace std; #include"mage.h" // constructor. default values don‘t need to be repeated here mage::mage(int lv_in, string name_in) { role = mg; // enumerate type of job LV = lv_in; name = name_in; // Initialising the character‘s properties, based on his level HPmax = 100 + 4 * (LV - 1); // HP increases 8 point2 per level HP = HPmax; MPmax = 120 + 6 * (LV - 1); // MP increases 2 points per level MP = MPmax; AP = 25 + 6 * (LV - 1); // AP increases 4 points per level DP = 25 + 3 * (LV - 1); // DP increases 4 points per level speed = 25 + 1 * (LV - 1); // speed increases 2 points per level playerdeath = 0; EXP = LV * LV * 75; bag.set(lv_in, lv_in); } void mage::isLevelUp() { if (EXP >= LV * LV * 75) { LV++; AP += 4; DP += 3; HPmax += 6; MPmax += 4; speed += 1; cout << name << " Level UP!" << endl; cout << "HP improved 4 points to " << HPmax << endl; cout << "MP improved 6 points to " << MPmax << endl; cout << "Speed improved 1 points to " << speed << endl; cout << "AP improved 6 points to " << AP << endl; cout << "DP improved 3 points to " << DP << endl; system("pause"); isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp } } bool mage::attack(player &p) { double HPtemp = 0; // opponent‘s HP decrement double EXPtemp = 0; // player obtained exp double hit = 1; // attach factor, probably give critical attack srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack if ((speed>p.speed) && (rand() % 100<(speed - p.speed))) // rand()%100 means generates a number no greater than 100 { HPtemp = (int)((0.8*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); // opponent‘s HP decrement calculated based their AP/DP, and uncertain chance cout << name << "‘s quick strike hit " << p.name << ", " << p.name << "‘s HP decreased " << HPtemp << endl; p.HP = int(p.HP - HPtemp); EXPtemp = (int)(HPtemp*1.2); } // If speed smaller than opponent, the opponent has possibility to evade if ((speed<p.speed) && (rand() % 50<1)) { cout << name << "‘s attack has been evaded by " << p.name << endl; system("pause"); return 1; } // 10% chance give critical attack if (rand() % 100 <= 10) { hit = 1.5; cout << "Critical attack: "; } // Normal attack HPtemp = (int)((0.8*AP / p.DP)*AP * 5 / (rand() % 4 + 10)); cout << name << " uses bash, " << p.name << "‘s HP decreases " << HPtemp << endl; EXPtemp = (int)(EXPtemp + HPtemp * 1.2); p.HP = (int)(p.HP - HPtemp); cout << name << " obtained " << EXPtemp << " experience." << endl; EXP = (int)(EXP + EXPtemp); system("pause"); return 1; // Attack success } bool mage::specialatt(player &p) { if (MP<40) { cout << "You don‘t have enough magic points!" << endl; system("pause"); return 0; // Attack failed } else { MP -= 40; // consume 40 MP to do special attack //10% chance opponent evades if (rand() % 100 <= 10) { cout << name << "‘s leap attack has been evaded by " << p.name << endl; system("pause"); return 1; } double HPtemp = 0; double EXPtemp = 0; //double hit=1; //srand(time(NULL)); HPtemp = (int)(AP*3 + 20); // not related to opponent‘s DP EXPtemp = (int)(HPtemp*1.5); // special attack provides more experience cout << name << " uses leap attack, " << p.name << "‘s HP decreases " << HPtemp << endl; cout << name << " obtained " << EXPtemp << " experience." << endl; p.HP = (int)(p.HP - HPtemp); EXP = (int)(EXP + EXPtemp); system("pause"); } return 1; // special attack succeed } // Computer opponent void mage::AI(player &p) { if ((HP<(int)((0.8*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))) // AI‘s HP cannot sustain 3 rounds && not too lavish && still has heal && won‘t be killed in next round { useHeal(); } else { if (MP >= 40 && HP>0.5*HPmax && rand() % 100 <= 30) // AI has enough MP, it has 30% to make special attack { specialatt(p); p.isDead(); // check whether player is dead } else { if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) // Not enough MP && HP is safe && still has magic water { useMW(); } else { attack(p); // normal attack p.isDead(); } } } }
main.cpp
//======================= // main.cpp //======================= // main function for the RPG style game #include <iostream> #include <string> #include <stdlib.h> using namespace std; #include"container.h" #include"player.h" #include"swordsman.h" #include"archer.h" #include"mage.h" int main() { string tempName; bool success=0; //flag for storing whether operation is successful cout <<"Please input player‘s name: "; cin >>tempName; // get player‘s name from keyboard input player *human = (player *)malloc(sizeof(player)); // use pointer of base class, convenience for polymorphism player *enemy = (player *)malloc(sizeof(player)); int tempJob; // temp choice for job selection do { cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; cin>>tempJob; system("cls"); // clear the screen switch(tempJob) { case 1: human = new swordsman(1,tempName); // create the character with user inputted name and job success = 1; // operation succeed break; case 2: human = new archer(1, tempName); success = 1; break; case 3: human = new mage(1, tempName); success = 1; break; default: break; // In this case, success=0, character creation failed } }while(success!=1); // so the loop will ask user to re-create a character int tempCom; // temp command inputted by user int nOpp=0; // the Nth opponent for(int i=1;nOpp<5;i+=2) // i is opponent‘s level { nOpp++; system("cls"); cout<<"STAGE" <<nOpp<<endl; system("pause"); switch (rand() % 3 + 1) { case 1: enemy = new swordsman(i, "Warrior"); cout << "Your opponent, a Level " << i << " Swordsman." << endl; break; case 2: enemy = new archer(i, "Warrior"); cout << "Your opponent, a Level " << i << " Archer." << endl; break; case 3: enemy = new mage(i, "Warrior"); cout << "Your opponent, a Level " << i << " Mage." << endl; break; } // Initialise an opponent, level i, name "Junior" human->reFill(); // get HP/MP refill before start fight while(!human->death() && ! enemy->death()) // no died { success=0; while (success!=1) { showinfo(*human,*enemy); // show fighter‘s information cout<<"Please give command: "<<endl; cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl; cin>>tempCom; switch(tempCom) { case 0: cout<<"Are you sure to exit? Y/N"<<endl; char temp; cin>>temp; if(temp==‘Y‘||temp==‘y‘) return 0; else break; case 1: success=human->attack(*enemy); human->isLevelUp(); enemy->isDead(); break; case 2: success=human->specialatt(*enemy); human->isLevelUp(); enemy->isDead(); break; case 3: success=human->useHeal(); break; case 4: success=human->useMW(); break; default: break; } } if (!enemy->death()) // If AI still alive enemy->AI(*human); else // AI died { cout<<"YOU WIN"<<endl; human->transfer(*enemy); // player got all AI‘s items } if (human->death()) { system("cls"); cout<<endl<<setw(50)<<"GAME OVER"<<endl; delete human; // player is dead, program is getting to its end, what should we do here? system("pause"); return 0; } } } delete human; // You win, program is getting to its end, what should we do here? system("cls"); cout<<"Congratulations! You defeated all opponents!!"<<endl; system("pause"); return 0; }
以上是关于实验六的主要内容,如果未能解决你的问题,请参考以下文章