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.
LifeRules.h@0:d43dc92ae767, 2014-04-16 (annotated)
- Committer:
- WiredHome
- Date:
- Wed Apr 16 22:25:19 2014 +0000
- Revision:
- 0:d43dc92ae767
- Child:
- 1:9e88d16ab21e
LifeRules apply the Conway game of life rules to a LifeMap defined in an array.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 0:d43dc92ae767 | 1 | |
WiredHome | 0:d43dc92ae767 | 2 | #include "mbed.h" |
WiredHome | 0:d43dc92ae767 | 3 | |
WiredHome | 0:d43dc92ae767 | 4 | // Defined and Derived values to check if it exceeds available memory |
WiredHome | 0:d43dc92ae767 | 5 | //#define BITS_PER_LIFE 2 /* 0, 1, 2, 3 provides a more colorful view of life */ |
WiredHome | 0:d43dc92ae767 | 6 | #define FRAMES_PER_BIT 2 /* current and next life cycle */ |
WiredHome | 0:d43dc92ae767 | 7 | #define BYTE_RSVD_RAM 0x8000 |
WiredHome | 0:d43dc92ae767 | 8 | |
WiredHome | 0:d43dc92ae767 | 9 | //#if (LIFE_W * LIFE_H * BITS_PER_LIFE * FRAMES_PER_BIT / 8) > (BYTE_RSVD_RAM) |
WiredHome | 0:d43dc92ae767 | 10 | //#error Bummer, can't make the lifemap that big |
WiredHome | 0:d43dc92ae767 | 11 | //#endif |
WiredHome | 0:d43dc92ae767 | 12 | |
WiredHome | 0:d43dc92ae767 | 13 | |
WiredHome | 0:d43dc92ae767 | 14 | class Life { |
WiredHome | 0:d43dc92ae767 | 15 | public: |
WiredHome | 0:d43dc92ae767 | 16 | typedef enum { |
WiredHome | 0:d43dc92ae767 | 17 | monochrome, ///< life cycle is dead or living |
WiredHome | 0:d43dc92ae767 | 18 | color ///< life cycle is dying to dead and birthing to living |
WiredHome | 0:d43dc92ae767 | 19 | } Animation; |
WiredHome | 0:d43dc92ae767 | 20 | |
WiredHome | 0:d43dc92ae767 | 21 | typedef enum { |
WiredHome | 0:d43dc92ae767 | 22 | dead, |
WiredHome | 0:d43dc92ae767 | 23 | living, |
WiredHome | 0:d43dc92ae767 | 24 | dying, |
WiredHome | 0:d43dc92ae767 | 25 | birthing |
WiredHome | 0:d43dc92ae767 | 26 | } ValueOfLife; |
WiredHome | 0:d43dc92ae767 | 27 | |
WiredHome | 0:d43dc92ae767 | 28 | /// constructor for the life |
WiredHome | 0:d43dc92ae767 | 29 | /// |
WiredHome | 0:d43dc92ae767 | 30 | /// @param w is the width of the life map |
WiredHome | 0:d43dc92ae767 | 31 | /// @param h is the height of the life map |
WiredHome | 0:d43dc92ae767 | 32 | /// |
WiredHome | 0:d43dc92ae767 | 33 | Life(int w, int h, Animation animate = color); |
WiredHome | 0:d43dc92ae767 | 34 | |
WiredHome | 0:d43dc92ae767 | 35 | /// sets a life value in the frame at location x,y. |
WiredHome | 0:d43dc92ae767 | 36 | /// |
WiredHome | 0:d43dc92ae767 | 37 | /// @param x is the x location in the life-map |
WiredHome | 0:d43dc92ae767 | 38 | /// @param y is the y location in the life-map |
WiredHome | 0:d43dc92ae767 | 39 | /// @param otherframe selects the next life-cycle of interest |
WiredHome | 0:d43dc92ae767 | 40 | /// @param b is the value of life to assign to that location |
WiredHome | 0:d43dc92ae767 | 41 | /// |
WiredHome | 0:d43dc92ae767 | 42 | void setbit(int x, int y, ValueOfLife b, int otherframe = 0); |
WiredHome | 0:d43dc92ae767 | 43 | |
WiredHome | 0:d43dc92ae767 | 44 | /// gets the life value from the specified location. |
WiredHome | 0:d43dc92ae767 | 45 | /// |
WiredHome | 0:d43dc92ae767 | 46 | /// @param x is the x location in the life-map |
WiredHome | 0:d43dc92ae767 | 47 | /// @param y is the y location in the life-map |
WiredHome | 0:d43dc92ae767 | 48 | /// @param otherframe selects the next life-cycle of interest |
WiredHome | 0:d43dc92ae767 | 49 | /// @returns the value of life at that location. |
WiredHome | 0:d43dc92ae767 | 50 | /// |
WiredHome | 0:d43dc92ae767 | 51 | ValueOfLife getbit(int x, int y, int otherframe = 0); |
WiredHome | 0:d43dc92ae767 | 52 | |
WiredHome | 0:d43dc92ae767 | 53 | /// Count and return the number of living neighbors. |
WiredHome | 0:d43dc92ae767 | 54 | /// |
WiredHome | 0:d43dc92ae767 | 55 | /// @param x is the x location in the life-map |
WiredHome | 0:d43dc92ae767 | 56 | /// @param y is the y location in the life-map |
WiredHome | 0:d43dc92ae767 | 57 | /// @returns the number of neighbors |
WiredHome | 0:d43dc92ae767 | 58 | /// |
WiredHome | 0:d43dc92ae767 | 59 | int CountNeighbors(int x, int y); |
WiredHome | 0:d43dc92ae767 | 60 | |
WiredHome | 0:d43dc92ae767 | 61 | /// Destroy all life - typically at startup. |
WiredHome | 0:d43dc92ae767 | 62 | /// |
WiredHome | 0:d43dc92ae767 | 63 | void DestroyAllLife(void); |
WiredHome | 0:d43dc92ae767 | 64 | |
WiredHome | 0:d43dc92ae767 | 65 | /// Step life forward by one generation |
WiredHome | 0:d43dc92ae767 | 66 | /// |
WiredHome | 0:d43dc92ae767 | 67 | void GenerationStep(void); |
WiredHome | 0:d43dc92ae767 | 68 | |
WiredHome | 0:d43dc92ae767 | 69 | /// Update the life-cycle. |
WiredHome | 0:d43dc92ae767 | 70 | /// |
WiredHome | 0:d43dc92ae767 | 71 | /// This method applies partial step to a life cycle. |
WiredHome | 0:d43dc92ae767 | 72 | /// It does this when the Life is set up for color mode |
WiredHome | 0:d43dc92ae767 | 73 | /// where you can then see birthing and dying cells as |
WiredHome | 0:d43dc92ae767 | 74 | /// a result of this method. |
WiredHome | 0:d43dc92ae767 | 75 | /// |
WiredHome | 0:d43dc92ae767 | 76 | void UpdateLifeCycle(void); |
WiredHome | 0:d43dc92ae767 | 77 | |
WiredHome | 0:d43dc92ae767 | 78 | /// Apply the cycle of life to a cell. |
WiredHome | 0:d43dc92ae767 | 79 | /// |
WiredHome | 0:d43dc92ae767 | 80 | /// Based on the number of neighbors, and the current |
WiredHome | 0:d43dc92ae767 | 81 | /// state of a cell, this determines whether the cell |
WiredHome | 0:d43dc92ae767 | 82 | /// is born, lives, or dies. |
WiredHome | 0:d43dc92ae767 | 83 | /// |
WiredHome | 0:d43dc92ae767 | 84 | /// @param x is the x offset into the life map. |
WiredHome | 0:d43dc92ae767 | 85 | /// @param y is the y offset into the life map. |
WiredHome | 0:d43dc92ae767 | 86 | /// @param neighbors is the count of neighbors. |
WiredHome | 0:d43dc92ae767 | 87 | /// |
WiredHome | 0:d43dc92ae767 | 88 | void CycleOfLife(int x, int y, int neighbors); |
WiredHome | 0:d43dc92ae767 | 89 | |
WiredHome | 0:d43dc92ae767 | 90 | private: |
WiredHome | 0:d43dc92ae767 | 91 | int LIFE_W; |
WiredHome | 0:d43dc92ae767 | 92 | int LIFE_H; |
WiredHome | 0:d43dc92ae767 | 93 | int maxX; // set max element |
WiredHome | 0:d43dc92ae767 | 94 | int maxY; |
WiredHome | 0:d43dc92ae767 | 95 | int frame; // the current life cycle |
WiredHome | 0:d43dc92ae767 | 96 | int animation; |
WiredHome | 0:d43dc92ae767 | 97 | |
WiredHome | 0:d43dc92ae767 | 98 | // CAUTION: This gives a pointer to a 32K byte range which is all being used! |
WiredHome | 0:d43dc92ae767 | 99 | uint8_t * pLifeMap; |
WiredHome | 0:d43dc92ae767 | 100 | }; |
WiredHome | 0:d43dc92ae767 | 101 |