fork download
  1. #include <iostream>
  2.  
  3. class Wheel {
  4. private:
  5. int resource = 0;
  6.  
  7. public:
  8. Wheel(int resource) {
  9. this->resource = resource;
  10. }
  11.  
  12. void use(int time) {
  13. this->resource -= time;
  14. }
  15.  
  16. int getResource() {
  17. return resource;
  18. }
  19. };
  20.  
  21.  
  22. #define CAR_WHEELS 4
  23.  
  24. class Car {
  25. private:
  26. Wheel* wheels[CAR_WHEELS];
  27. Wheel* trunk;
  28.  
  29. public:
  30. Car() {
  31. for(int i = 0; i < CAR_WHEELS; i++) {
  32. wheels[i] = nullptr;
  33. }
  34. trunk = nullptr;
  35. }
  36.  
  37. void setWheel(Wheel* wheel, int place) {
  38. if (place > CAR_WHEELS - 1 || place < 0) return;
  39. wheels[place] = wheel;
  40. }
  41.  
  42. void setTrunk(Wheel* wheel) {
  43. this->trunk = wheel;
  44. }
  45.  
  46. void go(int time) {
  47. for(int i = 0; i < CAR_WHEELS; i++) {
  48. wheels[i]->use(time);
  49. }
  50.  
  51. std::cout << "go " << time << std::endl;
  52. this->info();
  53. }
  54.  
  55. void replaceWheelFromTrunk(int place) {
  56. if (place > CAR_WHEELS - 1 || place < 0) return;
  57. Wheel* temp = wheels[place];
  58. wheels[place] = trunk;
  59. trunk = temp;
  60.  
  61. std::cout << "trunk <-> place " << place << std::endl;
  62. this->info();
  63. }
  64.  
  65. void info() {
  66. for(int i = 0; i < CAR_WHEELS; i++) {
  67. std::cout << "P" << i << ": " << wheels[i]->getResource() << " ";
  68. }
  69.  
  70. std::cout << "T" << ": " << trunk->getResource() << " " << std::endl;
  71. std::cout << std::endl;
  72. }
  73.  
  74. };
  75.  
  76. class CarBuilder {
  77. public:
  78. Car* build() {
  79. Car* car = new Car();
  80. Wheel* wheel;
  81. int resource = 100;
  82.  
  83. for(int i = 0; i < 4; i++) {
  84. wheel = new Wheel(resource);
  85. car->setWheel(wheel, i);
  86. }
  87.  
  88. wheel = new Wheel(resource);
  89. car->setTrunk(wheel);
  90.  
  91. return car;
  92. }
  93. };
  94.  
  95. class DriverStrategy {
  96. public:
  97. void execute(Car* car) {
  98.  
  99. const int STEPS = 4;
  100.  
  101. int steps[STEPS][2] = {
  102. {25, 0},
  103. {25, 1},
  104. {25, 2},
  105. {25, 3},
  106. };
  107.  
  108. for(int i = 0; i < STEPS; i++) {
  109. car->go(steps[i][0]);
  110. car->replaceWheelFromTrunk(steps[i][1]);
  111. }
  112.  
  113. car->go(25);
  114. }
  115. };
  116.  
  117. class Driver {
  118. private:
  119. Car* car;
  120. DriverStrategy* strategy;
  121. //Road road;
  122.  
  123. public:
  124. void go() {
  125. strategy->execute(this->car);
  126. }
  127.  
  128. void setStrategy(DriverStrategy* strategy) {
  129. this->strategy = strategy;
  130. }
  131.  
  132. void jumpInCar(Car* car) {
  133. this->car = car;
  134. }
  135. };
  136.  
  137.  
  138. int main(int argc, char* argv[]) {
  139. CarBuilder builder;
  140. Car* mycar = builder.build();
  141.  
  142. Driver driver;
  143. DriverStrategy* strategy = new DriverStrategy();
  144. driver.setStrategy(strategy);
  145. driver.jumpInCar(mycar);
  146.  
  147. mycar->info();
  148. driver.go();
  149.  
  150. return 0;
  151. }
  152.  
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
P0: 100 P1: 100 P2: 100 P3: 100 T: 100 

go 25
P0: 75 P1: 75 P2: 75 P3: 75 T: 100 

trunk <-> place 0
P0: 100 P1: 75 P2: 75 P3: 75 T: 75 

go 25
P0: 75 P1: 50 P2: 50 P3: 50 T: 75 

trunk <-> place 1
P0: 75 P1: 75 P2: 50 P3: 50 T: 50 

go 25
P0: 50 P1: 50 P2: 25 P3: 25 T: 50 

trunk <-> place 2
P0: 50 P1: 50 P2: 50 P3: 25 T: 25 

go 25
P0: 25 P1: 25 P2: 25 P3: 0 T: 25 

trunk <-> place 3
P0: 25 P1: 25 P2: 25 P3: 25 T: 0 

go 25
P0: 0 P1: 0 P2: 0 P3: 0 T: 0