mirror of https://github.com/string1995/eee102.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.8 KiB
153 lines
3.8 KiB
//======================= |
|
// main.cpp |
|
//======================= |
|
|
|
// main function for the RPG style game |
|
|
|
#include <iostream> |
|
#include <string> |
|
//>>>>>>> |
|
#include <cstdlib> |
|
#include <cmath> |
|
//<<<<<<<< |
|
using namespace std; |
|
|
|
#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; // 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 |
|
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); // 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; // the Nth opponent |
|
for(int i=1;nOpp<5;i+=2) // i is opponent's level |
|
{ |
|
nOpp++; |
|
system("cls"); |
|
cout<<"STAGE" <<nOpp<<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"); |
|
//>>>>>>>>>> |
|
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"); |
|
//<<<<<<<<<<< |
|
|
|
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;//6_??????????? // player is dead, program is getting to its end, what should we do here? |
|
system("pause"); |
|
return 0; |
|
} |
|
} |
|
//>>>>>>>>>>>>> |
|
delete enemy; |
|
//<<<<<<<<<<<<<<< |
|
} |
|
delete human;//7_????????? // 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; |
|
} |
|
|
|
|
|
|