Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OLED_I2C.h>
- #define WIDTH 128
- #define HEIGHT 64
- OLED myOLED(A4, A5, A4);
- // Создаём массив точек (начиная с 20 строки) и заполняем его случайными значениями
- // Третий индекс нужен для хранения состояния на текущем ходу (0) и количества очков для следующего хода (1)
- byte point[WIDTH][HEIGHT][2];
- void setup() {
- myOLED.begin();
- for (int h=19; h<HEIGHT; h++) {
- for (int w=0; w<WIDTH; w++) {
- point[w][h][0] = random(0,2); // Состояние на текущем шаге
- point[w][h][1] = 0; // Очки
- }
- }
- }
- void loop() {
- myOLED.clrScr();
- for (int h=19; h<HEIGHT; h++) {
- for (int w=0; w<WIDTH; w++) {
- if (h-1 >= 19) {
- if (point[w][h-1][0] == 1) {
- point[w][h][1]++;
- }
- }
- if (w-1 >= 0) {
- if (point[w-1][h][0] == 1) {
- point[w][h][1]++;
- }
- }
- if (w+1 < WIDTH) {
- if (point[w][h-1][0] == 1) {
- point[w][h][1]++;
- }
- }
- if (h+1 < HEIGHT) {
- if (point[w][h+1][0] == 1) {
- point[w][h][1]++;
- }
- }
- if (point[w][h][0] == 1) {
- myOLED.setPixel(w,h);
- }
- }
- }
- myOLED.update();
- for (int h=19; h<HEIGHT; h++) {
- for (int w=0; w<WIDTH; w++) {
- if ( point[w][h][1] == 3) { // Правило рождения
- point[w][h][0] = 1;
- }
- else if (point[w][h][1] == 4) { // Правило смерти
- point[w][h][0] = 0;
- }
- // Правило выживания просто тождество
- // Обнуление очков после подсчёта будущего состояния
- point[w][h][1] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment