fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main() {
  7. srand(time(0));
  8.  
  9. int playerHP = 100;
  10. int enemyHP = 50;
  11. int playerLevel = 1;
  12.  
  13. cout << "=== SKY FIGHT GAME ===" << endl;
  14. cout << "Battle an enemy in the sky!" << endl << endl;
  15.  
  16. while (playerHP > 0 && enemyHP > 0) {
  17. cout << "\n--- Your HP: " << playerHP << " | Enemy HP: " << enemyHP << " ---" << endl;
  18. cout << "1. Attack\n2. Defend\n3. Escape" << endl;
  19. cout << "Choose action: ";
  20.  
  21. int action;
  22. cin >> action;
  23.  
  24. if (action == 1) {
  25. int damage = 10 + rand() % 15;
  26. enemyHP -= damage;
  27. cout << "You attack for " << damage << " damage!" << endl;
  28. } else if (action == 2) {
  29. int damage = 5 + rand() % 8;
  30. playerHP += 5;
  31. cout << "You defend and recover 5 HP!" << endl;
  32. damage = rand() % 5;
  33. playerHP -= damage;
  34. cout << "Enemy deals " << damage << " damage!" << endl;
  35. continue;
  36. } else if (action == 3) {
  37. cout << "You escaped from the sky battle!" << endl;
  38. return 0;
  39. }
  40.  
  41. if (enemyHP > 0) {
  42. int enemyDamage = 8 + rand() % 12;
  43. playerHP -= enemyDamage;
  44. cout << "Enemy attacks for " << enemyDamage << " damage!" << endl;
  45. }
  46. }
  47.  
  48. if (playerHP > 0) {
  49. cout << "\n*** YOU WON! ***" << endl;
  50. cout << "Victory in the sky! Level up!" << endl;
  51. } else {
  52. cout << "\n*** YOU LOST! ***" << endl;
  53. cout << "Defeated in the sky battle..." << endl;
  54. }
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
=== SKY FIGHT GAME ===
Battle an enemy in the sky!


--- Your HP: 100 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 19 damage!

--- Your HP: 81 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 13 damage!

--- Your HP: 68 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 11 damage!

--- Your HP: 57 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 11 damage!

--- Your HP: 46 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 19 damage!

--- Your HP: 27 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 9 damage!

--- Your HP: 18 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 15 damage!

--- Your HP: 3 | Enemy HP: 50 ---
1. Attack
2. Defend
3. Escape
Choose action: Enemy attacks for 11 damage!

*** YOU LOST! ***
Defeated in the sky battle...