-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecorator.cpp
More file actions
107 lines (84 loc) · 2.65 KB
/
Decorator.cpp
File metadata and controls
107 lines (84 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<memory>
#include<iostream>
using namespace std;
/*
I have upgradable buildings, each building has an effect on GameState. Buildings with multiple upgrades will apply multiple effects. Buildings:
HOUSE, that can be upgraded with more obstacles in the garden, or speeding up the thief that wants to rob the house
*/
class GameState{
private:
int obstacles;
float money;
float speed;
public:
GameState(int obstacles, float money, float speed) : obstacles(obstacles), money(money), speed(speed) {}
int getObstacles(){
return this->obstacles;
}
float getMoney(){
return this->money;
}
float getSpeed(){
return this->speed;
}
void speedUp(int sp){
this->speed += sp;
}
void increaseObstacle(int obstInc){
this->obstacles += obstInc;
}
void decreseMoney(int money){
this->money -= money;
}
void printGameState(){
cout << "Obstacles: " << this->obstacles << ", Moneys: " << this->money << ", Speed: " << this->speed << endl;
}
};
class Building{
public:
virtual void effect(std::shared_ptr<GameState> gs) = 0;
};
class House : public Building{
void effect(std::shared_ptr<GameState> gs) override{
gs->decreseMoney(20);
gs->printGameState();
}
};
class BuildingDecorator : public Building{
protected:
std::shared_ptr<Building> building;
public:
BuildingDecorator(std::shared_ptr<Building> b){
this->building = b;
}
void effect(std::shared_ptr<GameState> gs) override{
building->effect(gs);
}
};
class SpeedUpgrade : public BuildingDecorator{
public:
SpeedUpgrade(std::shared_ptr<Building> b) : BuildingDecorator(b) {}
void effect(std::shared_ptr<GameState> gs){
gs->speedUp(10);
gs->decreseMoney(5);
BuildingDecorator::effect(gs);
}
};
class ObstacleUpgrade : public BuildingDecorator{
public:
ObstacleUpgrade(std::shared_ptr<Building> b) : BuildingDecorator(b) {}
void effect(std::shared_ptr<GameState> gs){
gs->increaseObstacle(1);
gs->decreseMoney(10);
BuildingDecorator::effect(gs);
}
};
int main(){
std::shared_ptr<GameState> gs(new GameState(3, 100, 20));
std::shared_ptr<Building> house1(new House());
house1 = std::make_shared<SpeedUpgrade>(house1);
house1 = std::make_shared<ObstacleUpgrade>(house1);
house1 = std::make_shared<SpeedUpgrade>(house1);
house1->effect(gs);
return 0;
}