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 //
manitou 0:fc1335b7b54f 2 // Bug.cpp
manitou 0:fc1335b7b54f 3 // INHERITANCE_AND_POLYMORPHISM
manitou 0:fc1335b7b54f 4 //
manitou 0:fc1335b7b54f 5 // Created by Kristjan Thorsteinsson on 01/04/14.
manitou 0:fc1335b7b54f 6 // Copyright (c) 2014 Kristjan Thorsteinsson. All rights reserved.
manitou 0:fc1335b7b54f 7 //
manitou 0:fc1335b7b54f 8
manitou 0:fc1335b7b54f 9 #include "Bug.h"
manitou 0:fc1335b7b54f 10 #include "Organism.h"
manitou 0:fc1335b7b54f 11 using namespace std;
manitou 0:fc1335b7b54f 12
manitou 0:fc1335b7b54f 13 Bug::Bug(World* aWorld, int xcoord, int ycoord) : Organism(aWorld, xcoord, ycoord)
manitou 0:fc1335b7b54f 14 {
manitou 0:fc1335b7b54f 15 starveTicks = 0;
manitou 0:fc1335b7b54f 16 }
manitou 0:fc1335b7b54f 17
manitou 0:fc1335b7b54f 18
manitou 0:fc1335b7b54f 19 void Bug::move()
manitou 0:fc1335b7b54f 20 {
manitou 0:fc1335b7b54f 21 breedTicks++;
manitou 0:fc1335b7b54f 22 starveTicks++;
manitou 0:fc1335b7b54f 23
manitou 0:fc1335b7b54f 24 #if 1
manitou 0:fc1335b7b54f 25 // find an edible nabor
manitou 0:fc1335b7b54f 26 for (int i = 0; i < NABORS; i++) {
manitou 0:fc1335b7b54f 27 switch (nabors[i]) {
manitou 0:fc1335b7b54f 28 case 0:
manitou 0:fc1335b7b54f 29 if (world->getAt(x, y + 1) != NULL)
manitou 0:fc1335b7b54f 30 {
manitou 0:fc1335b7b54f 31 if (world->getAt(x, y + 1)->getType() == ANT)
manitou 0:fc1335b7b54f 32 {
manitou 0:fc1335b7b54f 33 starveTicks = 0;
manitou 0:fc1335b7b54f 34 delete world->getAt(x, y + 1);
manitou 0:fc1335b7b54f 35 movesTo(x, y + 1);
manitou 0:fc1335b7b54f 36 return;
manitou 0:fc1335b7b54f 37 }
manitou 0:fc1335b7b54f 38 }
manitou 0:fc1335b7b54f 39 break;
manitou 0:fc1335b7b54f 40
manitou 0:fc1335b7b54f 41 case 1:
manitou 0:fc1335b7b54f 42 if (world->getAt(x, y - 1) != NULL)
manitou 0:fc1335b7b54f 43 {
manitou 0:fc1335b7b54f 44 if (world->getAt(x, y - 1)->getType() == ANT)
manitou 0:fc1335b7b54f 45 {
manitou 0:fc1335b7b54f 46 starveTicks = 0;
manitou 0:fc1335b7b54f 47 delete world->getAt(x, y - 1);
manitou 0:fc1335b7b54f 48 movesTo(x, y - 1);
manitou 0:fc1335b7b54f 49 return;
manitou 0:fc1335b7b54f 50 }
manitou 0:fc1335b7b54f 51 }
manitou 0:fc1335b7b54f 52 break;
manitou 0:fc1335b7b54f 53
manitou 0:fc1335b7b54f 54 case 2:
manitou 0:fc1335b7b54f 55 if (world->getAt(x - 1, y) != NULL)
manitou 0:fc1335b7b54f 56 {
manitou 0:fc1335b7b54f 57 if (world->getAt(x - 1, y)->getType() == ANT)
manitou 0:fc1335b7b54f 58 {
manitou 0:fc1335b7b54f 59 starveTicks = 0;
manitou 0:fc1335b7b54f 60 delete world->getAt(x - 1, y);
manitou 0:fc1335b7b54f 61 movesTo(x - 1, y);
manitou 0:fc1335b7b54f 62 return;
manitou 0:fc1335b7b54f 63 }
manitou 0:fc1335b7b54f 64 }
manitou 0:fc1335b7b54f 65 break;
manitou 0:fc1335b7b54f 66
manitou 0:fc1335b7b54f 67 case 3:
manitou 0:fc1335b7b54f 68 if (world->getAt(x + 1, y) != NULL)
manitou 0:fc1335b7b54f 69 {
manitou 0:fc1335b7b54f 70 if (world->getAt(x + 1, y)->getType() == ANT)
manitou 0:fc1335b7b54f 71 {
manitou 0:fc1335b7b54f 72 starveTicks = 0;
manitou 0:fc1335b7b54f 73 delete world->getAt(x + 1, y);
manitou 0:fc1335b7b54f 74 movesTo(x + 1, y);
manitou 0:fc1335b7b54f 75 return;
manitou 0:fc1335b7b54f 76 }
manitou 0:fc1335b7b54f 77 }
manitou 0:fc1335b7b54f 78 break;
manitou 0:fc1335b7b54f 79
manitou 0:fc1335b7b54f 80 default:
manitou 0:fc1335b7b54f 81 break;
manitou 0:fc1335b7b54f 82 }
manitou 0:fc1335b7b54f 83 } // for nabors
manitou 0:fc1335b7b54f 84 #else
manitou 0:fc1335b7b54f 85
manitou 0:fc1335b7b54f 86 if (world->getAt(x, y + 1) != NULL)
manitou 0:fc1335b7b54f 87 {
manitou 0:fc1335b7b54f 88 if (world->getAt(x, y + 1)->getType() == ANT)
manitou 0:fc1335b7b54f 89 {
manitou 0:fc1335b7b54f 90 starveTicks = 0;
manitou 0:fc1335b7b54f 91 delete world->getAt(x, y + 1);
manitou 0:fc1335b7b54f 92 movesTo(x, y + 1);
manitou 0:fc1335b7b54f 93 return;
manitou 0:fc1335b7b54f 94 }
manitou 0:fc1335b7b54f 95 }
manitou 0:fc1335b7b54f 96
manitou 0:fc1335b7b54f 97 if (world->getAt(x, y - 1) != NULL)
manitou 0:fc1335b7b54f 98 {
manitou 0:fc1335b7b54f 99 if (world->getAt(x, y - 1)->getType() == ANT)
manitou 0:fc1335b7b54f 100 {
manitou 0:fc1335b7b54f 101 starveTicks = 0;
manitou 0:fc1335b7b54f 102 delete world->getAt(x, y - 1);
manitou 0:fc1335b7b54f 103 movesTo(x, y - 1);
manitou 0:fc1335b7b54f 104 return;
manitou 0:fc1335b7b54f 105 }
manitou 0:fc1335b7b54f 106 }
manitou 0:fc1335b7b54f 107
manitou 0:fc1335b7b54f 108 if (world->getAt(x - 1, y) != NULL)
manitou 0:fc1335b7b54f 109 {
manitou 0:fc1335b7b54f 110 if (world->getAt(x - 1, y)->getType() == ANT)
manitou 0:fc1335b7b54f 111 {
manitou 0:fc1335b7b54f 112 starveTicks = 0;
manitou 0:fc1335b7b54f 113 delete world->getAt(x - 1, y);
manitou 0:fc1335b7b54f 114 movesTo(x - 1, y);
manitou 0:fc1335b7b54f 115 return;
manitou 0:fc1335b7b54f 116 }
manitou 0:fc1335b7b54f 117 }
manitou 0:fc1335b7b54f 118 if (world->getAt(x + 1, y) != NULL)
manitou 0:fc1335b7b54f 119 {
manitou 0:fc1335b7b54f 120 if (world->getAt(x + 1, y)->getType() == ANT)
manitou 0:fc1335b7b54f 121 {
manitou 0:fc1335b7b54f 122 starveTicks = 0;
manitou 0:fc1335b7b54f 123 delete world->getAt(x + 1, y);
manitou 0:fc1335b7b54f 124 movesTo(x + 1, y);
manitou 0:fc1335b7b54f 125 return;
manitou 0:fc1335b7b54f 126 }
manitou 0:fc1335b7b54f 127 }
manitou 0:fc1335b7b54f 128 #endif
manitou 0:fc1335b7b54f 129
manitou 0:fc1335b7b54f 130 Move mover = world->randomMove();
manitou 0:fc1335b7b54f 131 switch (mover) {
manitou 0:fc1335b7b54f 132 case UP:
manitou 0:fc1335b7b54f 133 if (world->getAt(x, y + 1) == NULL && in_range(x, y + 1))
manitou 0:fc1335b7b54f 134 {
manitou 0:fc1335b7b54f 135 movesTo(x, y + 1);
manitou 0:fc1335b7b54f 136 }
manitou 0:fc1335b7b54f 137 break;
manitou 0:fc1335b7b54f 138 case DOWN:
manitou 0:fc1335b7b54f 139 if (world->getAt(x, y - 1) == NULL && in_range(x, y - 1))
manitou 0:fc1335b7b54f 140 {
manitou 0:fc1335b7b54f 141 movesTo(x, y - 1);
manitou 0:fc1335b7b54f 142 }
manitou 0:fc1335b7b54f 143 break;
manitou 0:fc1335b7b54f 144 case LEFT:
manitou 0:fc1335b7b54f 145 if (world->getAt(x - 1, y) == NULL && in_range(x - 1, y))
manitou 0:fc1335b7b54f 146 {
manitou 0:fc1335b7b54f 147 movesTo(x - 1, y);
manitou 0:fc1335b7b54f 148 }
manitou 0:fc1335b7b54f 149 break;
manitou 0:fc1335b7b54f 150 case RIGHT:
manitou 0:fc1335b7b54f 151 if (world->getAt(x + 1, y) == NULL && in_range(x + 1, y))
manitou 0:fc1335b7b54f 152 {
manitou 0:fc1335b7b54f 153 movesTo(x + 1, y);
manitou 0:fc1335b7b54f 154 }
manitou 0:fc1335b7b54f 155 break;
manitou 0:fc1335b7b54f 156 default:
manitou 0:fc1335b7b54f 157 break;
manitou 0:fc1335b7b54f 158 }
manitou 0:fc1335b7b54f 159 }
manitou 0:fc1335b7b54f 160
manitou 0:fc1335b7b54f 161 void Bug::generateOffspring(int whereX, int whereY)
manitou 0:fc1335b7b54f 162 {
manitou 0:fc1335b7b54f 163 new Bug(this->world, whereX, whereY);
manitou 0:fc1335b7b54f 164 breedTicks = 0;
manitou 0:fc1335b7b54f 165 }
manitou 0:fc1335b7b54f 166
manitou 0:fc1335b7b54f 167 void Bug::breed()
manitou 0:fc1335b7b54f 168 {
manitou 0:fc1335b7b54f 169 if (breedTicks >= BREED_BUGS)
manitou 0:fc1335b7b54f 170 {
manitou 0:fc1335b7b54f 171 breedAtAdjacentCell();
manitou 0:fc1335b7b54f 172 }
manitou 0:fc1335b7b54f 173
manitou 0:fc1335b7b54f 174 }
manitou 0:fc1335b7b54f 175
manitou 0:fc1335b7b54f 176 bool Bug::isDead() const
manitou 0:fc1335b7b54f 177 {
manitou 0:fc1335b7b54f 178 if (starveTicks >= STARVE_BUGS)
manitou 0:fc1335b7b54f 179 {
manitou 0:fc1335b7b54f 180 return true;
manitou 0:fc1335b7b54f 181 }
manitou 0:fc1335b7b54f 182 else
manitou 0:fc1335b7b54f 183 {
manitou 0:fc1335b7b54f 184 return false;
manitou 0:fc1335b7b54f 185 }
manitou 0:fc1335b7b54f 186 }
manitou 0:fc1335b7b54f 187
manitou 0:fc1335b7b54f 188 OrganismType Bug::getType() const
manitou 0:fc1335b7b54f 189 {
manitou 0:fc1335b7b54f 190 return BUG;
manitou 0:fc1335b7b54f 191 }
manitou 0:fc1335b7b54f 192
manitou 0:fc1335b7b54f 193
manitou 0:fc1335b7b54f 194 char Bug::representation()const
manitou 0:fc1335b7b54f 195 {
manitou 0:fc1335b7b54f 196 return 'X';
manitou 0:fc1335b7b54f 197 }
manitou 0:fc1335b7b54f 198
manitou 0:fc1335b7b54f 199 int Bug::size() const
manitou 0:fc1335b7b54f 200 {
manitou 0:fc1335b7b54f 201 return 30;
manitou 0:fc1335b7b54f 202 }
manitou 0:fc1335b7b54f 203
manitou 0:fc1335b7b54f 204 bool Bug::in_range(int xx, int yy)
manitou 0:fc1335b7b54f 205 {
manitou 0:fc1335b7b54f 206 return (xx >= 0) && (xx < ROWS) && (yy >= 0) && (yy < COLS);
manitou 0:fc1335b7b54f 207 }