Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.05 KB | None | 0 0
  1. //
  2. // Created by Julia Sanguinetti on 2019-10-16.
  3. //
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <cstdlib>
  8. #include <time.h>
  9. using namespace std;
  10. // Constants
  11. const int WORLDSIZE = 20;
  12. const int INITIAL_ANTS = 20;
  13. const int INITIAL_DOODLEBUGS = 5;
  14. const int DOODLEBUG = 1;
  15. const int ANT = 2;
  16. const int ANT_BREED = 3;
  17. const int DOODLE_BREED = 8;
  18. const int DOODLE_STARVE =3;
  19.  
  20. //Declarations of classes
  21. class Organism;
  22. class Doodlebug;
  23. class Ant;
  24.  
  25. //World class
  26. class World {
  27. friend class Organism;
  28. friend class Doodlebug;
  29. friend class Ant;
  30. public:
  31. //Constructor
  32. World ();
  33. //Destructor
  34. ~World();
  35. Organism* getAt (int x, int y);
  36. void setAt(int x, int y, Organism *org);
  37. void display();
  38. void oneTurn ();
  39. private:
  40. Organism* grid [WORLDSIZE] [WORLDSIZE];
  41. };
  42.  
  43. // Definition for the Organism base class
  44. class Organism {
  45. friend class World;
  46. public:
  47. //Constructors
  48. Organism ();
  49. Organism (World *wrld, int x, int y);
  50. //Destructor
  51. virtual ~Organism() = default;
  52. //Methods
  53. virtual void breed () = 0;
  54. virtual void move () = 0;
  55. virtual int getType () = 0;
  56. virtual bool starve ()= 0;
  57. protected:
  58. int x,y;
  59. bool moved;
  60. int breedCount;
  61. World *world;
  62. };
  63.  
  64. //Constructor definition
  65. World::World (){
  66. int i,j;
  67. for (i=0; i<WORLDSIZE;i++){
  68. for (j=0; j<WORLDSIZE;j++)
  69. grid [i][j] = nullptr;
  70. }
  71. }
  72. // Destructor definition
  73. World::~World () {
  74. //Release any allocated memory
  75. for (int i = 0; i < WORLDSIZE; i++) {
  76. for (int j = 0; j < WORLDSIZE; j++) {
  77. if (grid[i][j] != nullptr)
  78. delete (grid[i][j]);
  79. }
  80. }
  81. }
  82.  
  83. // Member function definitions
  84.  
  85. Organism* World::getAt(int x, int y) {
  86. if ((x >= 0) && (x < WORLDSIZE) && (y >= 0) && (y < WORLDSIZE))
  87. return grid[x][y];
  88. return nullptr;
  89. }
  90.  
  91. // Method definition of setAt
  92. void World::setAt(int x, int y, Organism *org) {
  93. if ((x>=0) && (x<WORLDSIZE) && (y>=0) && (y<WORLDSIZE))
  94. grid [x][y] = org;
  95. }
  96.  
  97. //Method definition of display
  98. void World::display() {
  99. for (int j = 0; j<WORLDSIZE;j++){
  100. for (int i=0;i<WORLDSIZE;i++){
  101. if (grid[i][j]== nullptr)
  102. cout<<'-';
  103. else if (grid [i][j]->getType() == ANT)
  104. cout<<'o';
  105. else
  106. cout << "X";
  107. }
  108. cout<<endl;
  109. }
  110. }
  111. // Method definition of oneTurn
  112. void World::oneTurn(){
  113. //First reset all organism to not move
  114. for (int i =0; i<WORLDSIZE;i++)
  115. for (int j=0;j<WORLDSIZE;j++){
  116. if (grid [i][j] != nullptr)
  117. grid [i][j] ->moved = false;
  118. }
  119. //Loop through cells in order and move if its Doodlebug
  120. for (int i=0; i<WORLDSIZE;i++)
  121. for (int j=0;j<WORLDSIZE;j++){
  122. if ((grid [i][j])!=nullptr) && (grid[i][j]->getType() == DOODLEBUG)){
  123. if (!grid[i][j]->moved) {
  124. grid[i][j]->moved = true;
  125. grid[i][j]->move();
  126. }
  127. }
  128. }
  129.  
  130.  
  131. //Loop through cells in order and move if its an Ant
  132. for (int i=0;i<WORLDSIZE;i++)
  133. for (int j=0;j<WORLDSIZE;j++){
  134. if ((grid [i][j] != nullptr) && (grid[i][j]->getType()==ANT)){
  135. if (!grid [i][j] -> moved){
  136. grid[i][j]->moved = true;
  137. grid [i][j]->move();
  138. }
  139. }
  140. }
  141.  
  142. // Kill off any doodlebugs that haven't eaten
  143. for (int i=0; i<WORLDSIZE;i++)
  144. for (int j=0;j<WORLDSIZE;j++){
  145. if ((grid [i][j])!= nullptr) && (grid[i][j]->getType() == DOODLEBUG)){
  146. if (grid[i][j]->starve()) {
  147. delete (grid[i][j]);
  148. grid[i][j] = nullptr;
  149. }
  150. }
  151. }
  152.  
  153. // Check if organism should breed
  154. for (int i=0; i<WORLDSIZE;i++)
  155. for (int j=0; j<WORLDSIZE;j++){
  156. if ((grid [i][j] != nullptr) && grid [i][j]->moved)
  157. grid [i][j]->breed();
  158. }
  159. }
  160. // Default constructor of Organism
  161. Organism::Organism() {
  162. world = nullptr;
  163. moved = false;
  164. breedCount = 0;
  165. x=0;
  166. y=0;
  167. }
  168.  
  169. // Constructor with parameters
  170. Organism::Organism(World *wrld, int x, int y) {
  171. this->world = wrld;
  172. moved = false;
  173. breedCount = 0;
  174. this->x=x;
  175. this->y=y;
  176. wrld->setAt(x,y,this);
  177. }
  178. // Ant class
  179.  
  180. class Ant:public Organism{
  181. public:
  182. friend class World;
  183. //Constructors
  184. Ant() = default;
  185. Ant (World *wrld,int x, int y):Organism (wrld,x,y){};
  186. //Methods
  187. void breed () override;
  188. void move () override;
  189. int getType () override;
  190. bool starve () override {
  191. return false;
  192. }
  193. };
  194. // Method definition of move
  195. void Ant::move() {
  196. //Pick random direction to move
  197. int dir = rand() % 4;
  198. //Try to move up, if not at an edge and empty spot
  199. if (dir == 0) {
  200. if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
  201. world->setAt(x, y - 1, world->getAt(x, y));
  202. //Move to new spot
  203. worl->setAt(x, y, nullptr);
  204. y--;
  205. }
  206. }
  207. //Try to move down
  208. else if (dir == 1) {
  209. if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
  210. world->setAt(x, y - 1, world->getAt(x, y));
  211. //Move to new spot
  212. world->setAt(x, y, nullptr);
  213. y++;
  214. }
  215. }
  216. //Try to move left
  217. else if (dir == 2) {
  218. if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
  219. world->setAt(x - 1, y, world->getAt(x, y));
  220. //Move to new spot
  221. world->setAt(x, y, nullptr);
  222. x--;
  223. }
  224. }
  225. //Try to move right
  226. else {
  227. if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) == nullptr)) {
  228. world->setAt(x + 1, y, world->getAt(x, y));
  229. //Move to new spot
  230. world->setAt(x, y, nullptr);
  231. x++;
  232. }
  233. }
  234. }
  235.  
  236. //Method definition of getType
  237. int Ant::getType() {
  238. return ANT;
  239. }
  240.  
  241. // Method definition of breed
  242. void Ant::breed() {
  243. breedCount++;
  244. if (breedCount ==ANT_BREED){
  245. breedCount = 0;
  246. //Try to make a new any either above, left, right, below
  247. if ((y>0) && (world->getAt(x,y-1) == nullptr))
  248. auto *newAnt = new Ant (world,x,y-1);
  249. else if ((y<WORLDSIZE-1) && (world->getAt(x,y+1) == nullptr))
  250. auto *newAnt = new Ant (world,x, y + 1);
  251. else if ((x>0) && (world->getAt(x-1,y) == nullptr))
  252. auto *newAnt = new Ant (world, x -1 ,y);
  253. else if ((x<WORLDSIZE -1) && (world->getAt(x+1,y)== nullptr))
  254. auto *newAnt = new Ant (world, x+1, y);
  255. }
  256. }
  257. // Create a class Doodlebug
  258. class Doodlebug:public Organism {
  259. public:
  260. friend class World;
  261. public:
  262. Doodlebug ();
  263. Doodlebug (World *world, int x, int y);
  264. void breed () override;
  265. void move () override;
  266. int getType() override;
  267. bool starve () override;
  268. private:
  269. int starveTricks;
  270. };
  271. // Default constructor
  272. Doodlebug::Doodlebug():Organism () {
  273. starveTricks = 0;
  274. }
  275.  
  276. //Constructor with parameters
  277. Doodlebug::Doodlebug(World *world, int x, int y)
  278. : Organism(world, x, y) {
  279. starveTricks = 0;
  280. }
  281. // Member functions
  282. void Doodlebug::move() {
  283. if ((y > 0) && (world->getAt(x, y - 1) != nullptr) && (world->getAt(x, y - 1)->getType() == ANT)) {
  284. delete (world->grid[x][y - 1]);
  285. world->grid[x][y - 1] = this;
  286. world->setAt(x, y, nullptr);
  287. starveTricks = 0;
  288. y--;
  289. return;
  290. }
  291. else if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) != nullptr) && (world->getAt(x, y + 1)->getType() == ANT)) {
  292. delete (world->grid[x][y + 1]);
  293. world->grid[x][y + 1] = this;
  294. world->setAt(x, y, nullptr);
  295. starveTricks = 0;
  296. y++;
  297. return;
  298. }
  299. else if ((x > 0) && (world->getAt(x - 1, y) != nullptr) && (world->getAt(x - 1, y)->getType() == ANT)) {
  300. delete(world->grid[x - 1][y]);
  301. world->grid[x - 1][y] = this;
  302. world->setAt(x, y, nullptr);
  303. starveTricks = 0;
  304. x--;
  305. return;
  306. }
  307. else if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) != nullptr) && (world->getAt(x + 1, y)->getType() == ANT)) {
  308. delete(world->grid[x + 1][y]);
  309. world->grid[x + 1][y] = this;
  310. world->setAt(x, y, nullptr);
  311. starveTricks = 0;
  312. x++;
  313. return;
  314. }
  315. int dir = rand() % 4;
  316. // Try to move up, if not at an edge and empty spot
  317. if (dir == 0) {
  318. if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
  319. // Move to new spot
  320. world->setAt(x, y - 1, world->getAt(x, y));
  321. // Set current spot to empty
  322. world->setAt(x, y, nullptr);
  323. y--;
  324. }
  325. }
  326. // Try to move down
  327. else if (dir == 1) {
  328. if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
  329. // Move to new spot
  330. world->setAt(x, y + 1, world->getAt(x, y));
  331. // Set current spot to empty
  332. world->setAt(x, y, nullptr);
  333. y++;
  334. }
  335. }
  336. // Try to move left
  337. else if (dir == 2) {
  338. if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
  339. // Move to new spot
  340. world->setAt(x - 1, y, world->getAt(x, y));
  341. // Set current spot to empty
  342. world->setAt(x, y, nullptr);
  343. x--;
  344. }
  345. }
  346. // Try to move right
  347. else {
  348. if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) == nullptr)) {
  349. // Move to new spot
  350. world->setAt(x + 1, y, world->getAt(x, y));
  351. // Set current spot to empty
  352. world->setAt(x, y, nullptr);
  353. x++;
  354. }
  355. }
  356. // Increment starve tick since didn't eat on this turn
  357. starveTricks++;
  358. }
  359. // Method definition of getType
  360. int Doodlebug::getType() {
  361. return DOODLEBUG;
  362. }
  363.  
  364. // Method definition of breed
  365. void Doodlebug::breed() {
  366. breedCount++;
  367. if (breedCount == DOODLE_BREED) {
  368. breedCount = 0;
  369. // Try to make a new ant either above, left, right, or down
  370. if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
  371. auto *newDoodle = new Doodlebug(world, x, y - 1);
  372. }
  373. else if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
  374. auto *newDoodle = new Doodlebug(world, x, y + 1);
  375. }
  376. else if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
  377. auto *newDoodle = new Doodlebug(world, x - 1, y);
  378. }
  379. else {
  380. auto *newDoodle = new Doodlebug(world, x + 1, y);
  381. }
  382. }
  383. }
  384. // Method definition of starve
  385. bool Doodlebug::starve() {
  386. // Starve if no food eaten in last DOODLE_STARVE time ticks
  387. return starveTricks > DOODLE_STARVE;
  388. }
  389.  
  390. int main (){
  391. // User answer
  392. char userInput = 'N';
  393. // Seed random number generator
  394. srand(time(nullptr));
  395. World w;
  396. // Randomly create 100 ants
  397. int antCount = 0;
  398. while (antCount < INITIAL_ANTS) {
  399. int x = rand() % WORLDSIZE;
  400. int y = rand() % WORLDSIZE;
  401. // Check for empty spots
  402. if (w.getAt(x, y) == nullptr) {
  403. auto *a = new Ant(&w, x, y);
  404. antCount++;
  405. }
  406. }
  407. // Randomly create 5 doodlebugs
  408. int doodleCount = 0;
  409. while (doodleCount < INITIAL_DOODLEBUGS) {
  410. int x = rand() % WORLDSIZE;
  411. int y = rand() % WORLDSIZE;
  412. // Check for empty spots
  413. if (w.getAt(x, y) == nullptr) {
  414. auto *d = new Doodlebug(&w, x, y);
  415. doodleCount++;
  416. }
  417. }
  418. // Run simulation until user cancels
  419. do {w.display();
  420. w.oneTurn();
  421. std::cout << std::endl << "Would you like to run another step?\n";
  422. std::cin>> userInput;
  423. } while ((userInput == 'Y') || (userInput == 'y'));
  424. return 0;
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement