#include "adevs.h" #include #include #include #include using namespace std; using namespace adevs; /* A Parallel DEVS implementation of the game of life. */ /// Cellspace dimensions #define WIDTH 800 #define HEIGHT 600 /// Possible cell phases. typedef enum { Dead, Alive } Phase; /// IO type for a cell struct io_type { int x, y; Phase phase; }; /// A cell in the Game of Life. class Cell: public Atomic { public: /** * Create a cell and set the initial state. * The width and height fields are used to determine if a * cell is an edge cell. The last phase pointer is used to * visualize the cell space. */ Cell(int x, int y, Phase phase, int nalive, Phase* vis_phase): Atomic(),x(x),y(y),phase(phase), nalive(nalive),vis_phase(vis_phase){} // State transition functions void delta_int(); void delta_ext(Time e, vector& x); void delta_conf(vector& x); // Time advance function Time ta(); // Output function void output_func(vector& y); private: /// Location of the cell in the 2D space const int x, y; /// Current phase of this cell Phase phase; /// The number of living neighbors int nalive; /// Output variable for visualization Phase* vis_phase; /// Returns true if the cell will be born bool check_born_rule() const { return (phase == Dead && nalive == 3); } /// Return true if the cell will die bool check_death_rule() const { return (phase == Alive && (nalive < 2 || nalive > 3)); } }; Time Cell::ta() { // If a phase change should occur if (check_death_rule() || check_born_rule()) return Time(1,0); // Otherwise, do nothing return adevs_inf