|
| 1 | +#include <LEDMatrix.h> |
| 2 | +#include <RGBLEDMatrix.h> |
| 3 | +#include <RGBImage.h> |
| 4 | +#include <Glyph.h> |
| 5 | +#include <RGBAnimation.h> |
| 6 | +#include <RGBAnimationSequence.h> |
| 7 | +#include <TimerAction.h> |
| 8 | + |
| 9 | +// |
| 10 | +// This is an toroidal array implementation of Conway's gqme of life. |
| 11 | +// |
| 12 | +// Colors indicate: |
| 13 | +// green = cell is newly born |
| 14 | +// blue = cell is alive |
| 15 | +// red = cell is dying |
| 16 | +// black = cell is dead |
| 17 | +// |
| 18 | +// A random set of cells are born on the first generration, then the game commences |
| 19 | +// according to the standard rules. |
| 20 | +// |
| 21 | + |
| 22 | +class CellUniverse : public TimerAction { |
| 23 | +public: |
| 24 | + |
| 25 | + typedef byte LifeState; |
| 26 | + |
| 27 | + const static LifeState ALIVE = 1; |
| 28 | + const static LifeState BORN = 2; |
| 29 | + const static LifeState DYING = 3; |
| 30 | + const static LifeState DEAD = 0; |
| 31 | + const static LifeState GAME_OVER = 4; |
| 32 | + |
| 33 | +private: |
| 34 | + |
| 35 | + |
| 36 | + RGBLEDMatrix& _leds; |
| 37 | + |
| 38 | + LifeState* _cells; |
| 39 | + LifeState* _nextCells; |
| 40 | + |
| 41 | + bool _isReseting; |
| 42 | + |
| 43 | + RGBColorType getColorForLifeState( LifeState state ) const; |
| 44 | + |
| 45 | + |
| 46 | +protected: |
| 47 | + virtual void action(); |
| 48 | + |
| 49 | +public: |
| 50 | + |
| 51 | + CellUniverse( |
| 52 | + RGBLEDMatrix& matrix, |
| 53 | + unsigned long updateMicros |
| 54 | + ); |
| 55 | + |
| 56 | + void setCellStatus(unsigned int row, unsigned int column, LifeState cellStatus); |
| 57 | + LifeState getCellStatus(int row, int column) const; |
| 58 | + void createRandomState(); |
| 59 | + |
| 60 | + bool isAlive(int row, int column) const; |
| 61 | + int countAliveNeighbors(int row, int column) const; |
| 62 | + |
| 63 | + void drawToScreen(); |
| 64 | +}; |
| 65 | + |
| 66 | +CellUniverse::CellUniverse( |
| 67 | + RGBLEDMatrix& matrix, |
| 68 | + unsigned long updateMicros |
| 69 | +) : TimerAction(updateMicros), |
| 70 | + _leds(matrix), |
| 71 | + _cells(new LifeState[matrix.rows()*matrix.columns()]), |
| 72 | + _nextCells(new LifeState[matrix.rows()*matrix.columns()]), |
| 73 | + _isReseting(false) |
| 74 | +{ |
| 75 | + memset(_cells,DEAD,matrix.rows()*matrix.columns()*sizeof(LifeState)); |
| 76 | + memset(_nextCells,DEAD,matrix.rows()*matrix.columns()*sizeof(LifeState)); |
| 77 | +} |
| 78 | + |
| 79 | +void CellUniverse::setCellStatus(unsigned int row, unsigned int column, LifeState cellStatus) { |
| 80 | + if (row < 0 || row >= _leds.rows() || column < 0 || column >= _leds.columns()) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + unsigned int idx = row*_leds.columns() + column; |
| 85 | + |
| 86 | + _cells[idx] = cellStatus; |
| 87 | +} |
| 88 | + |
| 89 | +CellUniverse::LifeState CellUniverse::getCellStatus(int row, int column) const { |
| 90 | + // this causes the matrix to be a toroidal array |
| 91 | + unsigned int r = row < 0 ? row + _leds.rows() : ( row >= _leds.rows() ? row - _leds.rows() : row ); |
| 92 | + unsigned int c = column < 0 ? column + _leds.columns() : ( column >= _leds.columns() ? column - _leds.columns() : column ); |
| 93 | + |
| 94 | + // double check just to be sure |
| 95 | + if (r >= _leds.rows() || c >= _leds.columns()) { |
| 96 | + return CellUniverse::DEAD; |
| 97 | + } |
| 98 | + |
| 99 | + unsigned int idx = r*_leds.columns() + c; |
| 100 | + |
| 101 | + return _cells[idx]; |
| 102 | +} |
| 103 | + |
| 104 | +bool CellUniverse::isAlive(int row, int column) const { |
| 105 | + return (this->getCellStatus(row, column) == CellUniverse::ALIVE || this->getCellStatus(row, column) == CellUniverse::BORN); |
| 106 | +} |
| 107 | + |
| 108 | +int CellUniverse::countAliveNeighbors(int row, int column) const { |
| 109 | + int aliveCount = 0; |
| 110 | + |
| 111 | + for (int x = column - 1; x <= column+1; x++) { |
| 112 | + for (int y = row - 1; y <= row + 1; y++ ) { |
| 113 | + if (this->isAlive(y, x) && !(x == column && y == row)) { |
| 114 | + aliveCount++; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return aliveCount; |
| 120 | +} |
| 121 | + |
| 122 | +void CellUniverse::action() { |
| 123 | + if (_isReseting) { |
| 124 | + delay(5000); |
| 125 | + this->createRandomState(); |
| 126 | + _isReseting = false; |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + _leds.startDrawing(); |
| 131 | + bool isSame = true; |
| 132 | + |
| 133 | + for (unsigned int x = 0; x < _leds.columns(); x++) { |
| 134 | + for (unsigned int y = 0; y < _leds.rows(); y++ ) { |
| 135 | + LifeState newState = DEAD; |
| 136 | + LifeState currentState = this->getCellStatus(y, x); |
| 137 | + int count = this->countAliveNeighbors(y, x); |
| 138 | + |
| 139 | + switch (currentState) { |
| 140 | + case BORN: |
| 141 | + case ALIVE: |
| 142 | + if ( count < 2 || count > 3 ) { |
| 143 | + newState = DYING; |
| 144 | + } |
| 145 | + else { |
| 146 | + newState = ALIVE; |
| 147 | + } |
| 148 | + break; |
| 149 | + case DYING: |
| 150 | + case DEAD: |
| 151 | + default: |
| 152 | + if (count == 3) { |
| 153 | + newState = BORN; |
| 154 | + } |
| 155 | + break; |
| 156 | + } |
| 157 | + if (currentState != newState) { |
| 158 | + isSame = false; |
| 159 | + } |
| 160 | + |
| 161 | + unsigned int idx = y*_leds.columns() + x; |
| 162 | + _nextCells[idx] = newState; |
| 163 | + |
| 164 | + RGBColorType cellColor = this->getColorForLifeState(newState); |
| 165 | + _leds.image().pixel(y, x) = cellColor; |
| 166 | + } |
| 167 | + } |
| 168 | + _leds.stopDrawing(); |
| 169 | + |
| 170 | + // determine if life needs to start over |
| 171 | + if (isSame) { |
| 172 | + for (unsigned int x = 0; x < _leds.columns(); x++ ) { |
| 173 | + for (unsigned int y = 0; y < _leds.rows(); y++ ) { |
| 174 | + if (this->getCellStatus(y, x) == DEAD) { |
| 175 | + this->setCellStatus(y, x, GAME_OVER); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + this->drawToScreen(); |
| 180 | + _isReseting = true; |
| 181 | + } |
| 182 | + else { |
| 183 | + memcpy(_cells, _nextCells, _leds.rows()*_leds.columns()*sizeof(LifeState)); |
| 184 | + } |
| 185 | + |
| 186 | +} |
| 187 | + |
| 188 | +void CellUniverse::createRandomState() { |
| 189 | + unsigned int numTotalCells = _leds.rows()*_leds.columns(); |
| 190 | + |
| 191 | + for (unsigned int x = 0; x < _leds.columns(); x++ ) { |
| 192 | + for (unsigned int y = 0; y < _leds.rows(); y++ ) { |
| 193 | + this->setCellStatus(y, x, CellUniverse::DEAD); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + unsigned int countStartingCells = random(0.25*numTotalCells, 0.75*numTotalCells); |
| 198 | + |
| 199 | + for (unsigned int i = 0; i < countStartingCells; i++ ) { |
| 200 | + int randomRow = random(0,_leds.rows()); |
| 201 | + int randomColumn = random(0,_leds.columns()); |
| 202 | + |
| 203 | + this->setCellStatus(randomRow, randomColumn, CellUniverse::BORN); |
| 204 | + } |
| 205 | + |
| 206 | + this->drawToScreen(); |
| 207 | +} |
| 208 | + |
| 209 | +void CellUniverse::drawToScreen() { |
| 210 | + _leds.startDrawing(); |
| 211 | + for (unsigned int x = 0; x < _leds.columns(); x++) { |
| 212 | + for (unsigned int y = 0; y < _leds.rows(); y++ ) { |
| 213 | + LifeState currentState = this->getCellStatus(y, x); |
| 214 | + RGBColorType cellColor = this->getColorForLifeState(currentState); |
| 215 | + _leds.image().pixel(y, x) = cellColor; |
| 216 | + } |
| 217 | + } |
| 218 | + _leds.stopDrawing(); |
| 219 | +} |
| 220 | + |
| 221 | +RGBColorType CellUniverse::getColorForLifeState( LifeState state ) const { |
| 222 | + RGBColorType cellColor = BLACK_COLOR; |
| 223 | + switch (state) { |
| 224 | + case BORN: |
| 225 | + cellColor = GREEN_COLOR; |
| 226 | + break; |
| 227 | + case ALIVE: |
| 228 | + cellColor = BLUE_COLOR; |
| 229 | + break; |
| 230 | + case DYING: |
| 231 | + cellColor = RED_COLOR; |
| 232 | + break; |
| 233 | + case GAME_OVER: |
| 234 | + cellColor = BLACK_COLOR; |
| 235 | + break; |
| 236 | + case DEAD: |
| 237 | + default: |
| 238 | + cellColor = BLACK_COLOR; |
| 239 | + break; |
| 240 | + } |
| 241 | + |
| 242 | + return cellColor; |
| 243 | +} |
| 244 | + |
| 245 | +// |
| 246 | +// PROGRAM BEGINS |
| 247 | +// |
| 248 | + |
| 249 | +RGBLEDMatrix leds(16,16, RGBLEDMatrix::RGB_GROUPS_CPRG8, HIGH, LOW); |
| 250 | + |
| 251 | +CellUniverse uni(leds, 500000); |
| 252 | + |
| 253 | +void setup() { |
| 254 | + leds.setup(); |
| 255 | + // create starting life positions |
| 256 | + // first, pick a rando fraction between 0.25 and 0.75 of cells. |
| 257 | +#ifdef RANDOM_REG32 |
| 258 | + randomSeed(RANDOM_REG32); |
| 259 | +#else |
| 260 | + randomSeed(analogRead(0)); |
| 261 | +#endif |
| 262 | + int numTotalCells = leds.rows()*leds.columns(); |
| 263 | + int countStartingCells = random(0.25*numTotalCells, 0.75*numTotalCells); |
| 264 | + |
| 265 | + for (int i = 0; i < countStartingCells; i++ ) { |
| 266 | + int randomRow = random(0,leds.rows()); |
| 267 | + int randomColumn = random(0,leds.columns()); |
| 268 | + |
| 269 | + uni.setCellStatus(randomRow, randomColumn, CellUniverse::BORN); |
| 270 | + } |
| 271 | + |
| 272 | + uni.drawToScreen(); |
| 273 | + leds.startScanning(); |
| 274 | +} |
| 275 | + |
| 276 | +void loop() { |
| 277 | + uni.loop(); |
| 278 | + leds.loop(); |
| 279 | +} |
0 commit comments