Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <windows.h>
  6. #include "Player.h"
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. const string m_name[]={"Orc", "Goblin", "Dark Elf"};
  12.  
  13. int fRandomise(int a, int b);
  14. void fDoTurn(Player *, Player *, int *);
  15. void getChoice(Player p);
  16. void fBattle(Player p, Player *player);
  17. void fShowStats(Player p);
  18. void fSaveGame(Player p);
  19. void fLoadGame(Player *);
  20. void fLevelUp(Player *p);
  21. void fHeal(Player *p);
  22. void fQuit();
  23.  
  24. int main()
  25. {
  26.     srand(time(NULL));
  27.  
  28.     string name;
  29.     Player p(0, 0, 0, 0, 0, 0, 0, 0, name);
  30.  
  31.     bool start = true;
  32.     do
  33.     {
  34.         char cChoiceNewGame;
  35.  
  36.  
  37.         cout << "N - New Game" << endl;
  38.         cout << "L - Load Game" << endl;
  39.         cout << "Enter your choice here: ";
  40.         start = false;
  41.  
  42.         do
  43.         {
  44.             cin >> cChoiceNewGame;
  45.             if(cChoiceNewGame == 'N' || cChoiceNewGame == 'n')
  46.             {
  47.                 system("cls");
  48.  
  49.                 cout << "Enter your hero's name: ";
  50.                 cin >> name;
  51.                 Player p(20, 30, 5, 0, 1, 0, 500, 0, name);
  52.                 getChoice(p);
  53.             }
  54.             if(cChoiceNewGame == 'L' || cChoiceNewGame == 'l')
  55.             {
  56.                 system("cls");
  57.                 fLoadGame(&p);
  58.                 getChoice(p);
  59.             }
  60.         }while(cChoiceNewGame != 'N' && cChoiceNewGame != 'n' && cChoiceNewGame != 'L' && cChoiceNewGame != 'l');
  61.     }while (start != false);
  62.  
  63.     return 0;
  64. }
  65.  
  66. void getChoice(Player p)
  67. {
  68.     char cChoice;
  69.  
  70.     do
  71.     {
  72.         fLevelUp(&p);
  73.         system("cls");
  74.  
  75.         cout << "B - Battle" << endl;
  76.         cout << "H - Be healed" << endl;
  77.         cout << "V - See Stats" << endl;
  78.         cout << "S - Save the Game" << endl;
  79.         cout << "K - Save and quit" << endl;
  80.         cout << "Q - Quit the game" << endl;
  81.         cout << "Enter your choice here: ";
  82.         cin >> cChoice;
  83.  
  84.  
  85.         if(cChoice == 'B' || cChoice == 'b')
  86.         {
  87.             fBattle(p, &p);
  88.         }
  89.         else if(cChoice == 'V' || cChoice == 'v')
  90.         {
  91.             fShowStats(p);
  92.         }
  93.         else if(cChoice == 'H' || cChoice == 'h')
  94.         {
  95.             fHeal(&p);
  96.         }
  97.         else if(cChoice == 'S' || cChoice == 's')
  98.         {
  99.             fSaveGame(p);
  100.         }
  101.  
  102.         else if(cChoice == 'K' || cChoice == 'k')
  103.         {
  104.             fSaveGame(p);
  105.             fQuit();
  106.         }
  107.         else if(cChoice == 'Q' || cChoice == 'q')
  108.         {
  109.             fQuit();
  110.         }
  111.     }while(cChoice != 'Q' && cChoice !='q' && cChoice != 'K' && cChoice !='k');
  112. }
  113.  
  114. //Function fBattle
  115. void fBattle(Player p, Player *player)
  116. {
  117.     system("cls");
  118.  
  119.     int player_hp = p.getHP();
  120.  
  121.     string monster_name =       m_name[fRandomise(2,0)];
  122.     int monster_level   =       fRandomise(p.getLV(), 1);
  123.     int monster_str     =       1+monster_level*1.5;
  124.     int monster_hp      =       fRandomise(monster_level*50, monster_level*20);
  125.     int monster_drop    =       fRandomise(monster_level*8, 1+monster_level*2);
  126.     int monster_exp     =       fRandomise(monster_level*50, monster_level*10);
  127.  
  128.     Player m(monster_hp,monster_str,1,monster_level,30,monster_name);
  129.  
  130.     cout << monster_name << " of level " << m.getLV() << " has appeared!" << endl << "It has " << m.getHP() << "HP!" << endl << endl;
  131.     Sleep(2000);
  132.  
  133.     while (monster_hp >0 && player_hp >0)
  134.     {
  135.         if(player_hp <= 10)
  136.         {
  137.             cout << "Your HP is low. Do you wish to attempt to flee?\nThe rate of success is 50% (Y/N)" << endl;
  138.  
  139.             char cChoice;
  140.             do
  141.             {
  142.                 cin >> cChoice;
  143.                 if(cChoice == 'y' || cChoice == 'Y')
  144.                 {
  145.                     int nFlee = fRandomise(100, 0);
  146.  
  147.                     if (nFlee >= 50)
  148.                     {
  149.                         cout << "You fleed!" << endl << endl;
  150.                         Sleep(2000);
  151.                         return;
  152.                     }
  153.                     else
  154.                     {
  155.                         cout << "You failed!" << endl << endl;
  156.                     }
  157.                 }
  158.                 else if(cChoice == 'n' || cChoice == 'N')
  159.                 {
  160.                 }
  161.             }while(cChoice != 'n' && cChoice != 'N' && cChoice != 'y' && cChoice != 'Y');
  162.         }
  163.  
  164.         fDoTurn(&p, &m, &monster_hp);
  165.         Sleep(500);
  166.         if(monster_hp <=0)
  167.         {
  168.             cout << endl;
  169.             break;
  170.         }
  171.         fDoTurn(&m, &p, &player_hp);
  172.         Sleep(500);
  173.         cout << endl;
  174.     }
  175.     if(monster_hp <= 0)
  176.     {
  177.         cout << p.getNAME() << " has slain " << m.getNAME() << endl;
  178.         cout << "You have gained" << monster_exp << " exp and " << monster_drop << "gold" << endl << endl;
  179.  
  180.         player->setEXP(player->getEXP()+50);
  181.         player->setGOLD(player->getGOLD()+20);
  182.         player->setHP(player_hp);
  183.         player->setSLAIN(player->getSLAIN()+1);
  184.     }
  185.     if(player_hp <=0)
  186.     {
  187.         cout << m.getNAME() << " has slain " << p.getNAME() << endl;
  188.         cout << "You will now start over.";
  189.  
  190.         Sleep(5000);
  191.  
  192.         p.setHP(10);
  193.         p.setSTR(5);
  194.         p.setAR(0);
  195.         p.setLV(1);
  196.         p.setEXP(0);
  197.         p.setGOLD(500);
  198.     }
  199.  
  200.     cout << "Press any key to continue" << endl;
  201.     getch();
  202.  
  203.     return;
  204. }
  205.  
  206. void fDoTurn(Player *attacker, Player *target, int *hp)
  207. {
  208.     int dmg = fRandomise(attacker->getSTR(), 0);
  209.  
  210.     if(dmg == 0)
  211.     {
  212.         cout << attacker->getNAME() << " missed!" << endl;
  213.     }
  214.     else
  215.     {
  216.         cout << attacker->getNAME() << " hit " << target->getNAME() << " for " << dmg << endl;
  217.     }
  218.     *hp -= dmg;
  219.  
  220.     return;
  221. }
  222.  
  223. // Function fShowStats
  224. void fShowStats(Player p)
  225. {
  226.     system("cls");
  227.  
  228.     cout << "Stats of "           << p.getNAME() << endl << endl;
  229.     cout << "Level :            " << p.getLV()   << endl;
  230.     cout << "Health Points:     " << p.getHP()   << "/"  << p.getMAXHP() << endl;
  231.     cout << "Strength Points:   " << p.getSTR()  << endl;
  232.     cout << "Armor Points:      " << p.getAR()   << endl;
  233.     cout << "Experience Points: " << p.getEXP()  << endl;
  234.     cout << "Gold:              " << p.getGOLD() << endl << endl;
  235.     cout << "Enemies slain:     " << p.getSLAIN()<< endl << endl;
  236.     cout << "Press any key to go back to menu." << endl;
  237.     getch();
  238. }
  239.  
  240. //Function fSaveGame
  241. void fSaveGame(Player p)
  242. {
  243.     system("cls");
  244.  
  245.     ofstream saveFile("Save.txt");
  246.  
  247.     if(saveFile.is_open())
  248.     {
  249.         saveFile << p.getNAME() << ' ' << p.getHP() << ' ' << p.getMAXHP() << ' ' << p.getSTR() << ' ' << p.getAR()
  250.                  << ' ' << p.getLV() << ' ' << p.getEXP() << ' ' << p.getGOLD();
  251.  
  252.         cout << "Save successful..."<< endl << endl << "Press any key to continue." << endl;
  253.     }
  254.  
  255.     else
  256.     {
  257.         cout << "This is really strange" << endl;
  258.     }
  259.  
  260.     saveFile.close();
  261.  
  262.     getch();
  263.     return;
  264. }
  265.  
  266. //Function fLoadGame
  267. void fLoadGame(Player *p)
  268. {
  269.     system("cls");
  270.  
  271.             ifstream loadFile("Save.txt");
  272.  
  273.             string saveNAME;
  274.             int saveHP, saveMAXHP, saveSTR, saveAR, saveLV, saveEXP, saveGOLD;
  275.  
  276.             loadFile >> saveNAME >> saveHP >> saveMAXHP >> saveSTR >> saveAR >> saveLV >> saveEXP >> saveGOLD;
  277.  
  278.             p->setNAME(saveNAME);
  279.             p->setHP(saveHP);
  280.             p->setMAXHP(saveMAXHP);
  281.             p->setSTR(saveSTR);
  282.             p->setAR(saveAR);
  283.             p->setLV(saveLV);
  284.             p->setEXP(saveEXP);
  285.             p->setGOLD(saveGOLD);
  286.  
  287.             cout << "Load completed..." << endl << "Welcome back, " << p->getNAME() << endl << endl << "Press any key to continue" << endl;
  288.             getch();
  289. }
  290.  
  291. //Function fQuit
  292. void fQuit()
  293. {
  294.     system("cls");
  295.  
  296.     cout << "Thank you for playing! Press any key to exit the program.";
  297.     getch();
  298.  
  299.     return;
  300. }
  301.  
  302. //Level up system
  303. void fLevelUp(Player *p)
  304. {
  305.     int p_level = p->getLV();
  306.     int nExpNeeded = ((p_level*50) ^ 2);
  307.  
  308.     if (p->getEXP() >= nExpNeeded)
  309.     {
  310.         system("cls");
  311.         p->setLV(p->getLV()+1);
  312.         p->setMAXHP(p->getMAXHP()+5);
  313.         p->setHP(p->getMAXHP());
  314.         p->setSTR(p->getSTR()+2);
  315.         p->setEXP(p->getEXP()-nExpNeeded);
  316.  
  317.         cout << "Level up! Your level is now: " << p->getLV() << endl << endl << endl;
  318.         getch();
  319.     }
  320.     return;
  321. }
  322.  
  323. void fHeal(Player *p)
  324. {
  325.     system("cls");
  326.  
  327.     cout << "Do you wish to be healed by the town shaman?\nThe fee is 100 gold.(Y/N)" << endl;
  328.  
  329.     char cChoiceHeal;
  330.  
  331.     cin >> cChoiceHeal;
  332.  
  333.     do
  334.     {
  335.         if(cChoiceHeal == 'y' || cChoiceHeal == 'Y')
  336.         {
  337.             p->setHP(p->getMAXHP());
  338.             p->setGOLD(p->getGOLD() - 100);
  339.         }
  340.         else if (cChoiceHeal == 'n' || cChoiceHeal == 'N')
  341.         {
  342.             cout << "\n\nOkay. You will return to the menu";
  343.             Sleep(1000);
  344.         }
  345.     }while(cChoiceHeal != 'y' && cChoiceHeal != 'Y' && cChoiceHeal != 'n' && cChoiceHeal != 'N');
  346. }
  347.  
  348. //Randomise
  349. int fRandomise(int a, int b)
  350. {
  351.     return rand() % a+b;
  352. }
Add Comment
Please, Sign In to add comment