1
+ #ifndef BATTLE_H
2
+ #define BATTLE_H
3
+
4
+ #include < vector>
5
+ #include < iostream>
6
+ #include < utility>
7
+ #include " rebelfleet.h"
8
+ #include " imperialfleet.h"
9
+ #include " helper.h"
10
+
11
+ class SpaceBattle {
12
+ public:
13
+ class Builder ;
14
+
15
+ SpaceBattle (
16
+ Time t0,
17
+ Time t1,
18
+ std::vector<std::shared_ptr<RebelStarship>> rebelStarships,
19
+ std::vector<std::shared_ptr<ImperialStarship>> imperialStarships) :
20
+ rebelStarships (std::move(rebelStarships)),
21
+ imperialStarships (std::move(imperialStarships)) {
22
+ timer = new ExemplaryTimer (t0, t1);
23
+ }
24
+
25
+ size_t countImperialFleet () {
26
+ size_t imperialStarshipsNumber = 0 ;
27
+ for (auto unit : imperialStarships) {
28
+ imperialStarshipsNumber += unit->getUnitsNumber ();
29
+ }
30
+ return imperialStarshipsNumber;
31
+ }
32
+
33
+ size_t countRebelFleet () {
34
+ size_t rebelStarshipsNumber = 0 ;
35
+ for (auto unit : rebelStarships) {
36
+ rebelStarshipsNumber += unit->getUnitsNumber ();
37
+ }
38
+ return rebelStarshipsNumber;
39
+ }
40
+
41
+ void tick (Time timeStep) {
42
+ if (countImperialFleet () == 0 && countRebelFleet () == 0 ) {
43
+ std::cout << " DRAW" << std::endl;
44
+ } else if (countImperialFleet () == 0 ) {
45
+ std::cout << " REBELLION WON" << std::endl;
46
+ } else if (countRebelFleet () == 0 ) {
47
+ std::cout << " IMPERIUM WON" << std::endl;
48
+ }
49
+ if (timer->isAttackTime ()) {
50
+ for (auto imperialStarship : imperialStarships) {
51
+ for (auto rebelStarship : rebelStarships) {
52
+ if (!imperialStarship->isDestroyed () && !rebelStarship->isDestroyed ()) {
53
+ rebelStarship->receiveAttack (imperialStarship.get ());
54
+ }
55
+ }
56
+ }
57
+ }
58
+ timer->moveTime (timeStep);
59
+ }
60
+
61
+ private:
62
+ Timer *timer;
63
+ std::vector<std::shared_ptr<RebelStarship>> rebelStarships;
64
+ std::vector<std::shared_ptr<ImperialStarship>> imperialStarships;
65
+ };
66
+
67
+ class SpaceBattle ::Builder {
68
+ public:
69
+ Builder &startTime (Time t) {
70
+ t0 = t;
71
+ return *this ;
72
+ }
73
+
74
+ Builder &maxTime (Time t) {
75
+ t1 = t;
76
+ return *this ;
77
+ }
78
+
79
+ Builder &ship (std::shared_ptr<RebelStarship> ship) {
80
+ rebelStarships.push_back (ship);
81
+ return *this ;
82
+ }
83
+
84
+ Builder &ship (std::shared_ptr<ImperialStarship> ship) {
85
+ imperialStarships.push_back (ship);
86
+ return *this ;
87
+ }
88
+
89
+ SpaceBattle build () const {
90
+ return SpaceBattle (t0, t1, rebelStarships, imperialStarships);
91
+ }
92
+
93
+ private:
94
+ Time t0 = 0 ;
95
+ Time t1 = 1 ;
96
+ std::vector<std::shared_ptr<RebelStarship>> rebelStarships;
97
+ std::vector<std::shared_ptr<ImperialStarship>> imperialStarships;
98
+ };
99
+
100
+ #endif // BATTLE_H
0 commit comments