Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <math.h>
- #include <windows.h>
- #include "Player.h"
- #include <ctime>
- using namespace std;
- const string m_name[]={"Orc", "Goblin", "Dark Elf"};
- int fRandomise(int a, int b);
- void fDoTurn(Player *, Player *, int *);
- void getChoice(Player p);
- void fBattle(Player p, Player *player);
- void fShowStats(Player p);
- void fSaveGame(Player p);
- void fLoadGame(Player *);
- void fLevelUp(Player *p);
- void fHeal(Player *p);
- void fQuit();
- int main()
- {
- srand(time(NULL));
- string name;
- Player p(0, 0, 0, 0, 0, 0, 0, 0, name);
- bool start = true;
- do
- {
- char cChoiceNewGame;
- cout << "N - New Game" << endl;
- cout << "L - Load Game" << endl;
- cout << "Enter your choice here: ";
- start = false;
- do
- {
- cin >> cChoiceNewGame;
- if(cChoiceNewGame == 'N' || cChoiceNewGame == 'n')
- {
- system("cls");
- cout << "Enter your hero's name: ";
- cin >> name;
- Player p(20, 30, 5, 0, 1, 0, 500, 0, name);
- getChoice(p);
- }
- if(cChoiceNewGame == 'L' || cChoiceNewGame == 'l')
- {
- system("cls");
- fLoadGame(&p);
- getChoice(p);
- }
- }while(cChoiceNewGame != 'N' && cChoiceNewGame != 'n' && cChoiceNewGame != 'L' && cChoiceNewGame != 'l');
- }while (start != false);
- return 0;
- }
- void getChoice(Player p)
- {
- char cChoice;
- do
- {
- fLevelUp(&p);
- system("cls");
- cout << "B - Battle" << endl;
- cout << "H - Be healed" << endl;
- cout << "V - See Stats" << endl;
- cout << "S - Save the Game" << endl;
- cout << "K - Save and quit" << endl;
- cout << "Q - Quit the game" << endl;
- cout << "Enter your choice here: ";
- cin >> cChoice;
- if(cChoice == 'B' || cChoice == 'b')
- {
- fBattle(p, &p);
- }
- else if(cChoice == 'V' || cChoice == 'v')
- {
- fShowStats(p);
- }
- else if(cChoice == 'H' || cChoice == 'h')
- {
- fHeal(&p);
- }
- else if(cChoice == 'S' || cChoice == 's')
- {
- fSaveGame(p);
- }
- else if(cChoice == 'K' || cChoice == 'k')
- {
- fSaveGame(p);
- fQuit();
- }
- else if(cChoice == 'Q' || cChoice == 'q')
- {
- fQuit();
- }
- }while(cChoice != 'Q' && cChoice !='q' && cChoice != 'K' && cChoice !='k');
- }
- //Function fBattle
- void fBattle(Player p, Player *player)
- {
- system("cls");
- int player_hp = p.getHP();
- string monster_name = m_name[fRandomise(2,0)];
- int monster_level = fRandomise(p.getLV(), 1);
- int monster_str = 1+monster_level*1.5;
- int monster_hp = fRandomise(monster_level*50, monster_level*20);
- int monster_drop = fRandomise(monster_level*8, 1+monster_level*2);
- int monster_exp = fRandomise(monster_level*50, monster_level*10);
- Player m(monster_hp,monster_str,1,monster_level,30,monster_name);
- cout << monster_name << " of level " << m.getLV() << " has appeared!" << endl << "It has " << m.getHP() << "HP!" << endl << endl;
- Sleep(2000);
- while (monster_hp >0 && player_hp >0)
- {
- if(player_hp <= 10)
- {
- cout << "Your HP is low. Do you wish to attempt to flee?\nThe rate of success is 50% (Y/N)" << endl;
- char cChoice;
- do
- {
- cin >> cChoice;
- if(cChoice == 'y' || cChoice == 'Y')
- {
- int nFlee = fRandomise(100, 0);
- if (nFlee >= 50)
- {
- cout << "You fleed!" << endl << endl;
- Sleep(2000);
- return;
- }
- else
- {
- cout << "You failed!" << endl << endl;
- }
- }
- else if(cChoice == 'n' || cChoice == 'N')
- {
- }
- }while(cChoice != 'n' && cChoice != 'N' && cChoice != 'y' && cChoice != 'Y');
- }
- fDoTurn(&p, &m, &monster_hp);
- Sleep(500);
- if(monster_hp <=0)
- {
- cout << endl;
- break;
- }
- fDoTurn(&m, &p, &player_hp);
- Sleep(500);
- cout << endl;
- }
- if(monster_hp <= 0)
- {
- cout << p.getNAME() << " has slain " << m.getNAME() << endl;
- cout << "You have gained" << monster_exp << " exp and " << monster_drop << "gold" << endl << endl;
- player->setEXP(player->getEXP()+50);
- player->setGOLD(player->getGOLD()+20);
- player->setHP(player_hp);
- player->setSLAIN(player->getSLAIN()+1);
- }
- if(player_hp <=0)
- {
- cout << m.getNAME() << " has slain " << p.getNAME() << endl;
- cout << "You will now start over.";
- Sleep(5000);
- p.setHP(10);
- p.setSTR(5);
- p.setAR(0);
- p.setLV(1);
- p.setEXP(0);
- p.setGOLD(500);
- }
- cout << "Press any key to continue" << endl;
- getch();
- return;
- }
- void fDoTurn(Player *attacker, Player *target, int *hp)
- {
- int dmg = fRandomise(attacker->getSTR(), 0);
- if(dmg == 0)
- {
- cout << attacker->getNAME() << " missed!" << endl;
- }
- else
- {
- cout << attacker->getNAME() << " hit " << target->getNAME() << " for " << dmg << endl;
- }
- *hp -= dmg;
- return;
- }
- // Function fShowStats
- void fShowStats(Player p)
- {
- system("cls");
- cout << "Stats of " << p.getNAME() << endl << endl;
- cout << "Level : " << p.getLV() << endl;
- cout << "Health Points: " << p.getHP() << "/" << p.getMAXHP() << endl;
- cout << "Strength Points: " << p.getSTR() << endl;
- cout << "Armor Points: " << p.getAR() << endl;
- cout << "Experience Points: " << p.getEXP() << endl;
- cout << "Gold: " << p.getGOLD() << endl << endl;
- cout << "Enemies slain: " << p.getSLAIN()<< endl << endl;
- cout << "Press any key to go back to menu." << endl;
- getch();
- }
- //Function fSaveGame
- void fSaveGame(Player p)
- {
- system("cls");
- ofstream saveFile("Save.txt");
- if(saveFile.is_open())
- {
- saveFile << p.getNAME() << ' ' << p.getHP() << ' ' << p.getMAXHP() << ' ' << p.getSTR() << ' ' << p.getAR()
- << ' ' << p.getLV() << ' ' << p.getEXP() << ' ' << p.getGOLD();
- cout << "Save successful..."<< endl << endl << "Press any key to continue." << endl;
- }
- else
- {
- cout << "This is really strange" << endl;
- }
- saveFile.close();
- getch();
- return;
- }
- //Function fLoadGame
- void fLoadGame(Player *p)
- {
- system("cls");
- ifstream loadFile("Save.txt");
- string saveNAME;
- int saveHP, saveMAXHP, saveSTR, saveAR, saveLV, saveEXP, saveGOLD;
- loadFile >> saveNAME >> saveHP >> saveMAXHP >> saveSTR >> saveAR >> saveLV >> saveEXP >> saveGOLD;
- p->setNAME(saveNAME);
- p->setHP(saveHP);
- p->setMAXHP(saveMAXHP);
- p->setSTR(saveSTR);
- p->setAR(saveAR);
- p->setLV(saveLV);
- p->setEXP(saveEXP);
- p->setGOLD(saveGOLD);
- cout << "Load completed..." << endl << "Welcome back, " << p->getNAME() << endl << endl << "Press any key to continue" << endl;
- getch();
- }
- //Function fQuit
- void fQuit()
- {
- system("cls");
- cout << "Thank you for playing! Press any key to exit the program.";
- getch();
- return;
- }
- //Level up system
- void fLevelUp(Player *p)
- {
- int p_level = p->getLV();
- int nExpNeeded = ((p_level*50) ^ 2);
- if (p->getEXP() >= nExpNeeded)
- {
- system("cls");
- p->setLV(p->getLV()+1);
- p->setMAXHP(p->getMAXHP()+5);
- p->setHP(p->getMAXHP());
- p->setSTR(p->getSTR()+2);
- p->setEXP(p->getEXP()-nExpNeeded);
- cout << "Level up! Your level is now: " << p->getLV() << endl << endl << endl;
- getch();
- }
- return;
- }
- void fHeal(Player *p)
- {
- system("cls");
- cout << "Do you wish to be healed by the town shaman?\nThe fee is 100 gold.(Y/N)" << endl;
- char cChoiceHeal;
- cin >> cChoiceHeal;
- do
- {
- if(cChoiceHeal == 'y' || cChoiceHeal == 'Y')
- {
- p->setHP(p->getMAXHP());
- p->setGOLD(p->getGOLD() - 100);
- }
- else if (cChoiceHeal == 'n' || cChoiceHeal == 'N')
- {
- cout << "\n\nOkay. You will return to the menu";
- Sleep(1000);
- }
- }while(cChoiceHeal != 'y' && cChoiceHeal != 'Y' && cChoiceHeal != 'n' && cChoiceHeal != 'N');
- }
- //Randomise
- int fRandomise(int a, int b)
- {
- return rand() % a+b;
- }
Add Comment
Please, Sign In to add comment