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

Dependencies:   fastlib mbed vga640x480g

Committer:
JLS
Date:
Sun Mar 04 13:07:21 2012 +0000
Revision:
0:ba3520f6dbce

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JLS 0:ba3520f6dbce 1 #include "mbed.h"
JLS 0:ba3520f6dbce 2 #include "vga640x480g.h"
JLS 0:ba3520f6dbce 3
JLS 0:ba3520f6dbce 4 void DisplayState(int s[],int len, int row);
JLS 0:ba3520f6dbce 5
JLS 0:ba3520f6dbce 6 int main() {
JLS 0:ba3520f6dbce 7
JLS 0:ba3520f6dbce 8 init_vga();
JLS 0:ba3520f6dbce 9 vga_cls();
JLS 0:ba3520f6dbce 10
JLS 0:ba3520f6dbce 11
JLS 0:ba3520f6dbce 12 while(1) {
JLS 0:ba3520f6dbce 13
JLS 0:ba3520f6dbce 14 int iteration = 479;
JLS 0:ba3520f6dbce 15 int lenght = 639;
JLS 0:ba3520f6dbce 16 int row = 0;
JLS 0:ba3520f6dbce 17 int state[lenght];
JLS 0:ba3520f6dbce 18 int newstate[lenght];
JLS 0:ba3520f6dbce 19 int i,j,k;
JLS 0:ba3520f6dbce 20 int rules[8] = {rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2,rand()%2};
JLS 0:ba3520f6dbce 21
JLS 0:ba3520f6dbce 22 wait (0.5);
JLS 0:ba3520f6dbce 23
JLS 0:ba3520f6dbce 24 vga_cls();
JLS 0:ba3520f6dbce 25
JLS 0:ba3520f6dbce 26 for (i=0;i<lenght;i++)
JLS 0:ba3520f6dbce 27
JLS 0:ba3520f6dbce 28 state[i]= int (rand()%2);
JLS 0:ba3520f6dbce 29
JLS 0:ba3520f6dbce 30 for (i=0;i<iteration;i++) {
JLS 0:ba3520f6dbce 31
JLS 0:ba3520f6dbce 32 for (j=0;j<lenght;j++) newstate[j] = 0;
JLS 0:ba3520f6dbce 33
JLS 0:ba3520f6dbce 34 for (j=0;j<lenght;j++) {
JLS 0:ba3520f6dbce 35
JLS 0:ba3520f6dbce 36 k = 4*state[(j-1+lenght)%lenght] + 2*state[j] + state[(j+1)%lenght];
JLS 0:ba3520f6dbce 37 newstate[j] = rules[k];
JLS 0:ba3520f6dbce 38 }
JLS 0:ba3520f6dbce 39
JLS 0:ba3520f6dbce 40 for (j=0;j<lenght;j++) state[j] = newstate[j];
JLS 0:ba3520f6dbce 41
JLS 0:ba3520f6dbce 42 DisplayState(state,lenght,row);
JLS 0:ba3520f6dbce 43 row = row + 1;
JLS 0:ba3520f6dbce 44 if (row == 480) row = 0;
JLS 0:ba3520f6dbce 45 }
JLS 0:ba3520f6dbce 46
JLS 0:ba3520f6dbce 47 }
JLS 0:ba3520f6dbce 48
JLS 0:ba3520f6dbce 49 }
JLS 0:ba3520f6dbce 50
JLS 0:ba3520f6dbce 51
JLS 0:ba3520f6dbce 52 void DisplayState(int s[],int len, int row)
JLS 0:ba3520f6dbce 53 {
JLS 0:ba3520f6dbce 54 int i;
JLS 0:ba3520f6dbce 55
JLS 0:ba3520f6dbce 56 for (i=0;i<len;i++) {
JLS 0:ba3520f6dbce 57
JLS 0:ba3520f6dbce 58 if (s[i] == 1){
JLS 0:ba3520f6dbce 59 vga_plot(i,row,WHITE);
JLS 0:ba3520f6dbce 60 }
JLS 0:ba3520f6dbce 61
JLS 0:ba3520f6dbce 62 else{
JLS 0:ba3520f6dbce 63 vga_plot(i,row,BLACK);
JLS 0:ba3520f6dbce 64 }
JLS 0:ba3520f6dbce 65 }
JLS 0:ba3520f6dbce 66 }