Advertisement
Spocoman

03. Man O War

Nov 12th, 2023
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<int> vectorFiller(string s, char sep) {
  8.     vector<int> v;
  9.     string n = "";
  10.  
  11.     for (int i = 0; i < s.size(); i++) {
  12.         if (s[i] == sep) {
  13.             v.push_back(stoi(n));
  14.             n = "";
  15.         }
  16.         else {
  17.             n += s[i];
  18.         }
  19.     }
  20.     v.push_back(stoi(n));
  21.     return v;
  22. }
  23.  
  24. bool isIndexValid(int i, vector<int> v) {
  25.     return i >= 0 && i < v.size();
  26. }
  27.  
  28. int sum(vector<int> v) {
  29.     int result = 0;
  30.     for (auto& n : v) {
  31.         result += n;
  32.     }
  33.     return result;
  34. }
  35.  
  36. int main() {
  37.     string line, command;
  38.  
  39.     getline(cin, line);
  40.     auto pirateShip = vectorFiller(line, '>');
  41.  
  42.     getline(cin, line);
  43.     auto warShip = vectorFiller(line, '>');
  44.  
  45.     int maxSectionHealth, index, finalIndex, damage, health, count = 0;
  46.     cin >> maxSectionHealth;
  47.     cin.ignore();
  48.  
  49.     while (true) {
  50.         getline(cin, line);
  51.         if (line == "Retire") {
  52.             break;
  53.         }
  54.         else if (line == "Status") {
  55.             for (int i = 0; i < pirateShip.size(); i++) {
  56.                 if (pirateShip[i] < maxSectionHealth / 5) {
  57.                     count++;
  58.                 }
  59.             }
  60.             cout << count << " sections need repair.\n";
  61.             continue;
  62.         }
  63.  
  64.         command = line.substr(0, line.find(' '));
  65.         line.erase(0, command.size() + 1);
  66.         auto tokens = vectorFiller(line, ' ');
  67.         index = tokens[0];
  68.  
  69.         if (command == "Fire") {
  70.             damage = tokens[1];
  71.             if (isIndexValid(index, warShip) == 1) {
  72.                 warShip[index] -= damage;
  73.                 if (warShip[index] <= 0) {
  74.                     cout << "You won! The enemy ship has sunken.\n";
  75.                     warShip.clear();
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         else if (command == "Defend") {
  81.             finalIndex = tokens[1];
  82.             damage = tokens[2];
  83.             if (isIndexValid(index, pirateShip) == 1 && isIndexValid(finalIndex, pirateShip) == 1) {
  84.                 for (int i = index; i <= finalIndex; i++) {
  85.                     pirateShip[i] -= damage;
  86.                     if (pirateShip[i] <= 0) {
  87.                         cout << "You lost! The pirate ship has sunken.\n";
  88.                         pirateShip.clear();
  89.                         return(0);
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         else if (command == "Repair") {
  95.             health = tokens[1];
  96.             if (isIndexValid(index, pirateShip) == 1) {
  97.                 pirateShip[index] += health;
  98.                 if (pirateShip[index] > maxSectionHealth) {
  99.                     pirateShip[index] = maxSectionHealth;
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     if (pirateShip.size() > 0 && warShip.size() > 0) {
  106.         cout << "Pirate ship status: " << sum(pirateShip) << "\nWarship status: " << sum(warShip) << endl;
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement