Wolfram Rnd 1D cellular automata - 640x480 vga - with random rules

Dependencies:   fastlib mbed vga640x480g

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "vga640x480g.h"
00003 
00004 void DisplayState(int s[],int len, int row);
00005 
00006 int main() {
00007 
00008     init_vga();
00009     vga_cls();
00010     
00011  
00012  while(1) {
00013  
00014     int iteration = 479;
00015     int lenght = 639;
00016     int row = 0;
00017     int state[lenght];
00018     int newstate[lenght];
00019     int i,j,k;
00020     int rules[8] = {rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2};
00021     
00022     wait (0.5);
00023     
00024     vga_cls();
00025     
00026     for (i=0;i<lenght;i++)
00027   
00028         state[i]= int (rand()%2);
00029 
00030     for (i=0;i<iteration;i++) {
00031 
00032     for (j=0;j<lenght;j++) newstate[j] = 0;
00033 
00034     for (j=0;j<lenght;j++) {
00035 
00036         k = 4*state[(j-1+lenght)%lenght] + 2*state[j] + state[(j+1)%lenght];
00037         newstate[j] = rules[k];
00038       }
00039 
00040     for (j=0;j<lenght;j++) state[j] = newstate[j];
00041         
00042         DisplayState(state,lenght,row);
00043         row = row + 1;
00044         if (row == 480) row = 0;
00045    }
00046    
00047    }
00048 
00049   }    
00050   
00051   
00052 void DisplayState(int s[],int len, int row)
00053 {
00054     int i;
00055 
00056     for (i=0;i<len;i++) {
00057 
00058         if (s[i] == 1){
00059             vga_plot(i,row,WHITE);
00060        }
00061          
00062         else{
00063             vga_plot(i,row,BLACK);
00064         }
00065       }
00066 }