#include #include using namespace std; class Plant { public: Plant() : size{1}, water{3}, air{3}, nutrient{3} {} void grow() { if ( is_alive() ) { ++size; --nutrient; if ( --water < 1 ) ++air; } } void give_water(int amount, int nutri = 0) { if ( water > 5 ) --air; water += amount; nutrient += nutri; } void health() const { if ( ! is_alive() ) cout << "Your plant is dead." << endl; else cout << "Your plant is " << size << " cm high and healthy." << endl; } bool is_alive() const { return ( air && water && nutrient ); } int get_size() const { return size; } private: int size; // cm int water; // 1-5 int air; // 1-5 int nutrient; // 1-5 }; class Powerup { public: Powerup() = default; virtual ~Powerup() = default; virtual void apply_to(Plant& plant) const = 0; virtual void print(ostream& os) const = 0; }; class Water : public Powerup { public: Water(int w) : Powerup{}, amount_w{w} {} void apply_to(Plant& plant) const override { plant.give_water(amount_w); } void print(ostream& os) const override { os << "Water " << amount_w << endl; } protected: int amount_w; }; class Nutrient_Water : public Water { public: Nutrient_Water(int w, int n) : Water{w}, amount_n{n} {} void apply_to(Plant& plant) const override { plant.give_water(amount_w, amount_n); } void print(ostream& os) const override { os << "Nutrient_Water " << amount_w << ", " << amount_n << endl; } protected: int amount_n; }; class Sunlight : public Powerup { public: Sunlight() : Powerup{} {} void apply_to(Plant& plant) const override { plant.grow(); } void print(ostream& os) const override { os << "Sunlight" << endl; } }; random_device rd; mt19937 mt{rd()}; uniform_int_distribution dist(1,3); Powerup* create_powerup(int type) { switch( type ) { case 1: // Create new Water powerup return new Water( dist(mt) ); case 2: // Create new Nutrient_Water powerup return new Nutrient_Water( dist(mt), dist(mt) ); default: // Create new Sunlight powerup return new Sunlight(); } } int main() { Plant tamagothchi; int score{}; while ( tamagothchi.is_alive() ) { Powerup* p = create_powerup( dist(mt) ); // Let user choose if to apply the powerup cout << endl << "New powerup available: "; p->print(cout); cout << "Apply? (y/n) "; string answer; getline(cin, answer); if ( answer.front() != 'n' ) { p->apply_to(tamagothchi); } tamagothchi.health(); score += tamagothchi.get_size(); delete p; } cout << "You scored " << score << " points!" << endl; return 0; }