Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- vector<int> vectorFiller(string s, char sep) {
- vector<int> v;
- string n = "";
- for (int i = 0; i < s.size(); i++) {
- if (s[i] == sep) {
- v.push_back(stoi(n));
- n = "";
- }
- else {
- n += s[i];
- }
- }
- v.push_back(stoi(n));
- return v;
- }
- bool isIndexValid(int i, vector<int> v) {
- return i >= 0 && i < v.size();
- }
- int sum(vector<int> v) {
- int result = 0;
- for (auto& n : v) {
- result += n;
- }
- return result;
- }
- int main() {
- string line, command;
- getline(cin, line);
- auto pirateShip = vectorFiller(line, '>');
- getline(cin, line);
- auto warShip = vectorFiller(line, '>');
- int maxSectionHealth, index, finalIndex, damage, health, count = 0;
- cin >> maxSectionHealth;
- cin.ignore();
- while (true) {
- getline(cin, line);
- if (line == "Retire") {
- break;
- }
- else if (line == "Status") {
- for (int i = 0; i < pirateShip.size(); i++) {
- if (pirateShip[i] < maxSectionHealth / 5) {
- count++;
- }
- }
- cout << count << " sections need repair.\n";
- continue;
- }
- command = line.substr(0, line.find(' '));
- line.erase(0, command.size() + 1);
- auto tokens = vectorFiller(line, ' ');
- index = tokens[0];
- if (command == "Fire") {
- damage = tokens[1];
- if (isIndexValid(index, warShip) == 1) {
- warShip[index] -= damage;
- if (warShip[index] <= 0) {
- cout << "You won! The enemy ship has sunken.\n";
- warShip.clear();
- break;
- }
- }
- }
- else if (command == "Defend") {
- finalIndex = tokens[1];
- damage = tokens[2];
- if (isIndexValid(index, pirateShip) == 1 && isIndexValid(finalIndex, pirateShip) == 1) {
- for (int i = index; i <= finalIndex; i++) {
- pirateShip[i] -= damage;
- if (pirateShip[i] <= 0) {
- cout << "You lost! The pirate ship has sunken.\n";
- pirateShip.clear();
- return(0);
- }
- }
- }
- }
- else if (command == "Repair") {
- health = tokens[1];
- if (isIndexValid(index, pirateShip) == 1) {
- pirateShip[index] += health;
- if (pirateShip[index] > maxSectionHealth) {
- pirateShip[index] = maxSectionHealth;
- }
- }
- }
- }
- if (pirateShip.size() > 0 && warShip.size() > 0) {
- cout << "Pirate ship status: " << sum(pirateShip) << "\nWarship status: " << sum(warShip) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement