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 // Ant.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 #include <cstdlib>
manitou 0:fc1335b7b54f 9
manitou 0:fc1335b7b54f 10 //#include <iostream>
manitou 0:fc1335b7b54f 11 #include "Ant.h"
manitou 0:fc1335b7b54f 12 #include "Organism.h"
manitou 0:fc1335b7b54f 13 #include "World.h"
manitou 0:fc1335b7b54f 14 using namespace std;
manitou 0:fc1335b7b54f 15
manitou 0:fc1335b7b54f 16 Ant::Ant(World* aWorld, int xcoord, int ycoord) : Organism(aWorld, xcoord, ycoord)
manitou 0:fc1335b7b54f 17 {
manitou 0:fc1335b7b54f 18
manitou 0:fc1335b7b54f 19 }
manitou 0:fc1335b7b54f 20
manitou 0:fc1335b7b54f 21 void Ant::move()
manitou 0:fc1335b7b54f 22 {
manitou 0:fc1335b7b54f 23 breedTicks++;
manitou 0:fc1335b7b54f 24 Move mover = world->randomMove();
manitou 0:fc1335b7b54f 25 switch (mover) {
manitou 0:fc1335b7b54f 26 case UP:
manitou 0:fc1335b7b54f 27 if (world->getAt(x, y + 1) == NULL && in_range(x, y + 1))
manitou 0:fc1335b7b54f 28 {
manitou 0:fc1335b7b54f 29 movesTo(x, y + 1);
manitou 0:fc1335b7b54f 30 }
manitou 0:fc1335b7b54f 31 break;
manitou 0:fc1335b7b54f 32 case DOWN:
manitou 0:fc1335b7b54f 33 if (world->getAt(x, y - 1) == NULL && in_range(x, y - 1))
manitou 0:fc1335b7b54f 34 {
manitou 0:fc1335b7b54f 35 movesTo(x, y - 1);
manitou 0:fc1335b7b54f 36 }
manitou 0:fc1335b7b54f 37 break;
manitou 0:fc1335b7b54f 38 case LEFT:
manitou 0:fc1335b7b54f 39 if (world->getAt(x - 1, y) == NULL && in_range(x - 1, y))
manitou 0:fc1335b7b54f 40 {
manitou 0:fc1335b7b54f 41 movesTo(x - 1, y);
manitou 0:fc1335b7b54f 42 }
manitou 0:fc1335b7b54f 43 break;
manitou 0:fc1335b7b54f 44 case RIGHT:
manitou 0:fc1335b7b54f 45 if (world->getAt(x + 1, y) == NULL && in_range(x + 1, y))
manitou 0:fc1335b7b54f 46 {
manitou 0:fc1335b7b54f 47 movesTo(x + 1, y);
manitou 0:fc1335b7b54f 48 }
manitou 0:fc1335b7b54f 49 break;
manitou 0:fc1335b7b54f 50 default:
manitou 0:fc1335b7b54f 51 break;
manitou 0:fc1335b7b54f 52 }
manitou 0:fc1335b7b54f 53 }
manitou 0:fc1335b7b54f 54
manitou 0:fc1335b7b54f 55 void Ant::breed()
manitou 0:fc1335b7b54f 56 {
manitou 0:fc1335b7b54f 57 if (breedTicks >= BREED_ANTS)
manitou 0:fc1335b7b54f 58 {
manitou 0:fc1335b7b54f 59 breedAtAdjacentCell();
manitou 0:fc1335b7b54f 60 }
manitou 0:fc1335b7b54f 61 }
manitou 0:fc1335b7b54f 62
manitou 0:fc1335b7b54f 63
manitou 0:fc1335b7b54f 64 void Ant::generateOffspring(int whereX, int whereY)
manitou 0:fc1335b7b54f 65 {
manitou 0:fc1335b7b54f 66 new Ant(this->world, whereX, whereY);
manitou 0:fc1335b7b54f 67 breedTicks = 0;
manitou 0:fc1335b7b54f 68 }
manitou 0:fc1335b7b54f 69
manitou 0:fc1335b7b54f 70
manitou 0:fc1335b7b54f 71 OrganismType Ant::getType() const
manitou 0:fc1335b7b54f 72 {
manitou 0:fc1335b7b54f 73 return ANT;
manitou 0:fc1335b7b54f 74 }
manitou 0:fc1335b7b54f 75
manitou 0:fc1335b7b54f 76 char Ant::representation() const
manitou 0:fc1335b7b54f 77 {
manitou 0:fc1335b7b54f 78 return 'o';
manitou 0:fc1335b7b54f 79 }
manitou 0:fc1335b7b54f 80
manitou 0:fc1335b7b54f 81 int Ant::size() const
manitou 0:fc1335b7b54f 82 {
manitou 0:fc1335b7b54f 83 return 10;
manitou 0:fc1335b7b54f 84 }
manitou 0:fc1335b7b54f 85
manitou 0:fc1335b7b54f 86 bool Ant::in_range(int xx, int yy)
manitou 0:fc1335b7b54f 87 {
manitou 0:fc1335b7b54f 88 return (xx >= 0) && (xx < ROWS) && (yy >= 0) && (yy < COLS);
manitou 0:fc1335b7b54f 89 }