Advertisement
zCool

RPG Battle Part 2

May 7th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. void WaitForEnter();
  8. int attack(int attackerATK, int victimHP, int victimDEF);
  9. int heal(int oldHP);
  10. int playerHP = 100;
  11. int playerATK = 10;
  12. int playerDEF = 5;
  13. int monsterHP = 50;
  14. int monsterATK = 10;
  15. int monsterDEF = 5;
  16. string userInput = "";
  17. int tmpVar = 0;
  18. int damage = 0;
  19.  
  20.  
  21. while (monsterHP > 0 && playerHP > 0)
  22. {
  23.     //display HP for monster & player
  24.     cout << "Monster HP: "<< monsterHP << endl
  25.          << "Player HP: " << playerHP << endl;
  26.    
  27.     //display options (Attack, Heal)
  28.     cout << "Attack" << endl
  29.          << "Heal" << endl
  30.          << "What would you like to do? ";
  31.  
  32.     getline(cin, userInput);
  33.  
  34.     //Validate
  35.     while(userInput.compare("Attack") != 0 &&
  36.         userInput.compare("Heal") != 0)
  37.     {
  38.         cout << "What would you like to do? ";
  39.         getline(cin, userInput);
  40.     }
  41.  
  42.     //Attack
  43.     if(userInput.compare("Attack") == 0)
  44.     {
  45.         //player attack
  46.         tmpVar = monsterHP;
  47.         monsterHP = attack(playerATK, monsterHP, monsterDEF);
  48.         damage = tmpVar - monsterHP;
  49.         cout << "Player dealt " << damage << " damage!" << endl;
  50.  
  51.         //monster attack
  52.         tmpVar = playerHP;
  53.         playerHP = attack(monsterATK, playerHP, playerDEF);
  54.         damage = tmpVar - playerHP;
  55.         cout << "Monster dealt " << damage << " damage!" << endl;
  56.     }
  57.  
  58.     //Heal
  59.     if(userInput.compare("Heal") == 0)
  60.     {
  61.  
  62.         //player heal
  63.         tmpVar = playerHP;
  64.         playerHP = heal(playerHP);
  65.         cout << "Player Healed " << playerHP - tmpVar << " HP!" << endl;
  66.  
  67.         //monster attack
  68.         tmpVar = playerHP;
  69.         playerHP = attack(monsterATK, playerHP, playerDEF);
  70.         damage = tmpVar - playerHP;
  71.         cout << "Monster dealt " << damage << " damage!" << endl;
  72.     }
  73.  
  74. }
  75.  
  76. cout << "Press Enter To Exit...";
  77. WaitForEnter();
  78.  
  79. }
  80.  
  81. //Taken from Moosader (Rachel J. Morris)
  82. void WaitForEnter(){ while(1) { if ('\n' == getchar() ) { break; } } }
  83.  
  84. //attack function
  85. int attack(int attackerATK, int victimHP, int victimDEF)
  86. {
  87.     return (victimHP - rand()%(attackerATK - victimDEF));
  88. }
  89.  
  90. //heal function
  91. int heal(int oldHP)
  92. {
  93.     return (oldHP + rand()%5 + 1);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement