Advertisement
Guest User

How to save the player's health so the game can end

a guest
Mar 29th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>    
  4. #include <cstdio>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class Player {
  11. public:
  12.     int health = 100;
  13.     bool isAlive = true;
  14. };
  15.  
  16.  
  17. class Computer {
  18. public:
  19.     int health = 100;
  20.     bool isAlive = true;
  21. };
  22.  
  23. void playerAttack(int x)
  24. {
  25.     Player player{};
  26.  
  27.     Computer computer{};
  28.  
  29.     srand((unsigned)time(0));
  30.  
  31.     int damageTaken;
  32.  
  33.     damageTaken = (rand() % 41) + 15;
  34.  
  35.     if (damageTaken == 41)
  36.     {
  37.         damageTaken = 40;
  38.     }
  39.  
  40.     x = computer.health = computer.health - damageTaken;
  41.  
  42.     cout << "you have taken " << damageTaken << " damage from the computer. Its health is now " << computer.health << endl;
  43. }  
  44.  
  45.  
  46. void playerHeal(int x)
  47. {
  48.  
  49.     Player player{};
  50.    
  51.     srand((unsigned)time(0));
  52.  
  53.     int healthHealed;
  54.  
  55.     healthHealed = (rand() % 30) + 15;
  56.  
  57.     x = player.health = player.health + healthHealed;
  58.  
  59.     if (player.health > 100)
  60.     {
  61.         player.health = 100;
  62.     }
  63.  
  64.     cout << "You have healed " << healthHealed << " damage. Your health is now " << player.health << endl;
  65. }
  66.  
  67.  
  68. void computerAttack(int x)
  69. {  
  70.     Computer computer{};
  71.  
  72.     Player player{};
  73.  
  74.     int computerHealth = computer.health;
  75.  
  76.     srand((unsigned)time(0));
  77.  
  78.     int damageTaken;
  79.  
  80.     damageTaken = (rand() % 40) + 15;
  81.  
  82.     x = player.health = player.health - damageTaken;
  83.  
  84.     cout << "the computer has taken " << damageTaken << " damage from you. Your health is now " << player.health << endl;
  85. }
  86.  
  87.  
  88. void computerHeal(int x)
  89. {
  90.  
  91.     Computer computer{};
  92.  
  93.     int computerHealth = computer.health;
  94.  
  95.     srand((unsigned)time(0));
  96.  
  97.     int healthHealed;
  98.  
  99.     healthHealed = (rand() % 30) + 15;
  100.  
  101.     int convertTo100 = healthHealed + computer.health;
  102.  
  103.     x = computer.health = computer.health + healthHealed;
  104.  
  105.     if (computer.health > 100)
  106.     {
  107.         computer.health = 100;
  108.     }
  109.  
  110.     cout << "The computer has healed " << healthHealed << " damage. Its health is now " << computer.health << endl;
  111. }
  112.  
  113.  
  114. void computerRandomChoose()
  115. {
  116.     Computer computer{};
  117.  
  118.     Player player{};
  119.  
  120.     srand((unsigned)time(0));
  121.    
  122.     int randomMove;
  123.  
  124.     randomMove = (rand() % 3) + 1;
  125.  
  126.  
  127.     if (computer.health == 100)
  128.     {
  129.         computerAttack(player.health);
  130.     }
  131.  
  132.     else if (randomMove == 2)
  133.     {
  134.         computerAttack(player.health);
  135.     }
  136.      
  137.     else if (randomMove == 3)
  138.     {
  139.         computerAttack(player.health);
  140.     }
  141.  
  142.     else
  143.     {
  144.         computerHeal(computer.health);
  145.     }
  146.  
  147. }
  148.  
  149.  
  150. void askPlayerWhatToDo()
  151. {
  152.     Computer computer{};
  153.  
  154.     Player player{};
  155.     cout << "Will you attack (a), or heal (h): ";
  156.  
  157.     char choice;
  158.  
  159.     cin >> choice;
  160.  
  161.     if (choice == 'a')
  162.     {
  163.         playerAttack(computer.health);
  164.     }
  165.  
  166.     else if (choice == 'h')
  167.     {
  168.         playerHeal(player.health);
  169.     }
  170.      
  171.  
  172.     else
  173.     {
  174.         cout << "Invalid choice, please retry" << endl;
  175.  
  176.         askPlayerWhatToDo();
  177.     }
  178.  
  179. }
  180.  
  181.  
  182. void theGame()
  183. {
  184.     Player player{};
  185.  
  186.     Computer computer{};
  187.  
  188.     while (player.health > 0 && computer.health > 0) {
  189.  
  190.         askPlayerWhatToDo();
  191.  
  192.         computerRandomChoose();
  193.     }
  194.    
  195.     if (player.health <= 0)
  196.     {
  197.         player.isAlive = false;
  198.  
  199.         cout << "The computer has won. " << endl;
  200.     }
  201.  
  202.     else if (computer.health <= 0)
  203.     {  
  204.         computer.isAlive = false;
  205.  
  206.         cout << "You have won" << endl;
  207.     }
  208.    
  209.     else if (computer.health == 0 && player.health == 0)
  210.     {
  211.         cout << "You and the computer have died. So you tied." << endl;
  212.     }
  213.  
  214.     if (player.health == 0 || computer.health == 0)
  215.     {
  216.         cout << "Do you want to restart? (y = yes, n = no): ";
  217.  
  218.         char choice;
  219.         cin >> choice;
  220.  
  221.         if (choice == 'y')
  222.         {
  223.             theGame();
  224.         }
  225.     }
  226.  }
  227.  
  228.  
  229. int main()
  230. {  
  231.     cout << "Do you want to start a new game? (y = yes, n = no): ";
  232.  
  233.     char choice;
  234.  
  235.     cin >> choice;
  236.  
  237.     if (choice == 'y')
  238.     {
  239.         theGame();
  240.     }
  241.    
  242.     return 0;
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement