find change

master
IoTcat 5 years ago
parent 7f5eeb3e4d
commit 66ea618789
  1. 9
      as2/ex1/Fraction.h
  2. 6
      as2/ex1/ex1.cpp
  3. 283
      as2/ex2/archer.cpp
  4. 25
      as2/ex2/archer.h
  5. 4
      as2/ex2/container.cpp
  6. 4
      as2/ex2/container.h
  7. 283
      as2/ex2/mage.cpp
  8. 24
      as2/ex2/mage.h
  9. 72
      as2/ex2/main.cpp
  10. 13
      as2/ex2/player.cpp
  11. 14
      as2/ex2/player.h
  12. 185
      as2/ex2/swordsman.cpp
  13. 6
      as2/ex2/swordsman.h

@ -693,6 +693,15 @@ class iFraction: public Fraction {
return o; return o;
}; };
/* 提取整数 */
inline int integer() const{
return _get_integer();
};
/* 提取分子 */
inline int itop() const{
return (int)_get_iTop();
};
private: private:
/* 获取带分数整数部分 */ /* 获取带分数整数部分 */

@ -15,16 +15,16 @@ int main()
Fraction q(-3.2); Fraction q(-3.2);
iFraction p(q); iFraction p(-3.2);
p += q + p; //p += q + p;
//q = p; //q = p;
iFraction o; iFraction o;
cout << convertF(convertF(q)); cout << p.integer();
/* /*

@ -1,155 +1,174 @@
//======================= //=======================
// swordsman.cpp // archer.cpp
//======================= //=======================
#include "archer.h"
#include <iomanip> // use for setting field width
#include <ctime> // use for generating random factor
#include <stdlib.h>
using namespace std;
// constructor. default values don't need to be repeated here // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in) archer::archer(int lv_in, string name_in)
{ {
role=sw; // enumerate type of job role=ar; // enumerate type of job
LV=lv_in; LV=lv_in;
name=name_in; name=name_in;
// Initialising the character's properties, based on his level // Initialising the character's properties, based on his level
HPmax=150+8*(LV-1); // HP increases 8 point2 per level HPmax=150+8*(LV-1); // HP increases 8 point2 per level
HP=HPmax; HP=HPmax;
MPmax=75+2*(LV-1); // MP increases 2 points per level MPmax=75+2*(LV-1); // MP increases 2 points per level
MP=MPmax; MP=MPmax;
AP=25+4*(LV-1); // AP increases 4 points per level AP=25+4*(LV-1); // AP increases 4 points per level
DP=25+4*(LV-1); // DP 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 speed=25+2*(LV-1); // speed increases 2 points per level
playerdeath=0; playerdeath=0;
EXP=LV*LV*75; EXP=LV*LV*75;
bag.set(lv_in, lv_in); bag.set(lv_in*(rand()%2), lv_in*(rand()%2));
} }
void swordsman::isLevelUp() void archer::isLevelUp()
{ {
if(EXP>=LV*LV*75) if(EXP>=LV*LV*75)
{ {
LV++; LV++;
AP+=4; AP+=4;
DP+=4; DP+=4;
HPmax+=8; HPmax+=8;
MPmax+=2; MPmax+=2;
speed+=2; speed+=2;
cout<<name<<" Level UP!"<<endl; bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV);
cout<<"HP improved 8 points to "<<HPmax<<endl; cout<<name<<" Level UP!"<<endl;
cout<<"MP improved 2 points to "<<MPmax<<endl; cout<<"Get " << LV << " Heal and " << LV << " Magic Water!"<<endl;
cout<<"Speed improved 2 points to "<<speed<<endl; cout<<"HP improved 8 points to "<<HPmax<<endl;
cout<<"AP improved 4 points to "<<AP<<endl; cout<<"MP improved 2 points to "<<MPmax<<endl;
cout<<"DP improved 5 points to "<<DP<<endl; cout<<"Speed improved 2 points to "<<speed<<endl;
system("pause"); cout<<"AP improved 4 points to "<<AP<<endl;
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 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) bool archer::attack(player &p)
{ {
double HPtemp=0; // opponent's HP decrement double HPtemp=0; // opponent's HP decrement
double EXPtemp=0; // player obtained exp double EXPtemp=0; // player obtained exp
double hit=1; // attach factor, probably give critical attack double hit=1; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time srand((unsigned)time(NULL)); // generating random seed based on system time
int attack = (speed*1.8)/p.DP; //depand on player's speed and p.DP
// 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)((attack)*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
else if ((speed<p.speed) && (rand()%50<1))
{
cout<<name<<"'s attack has been evaded by "<<p.name<<endl;
system("pause");
return 1;
}
// If speed greater than opponent, you have some possibility to do double attack // 10% chance give critical attack
if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 else
{ {
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance if(rand()%100<=10) {
cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl; hit=1.5;
p.HP=int(p.HP-HPtemp); cout<<"Critical attack: ";
EXPtemp=(int)(HPtemp*1.2); }
} // Normal attack
//<!-- change
HPtemp=(int)(((attack)*AP*5/(rand()%4+10)) * hit);
//-->
cout<<name<<" uses shoot, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
//<!-- luck
p.HP=(int)(p.HP-HPtemp*((double)(rand() % 100 + 50) / (double)100));
//-->
// 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 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); //<!-- luck
cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; EXP=(int)(EXP+EXPtemp*((double)(rand() % 100 + 50) / (double)100));
EXPtemp=(int)(EXPtemp+HPtemp*1.2); //-->
p.HP=(int)(p.HP-HPtemp); system("pause");
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; return 1; // Attack success
EXP=(int)(EXP+EXPtemp);
system("pause");
return 1; // Attack success
} }
bool swordsman::specialatt(player &p) bool archer::specialatt(player &p)
{ {
if(MP<40) if(MP<40)
{ {
cout<<"You don't have enough magic points!"<<endl; cout<<"You don't have enough magic points!"<<endl;
system("pause"); system("pause");
return 0; // Attack failed return 0; // Attack failed
} }
else else
{ {
MP-=40; // consume 40 MP to do special attack MP-=40; // consume 40 MP to do special attack
//10% chance opponent evades //10% chance opponent evades
if(rand()%100<=10) if(rand()%100<=10)
{ {
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; cout<<name<<"'s shoooooooooooot attack has been evaded by "<<p.name<<endl;
system("pause"); system("pause");
return 1; return 1;
} }
double HPtemp=0; double HPtemp=0;
double EXPtemp=0; double EXPtemp=0;
//double hit=1; //double hit=1;
//srand(time(NULL)); //srand(time(NULL));
HPtemp=(int)(AP*1.2+20); // not related to opponent's DP //<!-- // Luck
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience HPtemp=(int)(AP*1.2*((double)(rand() % 100 + 50) / (double)100)+20); // not related to opponent's DP
cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; //-->
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
p.HP=(int)(p.HP-HPtemp); cout<<name<<" uses shoooooooooooot attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXP=(int)(EXP+EXPtemp); cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
system("pause"); p.HP=(int)(p.HP-HPtemp);
} EXP=(int)(EXP+EXPtemp);
return 1; // special attack succeed system("pause");
}
return 1; // special attack succeed
} }
// Computer opponent // Computer opponent
void swordsman::AI(player &p) void archer::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))) 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 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{ {
useHeal(); useHeal();
} }
else else
{ {
if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) if(MP>=40 && HP>0.5*HPmax && rand()%100<=30)
// AI has enough MP, it has 30% to make special attack // AI has enough MP, it has 30% to make special attack
{ {
specialatt(p); specialatt(p);
p.isDead(); // check whether player is dead p.isDead(); // check whether player is dead
} }
else else
{ {
if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water // Not enough MP && HP is safe && still has magic water
{ {
useMW(); useMW();
} }
else else
{ {
attack(p); // normal attack attack(p); // normal attack
p.isDead(); p.isDead();
} }
} }
} }
} }

@ -1,20 +1,21 @@
//======================= //=======================
// swordsman.h // archer.h
//======================= //=======================
// Derived from base class player // Derived from base class player
// For the job Swordsman // For the job archer
#include <iostream>
#include "player.h" #include "player.h"
class swordsman : 5_????????? // subclass swordsman publicly inherited from base player class archer : public player // subclass archer publicly inherited from base player
{ {
public: public:
swordsman(int lv_in=1, string name_in="Not Given"); archer(int lv_in=1, std::string name_in="Not Given");
// constructor with default level of 1 and name of "Not given" // constructor with default level of 1 and name of "Not given"
void isLevelUp(); void isLevelUp();
bool attack (player &p); bool attack (player &p);
bool specialatt(player &p); bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class /* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */ The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent void AI(player &p); // Computer opponent
}; };

@ -1,7 +1,9 @@
//======================= //=======================
// container.cpp // container.cpp
//======================= //=======================
#include "container.h"
using namespace std;
// default constructor initialise the inventory as empty // default constructor initialise the inventory as empty
container::container() container::container()
{ {
@ -38,7 +40,7 @@ void container::display()
//use heal //use heal
bool container::useHeal() bool container::useHeal()
{ {
2_???????? numOfHeal--; //2_???????????
return 1; // use heal successfully return 1; // use heal successfully
} }

@ -5,9 +5,11 @@
// The so-called inventory of a player in RPG games // The so-called inventory of a player in RPG games
// contains two items, heal and magic water // contains two items, heal and magic water
1_????????????? // Conditional compilation #ifndef _CONTAINER //1_??????????? // Conditional compilation
#define _CONTAINER #define _CONTAINER
#include <iostream>
class container // Inventory class container // Inventory
{ {
protected: protected:

@ -1,155 +1,174 @@
//======================= //=======================
// swordsman.cpp // mage.cpp
//======================= //=======================
#include "mage.h"
#include <iomanip> // use for setting field width
#include <ctime> // use for generating random factor
#include <stdlib.h>
using namespace std;
// constructor. default values don't need to be repeated here // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in) mage::mage(int lv_in, string name_in)
{ {
role=sw; // enumerate type of job role=mg; // enumerate type of job
LV=lv_in; LV=lv_in;
name=name_in; name=name_in;
// Initialising the character's properties, based on his level // Initialising the character's properties, based on his level
HPmax=150+8*(LV-1); // HP increases 8 point2 per level HPmax=150+8*(LV-1); // HP increases 8 point2 per level
HP=HPmax; HP=HPmax;
MPmax=75+2*(LV-1); // MP increases 2 points per level MPmax=75+2*(LV-1); // MP increases 2 points per level
MP=MPmax; MP=MPmax;
AP=25+4*(LV-1); // AP increases 4 points per level AP=25+4*(LV-1); // AP increases 4 points per level
DP=25+4*(LV-1); // DP 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 speed=25+2*(LV-1); // speed increases 2 points per level
playerdeath=0; playerdeath=0;
EXP=LV*LV*75; EXP=LV*LV*75;
bag.set(lv_in, lv_in); bag.set(lv_in*(rand()%2), lv_in*(rand()%2));
} }
void swordsman::isLevelUp() void mage::isLevelUp()
{ {
if(EXP>=LV*LV*75) if(EXP>=LV*LV*75)
{ {
LV++; LV++;
AP+=4; AP+=4;
DP+=4; DP+=4;
HPmax+=8; HPmax+=8;
MPmax+=2; MPmax+=2;
speed+=2; speed+=2;
cout<<name<<" Level UP!"<<endl; bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV);
cout<<"HP improved 8 points to "<<HPmax<<endl; cout<<name<<" Level UP!"<<endl;
cout<<"MP improved 2 points to "<<MPmax<<endl; cout<<"Get " << LV << " Heal and " << LV << " Magic Water!"<<endl;
cout<<"Speed improved 2 points to "<<speed<<endl; cout<<"HP improved 8 points to "<<HPmax<<endl;
cout<<"AP improved 4 points to "<<AP<<endl; cout<<"MP improved 2 points to "<<MPmax<<endl;
cout<<"DP improved 5 points to "<<DP<<endl; cout<<"Speed improved 2 points to "<<speed<<endl;
system("pause"); cout<<"AP improved 4 points to "<<AP<<endl;
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 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) bool mage::attack(player &p)
{ {
double HPtemp=0; // opponent's HP decrement double HPtemp=0; // opponent's HP decrement
double EXPtemp=0; // player obtained exp double EXPtemp=0; // player obtained exp
double hit=1; // attach factor, probably give critical attack double hit=1; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time srand((unsigned)time(NULL)); // generating random seed based on system time
int attack = (EXP/75 +25)/p.DP;
// 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)((attack)*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
else if ((speed<p.speed) && (rand()%50<1))
{
cout<<name<<"'s attack has been evaded by "<<p.name<<endl;
system("pause");
return 1;
}
// If speed greater than opponent, you have some possibility to do double attack // 10% chance give critical attack
if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 else
{ {
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance if(rand()%100<=10) {
cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl; hit=1.5;
p.HP=int(p.HP-HPtemp); cout<<"Critical attack: ";
EXPtemp=(int)(HPtemp*1.2); }
} // Normal attack
//<!-- change
HPtemp=(int)(((attack)*AP*5/(rand()%4+10)) * hit);
//-->
cout<<name<<" uses fire ball, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
//<!-- luck
p.HP=(int)(p.HP-HPtemp*((double)(rand() % 100 + 50) / (double)100));
//-->
// 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 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); //<!-- luck
cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; EXP=(int)(EXP+EXPtemp*((double)(rand() % 100 + 50) / (double)100));
EXPtemp=(int)(EXPtemp+HPtemp*1.2); //-->
p.HP=(int)(p.HP-HPtemp); system("pause");
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; return 1; // Attack success
EXP=(int)(EXP+EXPtemp);
system("pause");
return 1; // Attack success
} }
bool swordsman::specialatt(player &p) bool mage::specialatt(player &p)
{ {
if(MP<40) if(MP<40)
{ {
cout<<"You don't have enough magic points!"<<endl; cout<<"You don't have enough magic points!"<<endl;
system("pause"); system("pause");
return 0; // Attack failed return 0; // Attack failed
} }
else else
{ {
MP-=40; // consume 40 MP to do special attack MP-=40; // consume 40 MP to do special attack
//10% chance opponent evades //10% chance opponent evades
if(rand()%100<=10) if(rand()%100<=10)
{ {
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; cout<<name<<"'s ffffffire balls attack has been evaded by "<<p.name<<endl;
system("pause"); system("pause");
return 1; return 1;
} }
double HPtemp=0; double HPtemp=0;
double EXPtemp=0; double EXPtemp=0;
//double hit=1; //double hit=1;
//srand(time(NULL)); //srand(time(NULL));
HPtemp=(int)(AP*1.2+20); // not related to opponent's DP //<!-- // Luck
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience HPtemp=(int)(AP*1.2*((double)(rand() % 100 + 50) / (double)100)+20); // not related to opponent's DP
cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; //-->
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
p.HP=(int)(p.HP-HPtemp); cout<<name<<" uses ffffffire balls attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXP=(int)(EXP+EXPtemp); cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
system("pause"); p.HP=(int)(p.HP-HPtemp);
} EXP=(int)(EXP+EXPtemp);
return 1; // special attack succeed system("pause");
}
return 1; // special attack succeed
} }
// Computer opponent // Computer opponent
void swordsman::AI(player &p) void mage::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))) 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 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{ {
useHeal(); useHeal();
} }
else else
{ {
if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) if(MP>=40 && HP>0.5*HPmax && rand()%100<=30)
// AI has enough MP, it has 30% to make special attack // AI has enough MP, it has 30% to make special attack
{ {
specialatt(p); specialatt(p);
p.isDead(); // check whether player is dead p.isDead(); // check whether player is dead
} }
else else
{ {
if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water // Not enough MP && HP is safe && still has magic water
{ {
useMW(); useMW();
} }
else else
{ {
attack(p); // normal attack attack(p); // normal attack
p.isDead(); p.isDead();
} }
} }
} }
} }

@ -1,20 +1,20 @@
//======================= //=======================
// swordsman.h // mage.h
//======================= //=======================
// Derived from base class player // Derived from base class player
// For the job Swordsman // For the job mage
#include <iostream>
#include "player.h" #include "player.h"
class swordsman : 5_????????? // subclass swordsman publicly inherited from base player class mage : public player // subclass mage publicly inherited from base player
{ {
public: public:
swordsman(int lv_in=1, string name_in="Not Given"); mage(int lv_in=1, std::string name_in="Not Given");
// constructor with default level of 1 and name of "Not given" // constructor with default level of 1 and name of "Not given"
void isLevelUp(); void isLevelUp();
bool attack (player &p); bool attack (player &p);
bool specialatt(player &p); bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class /* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */ The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent void AI(player &p); // Computer opponent
}; };

@ -6,11 +6,17 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
//>>>>>>>
#include <cstdlib>
#include <cmath>
//<<<<<<<<
using namespace std; using namespace std;
#include "swordsman.h" #include "swordsman.h"
//>>>>>>>>
#include "archer.h"
#include "mage.h"
//<<<<<<<<
int main() int main()
{ {
string tempName; string tempName;
@ -18,6 +24,10 @@ int main()
cout <<"Please input player's name: "; cout <<"Please input player's name: ";
cin >>tempName; // get player's name from keyboard input cin >>tempName; // get player's name from keyboard input
player *human; // use pointer of base class, convenience for polymorphism player *human; // use pointer of base class, convenience for polymorphism
//>>>>>>>>>>
player *enemy; // use pointer of base class, convenience for polymorphism
//<<<<<<<<<<
int tempJob; // temp choice for job selection int tempJob; // temp choice for job selection
do do
{ {
@ -30,6 +40,16 @@ int main()
human=new swordsman(1,tempName); // create the character with user inputted name and job human=new swordsman(1,tempName); // create the character with user inputted name and job
success=1; // operation succeed success=1; // operation succeed
break; break;
//>>>>>>>>>>>
case 2:
human=new archer(1,tempName); // create the character with user inputted name and job
success=1; // operation succeed
break;
case 3:
human=new mage(1,tempName); // create the character with user inputted name and job
success=1; // operation succeed
break;
//<<<<<<<<<<<<<
default: default:
break; // In this case, success=0, character creation failed break; // In this case, success=0, character creation failed
} }
@ -42,17 +62,30 @@ int main()
nOpp++; nOpp++;
system("cls"); system("cls");
cout<<"STAGE" <<nOpp<<endl; cout<<"STAGE" <<nOpp<<endl;
cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl; //>>>>>>>>>>>>>
srand((unsigned int)time(NULL));
short t_s = rand() % 3;
cout<<"Your opponent, a Level "<<i<<((t_s == 0)?" Swordsman.":"")<<((t_s == 1)?" Archer.":"")<<((t_s == 2)?" Mage.":"")<<endl;
//<<<<<<<<<<<<<
system("pause"); system("pause");
swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" //>>>>>>>>>>
human->reFill(); // get HP/MP refill before start fight if(t_s == 0) enemy = new swordsman(i, "Warrior");
if(t_s == 1) enemy = new archer(i, "Warrior");
if(t_s == 2) enemy = new mage(i, "Warrior");
//<<<<<<<<<<<
while(!human->death() && !enemy.death()) // no died human->reFill(); // get HP/MP refill before start fight
//>>>>>>>>>>>>
while(!human->death() && !enemy->death()) // no died
//<<<<<<<<<<<
{ {
success=0; success=0;
while (success!=1) while (success!=1)
{ {
showinfo(*human,enemy); // show fighter's information //>>>>>>>
showinfo(*human,*enemy); // show fighter's information
//<<<<<<<<<<
cout<<"Please give command: "<<endl; cout<<"Please give command: "<<endl;
cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl; cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
cin>>tempCom; cin>>tempCom;
@ -66,15 +99,17 @@ int main()
return 0; return 0;
else else
break; break;
//>>>>>>>>>>>>>>>>
case 1: case 1:
success=human->attack(enemy); success=human->attack(*enemy);
human->isLevelUp(); human->isLevelUp();
enemy.isDead(); enemy->isDead();
break; break;
case 2: case 2:
success=human->specialatt(enemy); success=human->specialatt(*enemy);
human->isLevelUp(); human->isLevelUp();
enemy.isDead(); enemy->isDead();
//<<<<<<<<<<<<<<<<
break; break;
case 3: case 3:
success=human->useHeal(); success=human->useHeal();
@ -86,24 +121,29 @@ int main()
break; break;
} }
} }
if(!enemy.death()) // If AI still alive //>>>>>>>>>>>>>>>>
enemy.AI(*human); if(!enemy->death()) // If AI still alive
enemy->AI(*human);
else // AI died else // AI died
//<<<<<<<<<<<<<<<
{ {
cout<<"YOU WIN"<<endl; cout<<"YOU WIN"<<endl;
human->transfer(enemy); // player got all AI's items human->transfer(*enemy); // player got all AI's items
} }
if (human->death()) if (human->death())
{ {
system("cls"); system("cls");
cout<<endl<<setw(50)<<"GAME OVER"<<endl; cout<<endl<<setw(50)<<"GAME OVER"<<endl;
6_??????????? // player is dead, program is getting to its end, what should we do here? delete human;//6_??????????? // player is dead, program is getting to its end, what should we do here?
system("pause"); system("pause");
return 0; return 0;
} }
} }
//>>>>>>>>>>>>>
delete enemy;
//<<<<<<<<<<<<<<<
} }
7_????????? // You win, program is getting to its end, what should we do here? delete human;//7_????????? // You win, program is getting to its end, what should we do here?
system("cls"); system("cls");
cout<<"Congratulations! You defeated all opponents!!"<<endl; cout<<"Congratulations! You defeated all opponents!!"<<endl;
system("pause"); system("pause");

@ -2,7 +2,9 @@
// player.cpp // player.cpp
//======================= //=======================
#include "player.h"
using namespace std;
// character's HP and MP resume // character's HP and MP resume
void player::reFill() void player::reFill()
{ {
@ -21,7 +23,7 @@ void player::isDead()
{ {
if(HP<=0) // HP less than 0, character is dead if(HP<=0) // HP less than 0, character is dead
{ {
cout<<name<<" is Dead." <<endl; cout<< name <<" is Dead." <<endl;
system("pause"); system("pause");
playerdeath=1; // give the label of death value 1 playerdeath=1; // give the label of death value 1
} }
@ -74,7 +76,8 @@ void player::transfer(player &p)
{ {
cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl;
system("pause"); system("pause");
3_???????????
this->bag.set(this->bag.nOfHeal() + p.bag.nOfHeal(), this->bag.nOfMW() + p.bag.nOfMW());// 3_????????????
// set the character's bag, get opponent's items // set the character's bag, get opponent's items
} }
@ -87,10 +90,10 @@ void player::showRole()
cout<<"Swordsman"; cout<<"Swordsman";
break; break;
case ar: case ar:
cout<<"Archer"; cout<<" Archer ";
break; break;
case mg: case mg:
cout<<"Mage"; cout<<" Mage ";
break; break;
default: default:
break; break;
@ -99,7 +102,7 @@ void player::showRole()
// display character's job // display character's job
4_?????????????? void showinfo(player &p1, player &p2) // 4_??????????????????
{ {
system("cls"); system("cls");
cout<<"##############################################################"<<endl; cout<<"##############################################################"<<endl;

@ -8,8 +8,10 @@
#ifndef _PLAYER #ifndef _PLAYER
#define _PLAYER #define _PLAYER
#include <iostream>
#include <iomanip> // use for setting field width #include <iomanip> // use for setting field width
#include <time.h> // use for generating random factor #include <time.h> // use for generating random factor
#include <string>
#include "container.h" #include "container.h"
enum job {sw, ar, mg}; /* define 3 jobs by enumerate type enum job {sw, ar, mg}; /* define 3 jobs by enumerate type
@ -17,12 +19,17 @@ enum job {sw, ar, mg}; /* define 3 jobs by enumerate type
class player class player
{ {
friend void showinfo(player &p1, player &p2); friend void showinfo(player &p1, player &p2);
//>>>>>>>>>>>>
friend class swordsman; friend class swordsman;
friend class archer;
friend class mage;
//<<<<<<<<<<<<<<<
protected: protected:
int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
// General properties of all characters // General properties of all characters
string name; // character name protected:
std::string name; // character name
job role; /* character's job, one of swordman, archer and mage, job role; /* character's job, one of swordman, archer and mage,
as defined by the enumerate type */ as defined by the enumerate type */
container bag; // character's inventory container bag; // character's inventory
@ -31,6 +38,9 @@ public:
virtual bool attack(player &p)=0; // normal attack virtual bool attack(player &p)=0; // normal attack
virtual bool specialatt(player &p)=0; //special attack virtual bool specialatt(player &p)=0; //special attack
virtual void isLevelUp()=0; // level up judgement virtual void isLevelUp()=0; // level up judgement
//<!--
virtual void AI(player &p)=0; // AI for robot
//-->
/* Attention! /* Attention!
These three methods are called "Pure virtual functions". These three methods are called "Pure virtual functions".
They have only declaration, but no definition. They have only declaration, but no definition.
@ -42,8 +52,8 @@ public:
void isDead(); // check whether character is dead void isDead(); // check whether character is dead
bool useHeal(); // consume heal, irrelevant to job bool useHeal(); // consume heal, irrelevant to job
bool useMW(); // consume magic water, 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 void showRole(); // display character's job
void transfer(player &p); // possess opponent's items after victory
private: private:
bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited

@ -1,7 +1,11 @@
//======================= //=======================
// swordsman.cpp // swordsman.cpp
//======================= //=======================
#include "swordsman.h"
#include <iomanip> // use for setting field width
#include <ctime> // use for generating random factor
#include <stdlib.h>
using namespace std;
// constructor. default values don't need to be repeated here // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in) swordsman::swordsman(int lv_in, string name_in)
{ {
@ -25,100 +29,115 @@ swordsman::swordsman(int lv_in, string name_in)
void swordsman::isLevelUp() void swordsman::isLevelUp()
{ {
if(EXP>=LV*LV*75) if(EXP>=LV*LV*75)
{ {
LV++; LV++;
AP+=4; AP+=4;
DP+=4; DP+=4;
HPmax+=8; HPmax+=8;
MPmax+=2; MPmax+=2;
speed+=2; speed+=2;
cout<<name<<" Level UP!"<<endl; bag.set(bag.nOfHeal()+LV, bag.nOfMW()+LV);
cout<<"HP improved 8 points to "<<HPmax<<endl; cout<<name<<" Level UP!"<<endl;
cout<<"MP improved 2 points to "<<MPmax<<endl; cout<<"Get " << LV << " Heal and " << LV << " Magic Water!"<<endl;
cout<<"Speed improved 2 points to "<<speed<<endl; cout<<"HP improved 8 points to "<<HPmax<<endl;
cout<<"AP improved 4 points to "<<AP<<endl; cout<<"MP improved 2 points to "<<MPmax<<endl;
cout<<"DP improved 5 points to "<<DP<<endl; cout<<"Speed improved 2 points to "<<speed<<endl;
system("pause"); cout<<"AP improved 4 points to "<<AP<<endl;
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 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) bool swordsman::attack(player &p)
{ {
double HPtemp=0; // opponent's HP decrement double HPtemp=0; // opponent's HP decrement
double EXPtemp=0; // player obtained exp double EXPtemp=0; // player obtained exp
double hit=1; // attach factor, probably give critical attack double hit=1; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time srand((unsigned)time(NULL)); // generating random seed based on system time
int attack = AP/p.DP; //depand on player's speed and p.DP
// If speed greater than opponent, you have some possibility to do double attack // 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 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 HPtemp=(int)((attack)*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; cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp); p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2); EXPtemp=(int)(HPtemp*1.2);
} }
// If speed smaller than opponent, the opponent has possibility to evade // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%50<1)) else if ((speed<p.speed) && (rand()%50<1))
{ {
cout<<name<<"'s attack has been evaded by "<<p.name<<endl; cout<<name<<"'s attack has been evaded by "<<p.name<<endl;
system("pause"); system("pause");
return 1; return 1;
} }
// 10% chance give critical attack // 10% chance give critical attack
if (rand()%100<=10) else
{ {
hit=1.5; if(rand()%100<=10) {
cout<<"Critical attack: "; hit=1.5;
} cout<<"Critical attack: ";
}
// Normal attack
//<!-- change
HPtemp=(int)(((attack)*AP*5/(rand()%4+10)) * hit);
//-->
cout<<name<<" uses chop, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
//<!-- luck
p.HP=(int)(p.HP-HPtemp*((double)(rand() % 100 + 50) / (double)100));
//-->
// 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); cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
p.HP=(int)(p.HP-HPtemp); //<!-- luck
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; EXP=(int)(EXP+EXPtemp*((double)(rand() % 100 + 50) / (double)100));
EXP=(int)(EXP+EXPtemp); //-->
system("pause"); system("pause");
return 1; // Attack success return 1; // Attack success
} }
bool swordsman::specialatt(player &p) bool swordsman::specialatt(player &p)
{ {
if(MP<40) if(MP<40)
{ {
cout<<"You don't have enough magic points!"<<endl; cout<<"You don't have enough magic points!"<<endl;
system("pause"); system("pause");
return 0; // Attack failed return 0; // Attack failed
} }
else else
{ {
MP-=40; // consume 40 MP to do special attack MP-=40; // consume 40 MP to do special attack
//10% chance opponent evades //10% chance opponent evades
if(rand()%100<=10) if(rand()%100<=10)
{ {
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; cout<<name<<"'s chooooooooooooooop attack has been evaded by "<<p.name<<endl;
system("pause"); system("pause");
return 1; return 1;
} }
double HPtemp=0; double HPtemp=0;
double EXPtemp=0; double EXPtemp=0;
//double hit=1; //double hit=1;
//srand(time(NULL)); //srand(time(NULL));
HPtemp=(int)(AP*1.2+20); // not related to opponent's DP //<!-- // Luck
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience HPtemp=(int)(AP*1.2*((double)(rand() % 100 + 50) / (double)100)+20); // not related to opponent's DP
cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; //-->
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
p.HP=(int)(p.HP-HPtemp); cout<<name<<" uses chooooooooooooooop attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXP=(int)(EXP+EXPtemp); cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
system("pause"); p.HP=(int)(p.HP-HPtemp);
} EXP=(int)(EXP+EXPtemp);
return 1; // special attack succeed system("pause");
}
return 1; // special attack succeed
} }
// Computer opponent // Computer opponent

@ -4,12 +4,12 @@
// Derived from base class player // Derived from base class player
// For the job Swordsman // For the job Swordsman
#include <iostream>
#include "player.h" #include "player.h"
class swordsman : 5_????????? // subclass swordsman publicly inherited from base player class swordsman : public player // subclass swordsman publicly inherited from base player
{ {
public: public:
swordsman(int lv_in=1, string name_in="Not Given"); swordsman(int lv_in=1, std::string name_in="Not Given");
// constructor with default level of 1 and name of "Not given" // constructor with default level of 1 and name of "Not given"
void isLevelUp(); void isLevelUp();
bool attack (player &p); bool attack (player &p);

Loading…
Cancel
Save