Created
October 29, 2017 08:39
-
-
Save electronut/b55c3eb28ab9d419b8721c9c095326fe to your computer and use it in GitHub Desktop.
stm32-conway-update.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Conway64::update() | |
{ | |
// From my book Python Playground | |
// https://www.nostarch.com/pythonplayground | |
// copy grid since we require 8 neighbors for calculation | |
// and we go line by line | |
BitBuf88 _newGrid = _grid; | |
uint8_t N = 8; | |
// compute 8-neighbour sum | |
// using toroidal boundary conditions - x and y wrap around | |
// so that the simulaton takes place on a toroidal surface. | |
for(int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
int total = 0; | |
// count | |
if(_grid.get(i, (uint8_t)(j-1)%N)) | |
total++; | |
if(_grid.get(i, (j+1)%N)) | |
total++; | |
if(_grid.get((uint8_t)(i-1)%N, j)) | |
total++; | |
if(_grid.get((i+1)%N, j)) | |
total++; | |
if(_grid.get((uint8_t)(i-1)%N, (uint8_t)(j-1)%N)) | |
total++; | |
if(_grid.get((uint8_t)(i-1)%N, (j+1)%N)) | |
total++; | |
if(_grid.get((i+1)%N, (uint8_t)(j-1)%N)) | |
total++; | |
if(_grid.get((i+1)%N, (j+1)%N)) | |
total++; | |
// apply Conway's rules | |
if (_grid.get(i, j)) { | |
if ((total < 2) || (total > 3)) { | |
_newGrid.clr(i, j); | |
} | |
} | |
else { | |
if (total == 3) { | |
_newGrid.set(i, j); | |
} | |
} | |
} | |
} | |
// render | |
_max7219->setBuffer(_newGrid); | |
// update data | |
_grid = _newGrid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment