tom dunigan
/
antsnbugs
stochastic simulation, predator/prey
World.h@0:fc1335b7b54f, 2019-12-23 (annotated)
- Committer:
- manitou
- Date:
- Mon Dec 23 18:56:56 2019 +0000
- Revision:
- 0:fc1335b7b54f
stochastic simulation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manitou | 0:fc1335b7b54f | 1 | #ifndef WORLD_H |
manitou | 0:fc1335b7b54f | 2 | #define WORLD_H |
manitou | 0:fc1335b7b54f | 3 | #include <stddef.h> |
manitou | 0:fc1335b7b54f | 4 | |
manitou | 0:fc1335b7b54f | 5 | #include "Organism.h" |
manitou | 0:fc1335b7b54f | 6 | |
manitou | 0:fc1335b7b54f | 7 | // The possible moves |
manitou | 0:fc1335b7b54f | 8 | enum Move {UP = 0, DOWN, LEFT, RIGHT}; |
manitou | 0:fc1335b7b54f | 9 | |
manitou | 0:fc1335b7b54f | 10 | // The size of this world |
manitou | 0:fc1335b7b54f | 11 | const int ROWS = 24, COLS = 32; |
manitou | 0:fc1335b7b54f | 12 | |
manitou | 0:fc1335b7b54f | 13 | // Number of initial ants |
manitou | 0:fc1335b7b54f | 14 | const int INITIAL_ANTS = 50; |
manitou | 0:fc1335b7b54f | 15 | |
manitou | 0:fc1335b7b54f | 16 | // Number of initial bugs |
manitou | 0:fc1335b7b54f | 17 | const int INITIAL_BUGS = 10; |
manitou | 0:fc1335b7b54f | 18 | |
manitou | 0:fc1335b7b54f | 19 | // Time steps between breeding of ants |
manitou | 0:fc1335b7b54f | 20 | const int BREED_ANTS = 3; |
manitou | 0:fc1335b7b54f | 21 | |
manitou | 0:fc1335b7b54f | 22 | // Time steps between breeding of bugs |
manitou | 0:fc1335b7b54f | 23 | const int BREED_BUGS = 8; |
manitou | 0:fc1335b7b54f | 24 | |
manitou | 0:fc1335b7b54f | 25 | // Time steps until bugs die if they have not eaten |
manitou | 0:fc1335b7b54f | 26 | const int STARVE_BUGS = 3; |
manitou | 0:fc1335b7b54f | 27 | |
manitou | 0:fc1335b7b54f | 28 | const int NABORS = 4; |
manitou | 0:fc1335b7b54f | 29 | extern int nabors[]; |
manitou | 0:fc1335b7b54f | 30 | |
manitou | 0:fc1335b7b54f | 31 | struct Position |
manitou | 0:fc1335b7b54f | 32 | { |
manitou | 0:fc1335b7b54f | 33 | int x; |
manitou | 0:fc1335b7b54f | 34 | int y; |
manitou | 0:fc1335b7b54f | 35 | }; |
manitou | 0:fc1335b7b54f | 36 | |
manitou | 0:fc1335b7b54f | 37 | class World |
manitou | 0:fc1335b7b54f | 38 | { |
manitou | 0:fc1335b7b54f | 39 | public: |
manitou | 0:fc1335b7b54f | 40 | // Constructor: creates and initializes this world. the seed is used for |
manitou | 0:fc1335b7b54f | 41 | // seeding the random behaviour. |
manitou | 0:fc1335b7b54f | 42 | World(unsigned int seed); |
manitou | 0:fc1335b7b54f | 43 | |
manitou | 0:fc1335b7b54f | 44 | // Destructor. |
manitou | 0:fc1335b7b54f | 45 | ~World(); |
manitou | 0:fc1335b7b54f | 46 | |
manitou | 0:fc1335b7b54f | 47 | // Returns the organism at the given coordinates. |
manitou | 0:fc1335b7b54f | 48 | Organism* getAt(int x, int y) const; |
manitou | 0:fc1335b7b54f | 49 | |
manitou | 0:fc1335b7b54f | 50 | // Sets the organism org at position (x,y). |
manitou | 0:fc1335b7b54f | 51 | void setAt(int x, int y, Organism* org); |
manitou | 0:fc1335b7b54f | 52 | |
manitou | 0:fc1335b7b54f | 53 | // Displays this world. |
manitou | 0:fc1335b7b54f | 54 | void display() const; |
manitou | 0:fc1335b7b54f | 55 | void print() const; |
manitou | 0:fc1335b7b54f | 56 | void plot() const; |
manitou | 0:fc1335b7b54f | 57 | |
manitou | 0:fc1335b7b54f | 58 | // Simulates one time step in this world. |
manitou | 0:fc1335b7b54f | 59 | void simulateOneStep(); |
manitou | 0:fc1335b7b54f | 60 | |
manitou | 0:fc1335b7b54f | 61 | // Returns a random position in the grid. |
manitou | 0:fc1335b7b54f | 62 | Position randomPosition() const; |
manitou | 0:fc1335b7b54f | 63 | |
manitou | 0:fc1335b7b54f | 64 | // Returns a random move (UP, DOWN, LEFT or RIGHT). |
manitou | 0:fc1335b7b54f | 65 | Move randomMove() const; |
manitou | 0:fc1335b7b54f | 66 | |
manitou | 0:fc1335b7b54f | 67 | private: |
manitou | 0:fc1335b7b54f | 68 | // The grid in which the organisms live. According the to image below, |
manitou | 0:fc1335b7b54f | 69 | // the correct iteration order through grid starts at the top left |
manitou | 0:fc1335b7b54f | 70 | // corner (i.e. grid[0][0]), loops through one column at a time and ends |
manitou | 0:fc1335b7b54f | 71 | // at the bottom right corner (i.e. grid[WORLDSIZE-1][WORLDSIZE-1]). |
manitou | 0:fc1335b7b54f | 72 | // |
manitou | 0:fc1335b7b54f | 73 | // grid[0, 0] , grid[1, 0], ..., grid[WORLDSIZE-1, 0] |
manitou | 0:fc1335b7b54f | 74 | // grid[0, 1] , grid[1, 1], ..., grid[WORLDSIZE-1, 1] |
manitou | 0:fc1335b7b54f | 75 | // . . |
manitou | 0:fc1335b7b54f | 76 | // . . |
manitou | 0:fc1335b7b54f | 77 | // . . |
manitou | 0:fc1335b7b54f | 78 | // grid[0, WORLDSIZE-2], grid[1, WORLDSIZE-2], ..., grid[WORLDSIZE-1, WORLDSIZE-2] |
manitou | 0:fc1335b7b54f | 79 | // grid[0, WORLDSIZE-1], grid[1, WORLDSIZE-1], ..., grid[WORLDSIZE-1, WORLDSIZE-1] |
manitou | 0:fc1335b7b54f | 80 | // |
manitou | 0:fc1335b7b54f | 81 | // (See e.g. the destructor for correct iteration through the grid) |
manitou | 0:fc1335b7b54f | 82 | Organism* grid[ROWS][COLS]; |
manitou | 0:fc1335b7b54f | 83 | unsigned long steps, step_us; |
manitou | 0:fc1335b7b54f | 84 | |
manitou | 0:fc1335b7b54f | 85 | // Randomly create `count` many organisms of type `orgType`. This |
manitou | 0:fc1335b7b54f | 86 | // method uses the parameterized constructor in Ant and Bug. |
manitou | 0:fc1335b7b54f | 87 | void createOrganisms(OrganismType orgType, int count); |
manitou | 0:fc1335b7b54f | 88 | |
manitou | 0:fc1335b7b54f | 89 | // Reset all organisms to not moved. This is necessary because later we |
manitou | 0:fc1335b7b54f | 90 | // iterate through the grid starting from the top left moving to the |
manitou | 0:fc1335b7b54f | 91 | // bottom right looking for an organism to move. Say if an organism |
manitou | 0:fc1335b7b54f | 92 | // moves down, we don't want to move the organism again when we reach |
manitou | 0:fc1335b7b54f | 93 | // it. |
manitou | 0:fc1335b7b54f | 94 | void resetOrganisms(); |
manitou | 0:fc1335b7b54f | 95 | |
manitou | 0:fc1335b7b54f | 96 | // Make every organisms in this world of type aType move. Make sure to |
manitou | 0:fc1335b7b54f | 97 | // to iterate through grid in order as specified above and only move an |
manitou | 0:fc1335b7b54f | 98 | // organism if it hasn't moved already. |
manitou | 0:fc1335b7b54f | 99 | void moveOrganism(OrganismType aType); |
manitou | 0:fc1335b7b54f | 100 | |
manitou | 0:fc1335b7b54f | 101 | // Remove all dead organism from this world. Iterates through the grid |
manitou | 0:fc1335b7b54f | 102 | // and uses the method Organism::isDead() to test if an organism is |
manitou | 0:fc1335b7b54f | 103 | // dead. For this assignment in this method, only starved bugs will be |
manitou | 0:fc1335b7b54f | 104 | // removed. |
manitou | 0:fc1335b7b54f | 105 | void cleanup(); |
manitou | 0:fc1335b7b54f | 106 | |
manitou | 0:fc1335b7b54f | 107 | // Make every organism in this world breed. Make sure to iterate |
manitou | 0:fc1335b7b54f | 108 | // through grid in order as specified above and to only breed organisms |
manitou | 0:fc1335b7b54f | 109 | // that have moved, since breeding places new organisms on the map we |
manitou | 0:fc1335b7b54f | 110 | // don't want to try and breed those. |
manitou | 0:fc1335b7b54f | 111 | void breedOrganisms(); |
manitou | 0:fc1335b7b54f | 112 | }; |
manitou | 0:fc1335b7b54f | 113 | |
manitou | 0:fc1335b7b54f | 114 | #endif // WORLD_H |