Advertisement
shadowlucario50

C++ Basic Battle System

Apr 23rd, 2025 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. int main() {
  7.     //Challenge: Make a battle system where an enemy HP is slowly depleted by attacks.
  8.  
  9.     double hp = 500;
  10.     int atk;
  11.     double power;
  12.     double damage;
  13.     int atkAmt = 0;
  14.     double accuracy;
  15.     int random1;
  16.     int random2;
  17.     double accuracyHit;
  18.     srand(time(NULL));
  19.  
  20.     do{
  21.         std::cout << "Enter an Attack Number Between 1-100: ";
  22.         std::cin >> atk;
  23.         //if(atk <= 0) atk = 1;
  24.         //else if(atk > 100) atk = 100;
  25.     }while(atk > 100 || atk <= 0);
  26.  
  27.     //accuracy will go down to 80 if atk=100 and 100 if atk=1.
  28.     accuracy = (-20 * atk + 9920)/99;
  29.  
  30.     //atk 1 base 30 atk 100 base 150
  31.     // y = (120x + 2850) / 99
  32.     power = (120 * atk + 2850) / 99;
  33.  
  34.     do{
  35.         std::cout << "Enemy HP: " << round(hp) << '\n';
  36.        
  37.         //Accuracy Check
  38.         random1 = rand() % 100 + 1;
  39.  
  40.         if(random1 > accuracy) {
  41.             //Miss Chance
  42.             std::cout << "You missed! ";
  43.         }
  44.        
  45.         else{
  46.             //Damage Calcs
  47.             random2 = rand() % 41 + 80; //Goes from 0.8 to 1.2
  48.             if(random2 == 120) {
  49.                 //Crits
  50.                 damage = power * 1.5;
  51.                 std::cout << "**Critical Hit!**\n";
  52.                 std::cout << "You did " << damage << " damage! ";
  53.             }
  54.             else {
  55.                 damage = round(power * ((double)random2 / 100));
  56.                 std::cout << "You did " << damage << " damage! ";
  57.             }
  58.  
  59.             //HP Removal
  60.             hp = hp - damage;
  61.         }
  62.        
  63.         //Turn Track
  64.         atkAmt++;
  65.     }while(hp > 0);
  66.  
  67.     std::cout << "Enemy defeated!\nTook " << atkAmt << " turns to defeat!";
  68.  
  69.     return 0;
  70. }
Tags: Code Battle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement