执行出错
//=======================
// main.cpp
//=======================
// main function for the RPG style game
#include <iostream>
#include <string>
using namespace std;
#ifndef ITEM_BASE
#define ITEM_BASE
#endif
#include "player.h"
#include "swordsman.h"
#include "archer.h"
#include "container.h"
#include"Mage.h"
//=======================
// container.cpp
//=======================
// 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<<"你的背包里有: "<<endl;
cout<<"生命恢复药剂+100: "<<numOfHeal<<endl;
cout<<"魔法恢复药剂+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.cpp
//=======================
// 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<<" 使用生命恢复药剂,生命恢复 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<<" 使用魔法恢复药剂,魔法值恢复到 100."<<endl;
bag.useMW();
system("pause");
return 1; // usage of magic water succeed
}
else
{
cout<<"对不起,您没有足够的魔法恢复药剂可以使用."<<endl;
system("pause");
return 0; // usage of magic water failed
}
}
// possess opponent's items after victory
void player::transfer(player &p)
{
cout<<name<<" 得到"<<p.bag.nOfHeal()<<" 生命恢复药剂, and "<<p.bag.nOfMW()<<"魔法恢复药剂."<<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<<"武士";
break;
case ar:
cout<<"弓箭手";
break;
case mg:
cout<<"法师";
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;
}
//=======================
// swordsman.cpp
//=======================
// 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<<"升级了!"<<endl;
cout<<"HP 提高 8 points to "<<HPmax<<endl;
cout<<"MP improved 2 points to "<<MPmax<<endl;
cout<<"速度 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<<"进行攻击, "<<p.name<<"'s HP减少 "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" 获得了 "<<EXPtemp<<"经验值."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return 1; // Attack success
}
bool swordsman::specialatt(player &p)
{
if(MP<40)
{
cout<<"你没有足够的魔法值使用技能!"<<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();
}
}
}
}
int main()
{
srand((unsigned)time(NULL));
string tempName;
int j;
bool success=0; //flag for storing whether operation is successful
cout <<"请输入玩家的姓名: ";
cin >>tempName; // 从键盘输入人物的姓名
player *human; // 使用基类的指针,方便多态性
int tempJob; // temp choice for job selection临时工作的选择
do
{
cout <<"请选择您的角色: 1 武士, 2 弓箭手, 3法师"<<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); // 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:
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;
j=rand()%3; // the Nth opponent
for(int i=1;nOpp<5;i+=2) // i is opponent's level
{
nOpp++;
system("cls");
switch (j)
{
case 0:
{
cout<<"STAGE" <<nOpp<<endl;
cout<<"你的对手, a Level "<<i<<" 武士."<<endl;
system("pause");
swordsman enemy(i, "Warrior"); // 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攻击; 2 特殊攻击; 3 使用生命恢复药剂; 4使用魔法恢复药剂r; 0 退出游戏"<<endl;
cin>>tempCom;
switch(tempCom)
{
case 0:
cout<<"你确定退出吗? 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;
}
}
break;
}//case
case 1:
{
cout<<"STAGE" <<nOpp<<endl;
cout<<"你的对手是一个 "<<i<<"弓箭手"<<endl;
system("pause");
archer enemy(i, "Warrior"); // 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 攻击; 2 技能; 3 使用红药水; 4 使用魔法药水; 0 退出游戏"<<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;
//6_??????????? // player is dead, program is getting to its end, what should we do here?
delete human;
system("pause");
return 0;
}
}
break;
}
case 2:
{
cout<<"STAGE" <<nOpp<<endl;
cout<<"你的对手是一个 "<<i<<" 级的法师"<<endl;
system("pause");
Mage enemy(i, "Warrior"); // 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 攻击; 2 技能; 3 使用红药水; 4 使用魔法药水; 0 退出游戏"<<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;
//6_??????????? // player is dead, program is getting to its end, what should we do here?
delete human;
system("pause");
return 0;
}
}
break;
}
}//switch
}//for
system("cls");
cout<<"恭喜通关!"<<endl;
system("pause");
return 0;
}