Home > OS >  Why does my elementary cellular automata not display the right image given a certain rule-set?
Why does my elementary cellular automata not display the right image given a certain rule-set?

Time:08-09

I have recently made a simple elementary cellular automata visualisation using the built in C console, a cell that is alive is a @ and one that is dead is a space but for some reason it displays some random output to the console no matter which rule-set, the ones that seem to work are simple rule-sets such as rule 2. I do not yet know what might be causing the problem.

code:

int gen[]={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,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};
int ruleset[]={0,0,0,0,0,0,1,0};
int size = sizeof gen / sizeof gen[0];
int alive=1;
int dead=0;

int rules(int a, int b, int c){
    if(a==1 && b==1 && c==1){return ruleset[0];}
    if(a==1 && b==1 && c==0){return ruleset[1];}
    if(a==1 && b==0 && c==1){return ruleset[2];}
    if(a==1 && b==0 && c==0){return ruleset[3];}
    if(a==0 && b==1 && c==1){return ruleset[4];}
    if(a==0 && b==1 && c==0){return ruleset[5];}
    if(a==0 && b==0 && c==1){return ruleset[6];}
    if(a==0 && b==0 && c==0){return ruleset[7];}
}

int main(void){
    int iterations;
    int procces;
    for(iterations=0;iterations<100;iterations  ){
        for(procces=0;procces<size;procces  ){
            int left=gen[procces-1];
            int right=gen[procces 1];
            int cell=gen[procces];
            
            gen[procces]=rules(left,cell,right);
            if(gen[procces]==0){
             printf(" ");
         }
         else{
            printf("@");
         }
        }
     printf("\n");
 }
}

CodePudding user response:

I've introduced a function that avoids out of range subscripts called get_gen().

This one assumes everything beyond your array is barren 0.

For your rule the live cell 'moves' left and then falls out of the world at that barren edge.

As others point out you could execute on a ring. You could even write a version that expands the world when the pattern expands beyond the space given.

#include <stdio.h>
int gen[]={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,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};
int ruleset[]={0,0,0,0,0,0,1,0};
int size = sizeof gen / sizeof gen[0];
int alive=1;
int dead=0;

int rules(int a, int b, int c){
    if(a==1 && b==1 && c==1){return ruleset[0];}
    if(a==1 && b==1 && c==0){return ruleset[1];}
    if(a==1 && b==0 && c==1){return ruleset[2];}
    if(a==1 && b==0 && c==0){return ruleset[3];}
    if(a==0 && b==1 && c==1){return ruleset[4];}
    if(a==0 && b==1 && c==0){return ruleset[5];}
    if(a==0 && b==0 && c==1){return ruleset[6];}
    if(a==0 && b==0 && c==0){return ruleset[7];}
}

int get_gen(int pos,int *gen,int sz){
    if(pos<0 || pos>=sz){
        return 0;
    }
    return gen[pos];
}

int main(void){
    int iterations;
    int procces;
    for(iterations=0;iterations<100;iterations  ){
        for(procces=0;procces<size;procces  ){
            int left=get_gen(procces-1,gen,size);
            int right=get_gen(procces 1,gen,size);
            int cell=get_gen(procces,gen,size);
            
            gen[procces]=rules(left,cell,right);
            if(gen[procces]==0){
                printf(" ");
            } else {
                printf("@");
            }
        }
        printf("\n");
    }
}
  • Related