Conway's game of life - derived from another project, turned into a c++ class, and scaled to support up to a 480x272 display, or a lower resolution color display.
Diff: LifeRules.h
- Revision:
- 0:d43dc92ae767
- Child:
- 1:9e88d16ab21e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LifeRules.h Wed Apr 16 22:25:19 2014 +0000 @@ -0,0 +1,101 @@ + +#include "mbed.h" + +// Defined and Derived values to check if it exceeds available memory +//#define BITS_PER_LIFE 2 /* 0, 1, 2, 3 provides a more colorful view of life */ +#define FRAMES_PER_BIT 2 /* current and next life cycle */ +#define BYTE_RSVD_RAM 0x8000 + +//#if (LIFE_W * LIFE_H * BITS_PER_LIFE * FRAMES_PER_BIT / 8) > (BYTE_RSVD_RAM) +//#error Bummer, can't make the lifemap that big +//#endif + + +class Life { +public: + typedef enum { + monochrome, ///< life cycle is dead or living + color ///< life cycle is dying to dead and birthing to living + } Animation; + + typedef enum { + dead, + living, + dying, + birthing + } ValueOfLife; + + /// constructor for the life + /// + /// @param w is the width of the life map + /// @param h is the height of the life map + /// + Life(int w, int h, Animation animate = color); + + /// sets a life value in the frame at location x,y. + /// + /// @param x is the x location in the life-map + /// @param y is the y location in the life-map + /// @param otherframe selects the next life-cycle of interest + /// @param b is the value of life to assign to that location + /// + void setbit(int x, int y, ValueOfLife b, int otherframe = 0); + + /// gets the life value from the specified location. + /// + /// @param x is the x location in the life-map + /// @param y is the y location in the life-map + /// @param otherframe selects the next life-cycle of interest + /// @returns the value of life at that location. + /// + ValueOfLife getbit(int x, int y, int otherframe = 0); + + /// Count and return the number of living neighbors. + /// + /// @param x is the x location in the life-map + /// @param y is the y location in the life-map + /// @returns the number of neighbors + /// + int CountNeighbors(int x, int y); + + /// Destroy all life - typically at startup. + /// + void DestroyAllLife(void); + + /// Step life forward by one generation + /// + void GenerationStep(void); + + /// Update the life-cycle. + /// + /// This method applies partial step to a life cycle. + /// It does this when the Life is set up for color mode + /// where you can then see birthing and dying cells as + /// a result of this method. + /// + void UpdateLifeCycle(void); + + /// Apply the cycle of life to a cell. + /// + /// Based on the number of neighbors, and the current + /// state of a cell, this determines whether the cell + /// is born, lives, or dies. + /// + /// @param x is the x offset into the life map. + /// @param y is the y offset into the life map. + /// @param neighbors is the count of neighbors. + /// + void CycleOfLife(int x, int y, int neighbors); + +private: + int LIFE_W; + int LIFE_H; + int maxX; // set max element + int maxY; + int frame; // the current life cycle + int animation; + + // CAUTION: This gives a pointer to a 32K byte range which is all being used! + uint8_t * pLifeMap; +}; + \ No newline at end of file