#include "mbed.h" Ticker tick; DigitalOut row[] = {PTB18, PTB19, PTC0, PTC4, PTC6, PTC7, PTC10, PTC11}; // 1 = zap DigitalOut red[] = {PTC9, PTC8, PTA5, PTA4, PTA12, PTD3, PTE3, PTE2}; // 0 = zap DigitalOut green[] = {PTC13, PTC16, PTA7, PTA6, PTA14, PTA15, PTA16, PTA17}; // 0 = zap DigitalOut blue[] = {PTA13, PTD2, PTD4, PTD6, PTD7, PTB9, PTE0, PTE1}; // 0 = zap int rpentomino[10][10] = {{0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,0,0,0}, {0,0,0,0,1,1,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,0,0,0,0,0,0,0,0,0}}; int glider[10][10] = {{0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,1,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0}, {1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,0,0,0,0,0,0,0,0,0}}; int data[10][10] = {{0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0}, {0,0,0,0,1,1,1,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,1,1,0}, {0,0,1,0,0,0,0,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, ///////////////////// {0,0,0,0,0,0,0,0,0,0}}; int temp[10][10]; float wtime = 0.5; // cas mezi barvama (ms) void clean() { for(int i=0; i<8; i++) { row[i] = 0; } } int rnd() { int num = rand() % 100; if(num > 50) return 1; else return 0; } void randomizer() { for(int i=0; i<8; i++) { for(int j=0; j<8; j++) { data[i+1][j+1] = rnd(); } } } void copy(int array1[10][10], int array2[10][10]) { for(int j = 0; j < 10; j++) { for(int i = 0; i < 10; i++) array2[j][i] = array1[j][i]; } } void gameLoop() { copy(data, temp); for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { int count = 0; count = data[j-1][i] + data[j-1][i-1] + data[j][i-1] + data[j+1][i-1] + data[j+1][i] + data[j+1][i+1] + data[j][i+1] + data[j-1][i+1]; //The cell dies. if(count < 2 || count > 3) temp[j][i] = 0; //The cell stays the same. if(count == 2) temp[j][i] = data[j][i]; //The cell either stays alive, or is "born". if(count == 3) temp[j][i] = 1; } } copy(temp, data); } int main() { //randomizer(); //copy(rpentomino,data); //copy(glider,data); tick.attach(&gameLoop, 0.5); while (true) { // display for(int i=0; i<8; i++) { clean(); row[i] = 1; // set red for(int j=0; j<8; j++) { red[j] = !data[i+1][j+1]; } wait_ms(wtime); // clean red for(int j=0; j<8; j++) { red[j] = 1; } // set green for(int j=0; j<8; j++) { green[j] = !data[i+1][j+1]; } wait_ms(wtime); // clean green for(int j=0; j<8; j++) { green[j] = 1; } // set blue for(int j=0; j<8; j++) { blue[j] = !data[i+1][j+1]; } wait_ms(wtime); // clean blue for(int j=0; j<8; j++) { blue[j] = 1; } } } }