stochastic simulation, predator/prey

Dependencies:   mbed

Committer:
manitou
Date:
Mon Dec 23 18:56:56 2019 +0000
Revision:
0:fc1335b7b54f
stochastic simulation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manitou 0:fc1335b7b54f 1 //#include <iostream>
manitou 0:fc1335b7b54f 2 #include "Organism.h"
manitou 0:fc1335b7b54f 3 #include "World.h"
manitou 0:fc1335b7b54f 4
manitou 0:fc1335b7b54f 5 // Create an organism at the given coordinates in the given world.
manitou 0:fc1335b7b54f 6 Organism::Organism(World* aWorld, int xcoord, int ycoord) {
manitou 0:fc1335b7b54f 7 world = aWorld;
manitou 0:fc1335b7b54f 8 x = xcoord;
manitou 0:fc1335b7b54f 9 y = ycoord;
manitou 0:fc1335b7b54f 10 breedTicks = 0;
manitou 0:fc1335b7b54f 11 moved = false;
manitou 0:fc1335b7b54f 12 world->setAt(x, y, this);
manitou 0:fc1335b7b54f 13 }
manitou 0:fc1335b7b54f 14
manitou 0:fc1335b7b54f 15 // flags the organism as moved or not
manitou 0:fc1335b7b54f 16 void Organism::setMoved(bool hasMoved) {
manitou 0:fc1335b7b54f 17 moved = hasMoved;
manitou 0:fc1335b7b54f 18 }
manitou 0:fc1335b7b54f 19
manitou 0:fc1335b7b54f 20 // has the organism moved or not?
manitou 0:fc1335b7b54f 21 bool Organism::hasMoved() const {
manitou 0:fc1335b7b54f 22 return moved;
manitou 0:fc1335b7b54f 23 }
manitou 0:fc1335b7b54f 24
manitou 0:fc1335b7b54f 25 // moves the organism from coordinates (x,y) to (xNew,yNew)
manitou 0:fc1335b7b54f 26 void Organism::movesTo(int xNew, int yNew) {
manitou 0:fc1335b7b54f 27
manitou 0:fc1335b7b54f 28 world->setAt(xNew, yNew, world->getAt(x, y));
manitou 0:fc1335b7b54f 29
manitou 0:fc1335b7b54f 30 world->setAt(x, y, NULL);
manitou 0:fc1335b7b54f 31
manitou 0:fc1335b7b54f 32 x = xNew;
manitou 0:fc1335b7b54f 33 y = yNew;
manitou 0:fc1335b7b54f 34
manitou 0:fc1335b7b54f 35 world->getAt(x, y)->setMoved(true);
manitou 0:fc1335b7b54f 36 }
manitou 0:fc1335b7b54f 37
manitou 0:fc1335b7b54f 38 // Breeds an organism at an adjacent cell. This method calls the
manitou 0:fc1335b7b54f 39 // generateOffspring() method.
manitou 0:fc1335b7b54f 40 void Organism::breedAtAdjacentCell() {
manitou 0:fc1335b7b54f 41 #if 1
manitou 0:fc1335b7b54f 42 // find empty nabor for breeding
manitou 0:fc1335b7b54f 43 for (int i = 0; i < NABORS; i++) {
manitou 0:fc1335b7b54f 44 switch (nabors[i]) {
manitou 0:fc1335b7b54f 45 case 0:
manitou 0:fc1335b7b54f 46 if ((world->getAt(x, y + 1) == NULL) && in_range(x, y + 1))
manitou 0:fc1335b7b54f 47 {
manitou 0:fc1335b7b54f 48 generateOffspring(x, y + 1);
manitou 0:fc1335b7b54f 49 return;
manitou 0:fc1335b7b54f 50 }
manitou 0:fc1335b7b54f 51 break;
manitou 0:fc1335b7b54f 52 case 1:
manitou 0:fc1335b7b54f 53 if ((world->getAt(x, y - 1) == NULL) && in_range(x, y - 1))
manitou 0:fc1335b7b54f 54 {
manitou 0:fc1335b7b54f 55 generateOffspring(x, y - 1);
manitou 0:fc1335b7b54f 56 return;
manitou 0:fc1335b7b54f 57 }
manitou 0:fc1335b7b54f 58 break;
manitou 0:fc1335b7b54f 59 case 2:
manitou 0:fc1335b7b54f 60 if ((world->getAt(x - 1, y) == NULL) && in_range(x - 1, y))
manitou 0:fc1335b7b54f 61 {
manitou 0:fc1335b7b54f 62 generateOffspring(x - 1, y);
manitou 0:fc1335b7b54f 63 return;
manitou 0:fc1335b7b54f 64 }
manitou 0:fc1335b7b54f 65 case 3:
manitou 0:fc1335b7b54f 66 if ((world->getAt(x + 1, y) == NULL) && in_range(x + 1, y))
manitou 0:fc1335b7b54f 67 {
manitou 0:fc1335b7b54f 68 generateOffspring(x + 1, y);
manitou 0:fc1335b7b54f 69 return;
manitou 0:fc1335b7b54f 70 }
manitou 0:fc1335b7b54f 71 break;
manitou 0:fc1335b7b54f 72 default:
manitou 0:fc1335b7b54f 73 break;
manitou 0:fc1335b7b54f 74 }
manitou 0:fc1335b7b54f 75 } // nabor for
manitou 0:fc1335b7b54f 76 #else
manitou 0:fc1335b7b54f 77 if ((world->getAt(x, y + 1) == NULL) && in_range(x, y + 1))
manitou 0:fc1335b7b54f 78 {
manitou 0:fc1335b7b54f 79 generateOffspring(x, y + 1);
manitou 0:fc1335b7b54f 80 }
manitou 0:fc1335b7b54f 81 else if ((world->getAt(x, y - 1) == NULL) && in_range(x, y - 1))
manitou 0:fc1335b7b54f 82 {
manitou 0:fc1335b7b54f 83 generateOffspring(x, y - 1);
manitou 0:fc1335b7b54f 84 }
manitou 0:fc1335b7b54f 85 else if ((world->getAt(x - 1, y) == NULL) && in_range(x - 1, y))
manitou 0:fc1335b7b54f 86 {
manitou 0:fc1335b7b54f 87 generateOffspring(x - 1, y);
manitou 0:fc1335b7b54f 88 }
manitou 0:fc1335b7b54f 89 else if ((world->getAt(x + 1, y) == NULL) && in_range(x + 1, y))
manitou 0:fc1335b7b54f 90 {
manitou 0:fc1335b7b54f 91 generateOffspring(x + 1, y);
manitou 0:fc1335b7b54f 92 }
manitou 0:fc1335b7b54f 93 #endif
manitou 0:fc1335b7b54f 94 }
manitou 0:fc1335b7b54f 95
manitou 0:fc1335b7b54f 96
manitou 0:fc1335b7b54f 97 bool Organism::in_range(int xx, int yy)
manitou 0:fc1335b7b54f 98 {
manitou 0:fc1335b7b54f 99 return (xx >= 0) && (xx < ROWS) && (yy >= 0) && (yy < COLS);
manitou 0:fc1335b7b54f 100 }
manitou 0:fc1335b7b54f 101 // Returns true if organism is dead, false otherwise.
manitou 0:fc1335b7b54f 102 bool Organism::isDead() const {
manitou 0:fc1335b7b54f 103 return false;
manitou 0:fc1335b7b54f 104 }