Reliable - ni chuja nic nie zrobiłem i tyle

This commit is contained in:
Sasza Stanczew 2024-05-06 14:48:58 +02:00
parent 17638fcc48
commit 18bdf44313
4 changed files with 46 additions and 11 deletions

View file

@ -15,7 +15,9 @@
int main() { int main() {
Time time; Time time;
Ship ship(&time, "Black Widow", 1000, 40); auto strat = std::make_unique<ShipEasyStrategy<Ship, bool>>();
Ship ship(&time, strat, "Black Widow", 1000, 40);
ship.load(std::make_unique<Alcohol>(300)); ship.load(std::make_unique<Alcohol>(300));
ship.load(std::make_unique<Fruit>(200)); ship.load(std::make_unique<Fruit>(200));
ship.load(std::make_unique<Fruit>(150)); ship.load(std::make_unique<Fruit>(150));
@ -28,11 +30,11 @@ int main() {
ship.printCargo(); ship.printCargo();
} }
std::cout << "\n\n****************************************************\n"; // std::cout << "\n\n****************************************************\n";
auto ship2 = std::make_unique<Ship>(&time, "Queens Anne revenge", 1000, 40); // auto ship2 = std::make_unique<Ship>(&time, "Queens Anne revenge", 1000, 40);
Enemy enemy("Enemy1", std::move(ship2)); // Enemy enemy("Enemy1", std::move(ship2));
//
for (int i = 0; i < 15; ++i) { // for (int i = 0; i < 15; ++i) {
enemy.attack(ship); // enemy.attack(ship);
} // }
} }

View file

@ -0,0 +1,4 @@
template <typename Res, typename T>
class ShipStrategy {
Res handle(T&);
}

View file

@ -5,10 +5,16 @@
#include <vector> #include <vector>
#include <Core/TimeObserver.h> #include <Core/TimeObserver.h>
#include <Core/ShipStrategy.h>
#include "Ship/Cargo.h" #include "Ship/Cargo.h"
class Time; class Time;
template <typename T, typename Res>
class ShipEasyStrategy : public ShipStrategy<Res, T> {
Res handle(T &t) override;
};
class Ship : public TimeObserver { class Ship : public TimeObserver {
public: public:
enum class StatusCode { enum class StatusCode {
@ -17,7 +23,7 @@ public:
LackOfSpace LackOfSpace
}; };
Ship(Time* time, const std::string& name, int capacity, int crew); Ship(Time* time, std::unique_ptr<ShipStrategy<Ship, bool>> strategy, const std::string& name, int capacity, int crew);
~Ship(); ~Ship();
Ship(const Ship&) = default; Ship(const Ship&) = default;
Ship(Ship&&) = default; Ship(Ship&&) = default;
@ -48,8 +54,10 @@ private:
Time* _time; Time* _time;
std::string _name; std::string _name;
ShipStrategy<Ship, bool> _strategy;
int _capacity; int _capacity;
int _crew; int _crew;
int _durability{1000}; int _durability{1000};
std::vector<std::unique_ptr<Cargo>> _cargoes; std::vector<std::unique_ptr<Cargo>> _cargoes;
}; };

View file

@ -8,7 +8,7 @@
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
Ship::Ship(Time* time, std::unique_ptr<DifficultLvlStrategy<Ship, bool>> strategy, const std::string& name, int capacity, int crew) Ship::Ship(Time* time, std::unique_ptr<ShipStrategy<Ship, bool>> strategy, const std::string& name, int capacity, int crew)
: _time(time), _strategy(std::move(strategy)), _name(name), _capacity(capacity), _crew(crew) { : _time(time), _strategy(std::move(strategy)), _name(name), _capacity(capacity), _crew(crew) {
if (_capacity < 0) { if (_capacity < 0) {
throw std::runtime_error("Capacity can't be negative value!"); throw std::runtime_error("Capacity can't be negative value!");
@ -111,4 +111,25 @@ void Ship::rebel() {
if (_crew < 5) { if (_crew < 5) {
throw std::runtime_error("There is not enought crew! Game over!"); throw std::runtime_error("There is not enought crew! Game over!");
} }
} }
template <typename T, typename Res>
bool ShipEasyStrategy::handle(std::string &t) {
int left = _crew;
while (left != 0) {
auto it = std::ranges::find_if(_cargoes, [&cargoName](const auto& cargo) { return cargo->name() == cargoName; });
if (it != _cargoes.end()) {
if ((*it)->amount > left) {
(*it)->amount -= left;
return true;
}
left -= (*it)->amount;
_cargoes.erase(it);
} else {
return false;
}
}
return true;
}