fork download
  1. //CritterModel.h
  2.  
  3. #ifndef __CritterModelObserver__CritterModel__
  4. #define __CritterModelObserver__CritterModel__
  5.  
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10.  
  11. //IObserver.h
  12.  
  13. #ifndef CritterModelObserver_IObserver_h
  14. #define CritterModelObserver_IObserver_h
  15.  
  16. class IObserver {
  17. public:
  18. virtual void update() = 0;
  19. };
  20.  
  21. #endif
  22.  
  23. //Observable.h
  24.  
  25. #ifndef __CritterModelObserver__Observable__
  26. #define __CritterModelObserver__Observable__
  27.  
  28. #include <stdio.h>
  29.  
  30. class Observable {
  31. public:
  32. Observable();
  33. virtual ~Observable();
  34. void addObserver(IObserver* ob);
  35. void removeObserver(IObserver* ob);
  36.  
  37. protected:
  38. void notify() const;
  39.  
  40. private:
  41. IObserver** observers;
  42. static const int maxObservers;
  43. int numObservers;
  44. };
  45.  
  46. #endif /* defined(__CritterModelObserver__Observable__) */
  47.  
  48. class CritterModel : public Observable {
  49.  
  50. public:
  51. CritterModel();
  52. virtual ~CritterModel();
  53. CritterModel(int x, int y);
  54.  
  55. // Getters
  56. inline int getReward() const { return reward; }
  57. inline int getHitPoint() const { return hitPoint; }
  58. inline int getStrength() const { return strength; }
  59. inline int getSpeeed() const { return speed; }
  60. inline int getLevel() const { return level; }
  61. inline int getXCoord() const { return x; }
  62. inline int getYCoord() const { return y; }
  63. inline int getDuration() const { return duration; }
  64. // Setters
  65. void setLevel(int l) {
  66. level = l;
  67. }
  68.  
  69. void setStrength(int s) {
  70. strength = s;
  71. }
  72.  
  73. void setHitPoint(int h) {
  74. hitPoint = h;
  75. }
  76. /**
  77.   void setCellType(int c) {
  78.   cellType = c;
  79.   }
  80.   */
  81. void setSpeed(int s) {
  82. speed = s;
  83. }
  84.  
  85. void setReward(int r) {
  86. reward = r;
  87. }
  88.  
  89. void setXCoord(int xC) {
  90. x = xC;
  91. }
  92.  
  93. void setYCoord(int yC) {
  94. y = yC;
  95. }
  96.  
  97. void setDuration(int d) {
  98. duration = d;
  99. }
  100.  
  101. // Other methods
  102. int generateCritters(int);
  103. std::string getCell();
  104. void printCritters();
  105. void moveCritter(int, int); // Undefined at the moment.
  106. void shoot();
  107. void timer();
  108.  
  109. private:
  110. int reward;
  111. int hitPoint;
  112. int strength;
  113. int speed;
  114. int level;
  115. int duration;
  116. //int cellType;
  117. int x,y;
  118. //Cell cell;
  119.  
  120.  
  121. };
  122.  
  123. #endif /* defined(__CritterModelObserver__CritterModel__) */
  124.  
  125.  
  126. //CritterModel.cpp
  127. #include <iostream>
  128. #include <time.h>
  129. #include <vector>
  130.  
  131. using namespace std;
  132.  
  133. CritterModel::CritterModel() : Observable() {
  134. reward = 0;
  135. hitPoint = 0;
  136. strength = 0;
  137. speed = 0;
  138. level = 0;
  139. y = 0;
  140. x = 0;
  141. }
  142.  
  143. CritterModel::~CritterModel() {
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150. // Generate Critters
  151. CritterModel* generateCritters(int numCritters) {
  152. CritterModel* critters = new CritterModel[numCritters];
  153.  
  154. for (int i=0; i < numCritters; i++) {
  155. critters[i] = CritterModel();
  156. }
  157. return critters;
  158. }
  159.  
  160.  
  161. // Move those critters
  162. void CritterModel::moveCritter(int xCoord, int yCoord) {
  163. bool flag = false;
  164. if (this->x != xCoord) {
  165. x = xCoord;
  166. flag = true;
  167. }
  168.  
  169. if (this->y != yCoord) {
  170. y = yCoord;
  171. flag = true;
  172. }
  173.  
  174. if (flag) {
  175. notify();
  176. }
  177. }
  178.  
  179.  
  180. //Observable.cpp
  181. #include <iostream>
  182.  
  183. const int Observable::maxObservers = 5;
  184.  
  185. Observable::Observable() {
  186. observers = new IObserver* [maxObservers];
  187. for (int i=0; i < maxObservers; i++) {
  188. observers[i] = NULL;
  189. numObservers = 0;
  190. }
  191. }
  192. // Destructor
  193. Observable::~Observable() {
  194. delete[] observers;
  195. }
  196.  
  197. // Add observer method
  198. void Observable::addObserver(IObserver* ob) {
  199. // Check if full
  200. if (numObservers == maxObservers) {
  201. return;
  202. }
  203. // Check if observer exists
  204. for (int i=0; i < maxObservers; i++) {
  205. if (observers[i] == ob) {
  206. return;
  207. }
  208. }
  209.  
  210. // Otherwise add the observer
  211. observers[numObservers] = ob;
  212. ++numObservers;
  213. }
  214.  
  215. void Observable::removeObserver(IObserver* ob) {
  216. // ommitted
  217. }
  218.  
  219. void Observable::notify() const {
  220. for (int i=0; i < numObservers; i++) {
  221. observers[i]->update();
  222. }
  223. }
  224.  
  225. //CritterDisplay.h
  226.  
  227. #ifndef __CritterModelObserver__CritterDisplay__
  228. #define __CritterModelObserver__CritterDisplay__
  229.  
  230. #include <stdio.h>
  231.  
  232. class CritterDisplay: public IObserver {
  233.  
  234. public:
  235. CritterDisplay(CritterModel* critterModel);
  236. virtual ~CritterDisplay();
  237. void update();
  238.  
  239. private:
  240. CritterModel* critterModel;
  241. };
  242.  
  243. #endif /* defined(__CritterModelObserver__CritterDisplay__) */
  244.  
  245. //CritterDisplay.cpp
  246. #include <iostream>
  247.  
  248. using namespace std;
  249.  
  250. CritterDisplay::CritterDisplay(CritterModel* critterModel) {
  251. this->critterModel = critterModel;
  252. }
  253.  
  254. CritterDisplay::~CritterDisplay() {}
  255.  
  256. void CritterDisplay::update() {
  257.  
  258. cout <<
  259. "================" << endl <<
  260. "Position: " << "(" << critterModel->getXCoord() << ", " << critterModel->getYCoord() << ")" << endl <<
  261. "Speed: " << critterModel->getSpeeed() << endl <<
  262. "Strength: " << critterModel->getStrength() << endl <<
  263. "Level: " << critterModel->getLevel() << endl <<
  264. "Hit Points: " << critterModel->getHitPoint() << endl <<
  265. "Reward: " << critterModel->getReward() << endl << "\n";
  266.  
  267.  
  268. }
  269.  
  270.  
  271. //Driver.cpp
  272.  
  273.  
  274. #include <iostream>
  275. #include <time.h>
  276. #include <unistd.h>
  277. #include <limits>
  278.  
  279. using namespace std;
  280.  
  281. void PressEnterToBegin()
  282. {
  283. std::cout << "Press ENTER to begin..."; //<< &fflush;
  284. std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  285. }
  286.  
  287. int main(int argc, const char * argv[]) {
  288.  
  289. double dif = 0.0;
  290. bool flag = true;
  291. int x,y = 0;
  292.  
  293. // Initialize players coin
  294. int pCoin = 100;
  295.  
  296. // Initialize coin steal
  297. int stealCoin = 0;
  298.  
  299. // Initialize reward
  300. int reward = 0;
  301.  
  302. //CritterModel* critters;
  303. CritterModel* critterModel = new CritterModel();
  304.  
  305. //CritterModel* critters = CritterModel::generateCritters(5);
  306. //critters->generateCritters(5);
  307.  
  308. CritterDisplay* display = new CritterDisplay(critterModel);
  309.  
  310. critterModel->addObserver(display);
  311. critterModel->setSpeed(2);
  312. critterModel->setStrength(10);
  313. critterModel->setLevel(1);
  314. critterModel->setHitPoint(100);
  315. critterModel->setReward(300);
  316.  
  317. // Welcome message
  318. std::cout << "Welcome to the Critter game!\n" << "You have $100 in your bank to begin with. If a Critter happens to get to the exit, \nit steals coin from your bank proportional to the Critters strength.\n\n" << "Game play: \n1.) Game starts by a Critter wave being ready to enter the map.\n2.) Press ENTER to begin.\n3.) After the Critters enter, you will be notified that a timer will start.\n4.) Fire by pressing ENTER before time runs out and Critters exit the map.\n " << "---------------------------------------------------------------------------\n\n";
  319.  
  320. PressEnterToBegin();
  321.  
  322. // Start timed loop
  323. time_t start, end;
  324. time(&start);
  325. do {
  326.  
  327. // End simulation after dif > 2
  328. if (dif > 2) {
  329. flag = false;
  330. }
  331.  
  332. // Otherwise move critter to next cell
  333. critterModel->moveCritter(x, y);
  334. ++x;
  335. ++y;
  336.  
  337. time(&end);
  338. dif = difftime(end, start);
  339. cout << dif << endl;
  340.  
  341. // Counter to simulate speed of critter. A critter moves every 2 seconds and the sleep method stops time which keeps dif < 2 in the above if-statment
  342. if (x > 1 || y > 1) {
  343. sleep(2);
  344. }
  345.  
  346. // If critter coordinates are at the exit, steal coin from player
  347. if (critterModel->getXCoord() == 4 && critterModel->getYCoord() == 4) {
  348. stealCoin = stealCoin + critterModel->getStrength();
  349. pCoin = pCoin - stealCoin;
  350. std::cout << "A critter got through! You have $" << pCoin << " left in your bank.\n\n";
  351. }
  352.  
  353. }while (flag);
  354.  
  355.  
  356.  
  357. delete display;
  358. delete critterModel;
  359.  
  360. return 0;
  361.  
  362.  
  363. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Welcome to the Critter game!
You have $100 in your bank to begin with. If a Critter happens to get to the exit, 
it steals coin from your bank proportional to the Critters strength.

Game play: 
1.) Game starts by a Critter wave being ready to enter the map.
2.) Press ENTER to begin.
3.) After the Critters enter, you will be notified that a timer will start.
4.) Fire by pressing ENTER before time runs out and Critters exit the map.
 ---------------------------------------------------------------------------

Press ENTER to begin...0
================
Position: (1, 1)
Speed: 2
Strength: 10
Level: 1
Hit Points: 100
Reward: 300

0
================
Position: (2, 2)
Speed: 2
Strength: 10
Level: 1
Hit Points: 100
Reward: 300

2
================
Position: (3, 3)
Speed: 2
Strength: 10
Level: 1
Hit Points: 100
Reward: 300

4
================
Position: (4, 4)
Speed: 2
Strength: 10
Level: 1
Hit Points: 100
Reward: 300

6
A critter got through! You have $90 left in your bank.