使用1D阵列实现规则110细胞自动机

时间:2015-10-10 18:35:39

标签: c arrays cellular-automata

我试图编写代码来使用1D数组来显示规则110,对于30个整数的数组,显示20行。

#include <stdio.h>

void rule(int t[]);

int main(void)
{
int count = 0;
int i;
int t[] = {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,1,0,0,0,0};
while (count++ < 20)
{
    for (i = 0; i < 30; i++)
    {
        rule(t);
        printf("%d", *t);
    }
    printf("\n");
}
return 0;
}

void rule(int t[])
{

int t1[30];
int ix;
int i;
for (ix=0; ix < 30; ix++)
    {
    if ((t[ix-1] == 0) && (t[ix] == 0) && (t[ix+1] == 0))
        {
        t1[ix] = 0;
        }
        else if ((t[ix-1] == 0) && (t[ix] == 0) && (t[ix+1] == 1))
        {
        t1[ix] = 1; 
        }
        else if ((t[ix-1] == 0) && (t[ix] == 1) && (t[ix+1] == 0))
        {
        t1[ix] = 1;
        }
        else if ((t[ix-1] == 0) && (t[ix] == 1) && (t[ix+1] == 1))
        {
        t1[ix] = 1;
        }
        else if ((t[ix-1] == 1) && (t[ix] == 0) && (t[ix+1] == 0))
        {
        t1[ix] = 0;
        }
        else if ((t[ix-1] == 1) && (t[ix] == 0) && (t[ix+1] == 1))
        {
        t1[ix] = 1;
        }
        else if ((t[ix-1] == 1) && (t[ix] == 1) && (t[ix+1] == 0))
        {
        t1[ix] = 1;
        }
        else if ((t[ix-1] == 1) && (t[ix] == 1) && (t[ix+1] == 1))
        {
        t1[ix] = 0;
        }
    }
    for (i = 0; i < 30; i++)
        {
        t[ix] = t1[ix];
        }


}

它创建一个数组大小30,主要填充0和1和1,然后在规则函数中它创建一个新的并根据前一个数组包含的内容填充它,然后复制这进入了初始数组,并将其传递回main。然而我的似乎只打印了许多0。

看起来应该是:enter image description here

其中0为空,1表示填充。

1 个答案:

答案 0 :(得分:1)

我评论过的代码中有几个错误。我还添加了处理边缘条件的代码。

#include <stdio.h>

#define WIDTH 30            // don't hard code the dimensions etc.

void rule(int t[]);

int main(void) {
    int count = 0, i;
    int t[] = {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,1,0,0,0,0};
    while (count++ < 20) {
        for (i = 0; i < WIDTH; i++) {
            printf("%d", t[i]);             // error here was *t
        }
        printf("\n");
        rule(t);                            // moved outside of print loop
    }
    return 0;
}

void rule(int t[]) {
    int t1[WIDTH];
    int ix, lx, rx, i;

    for (ix=0; ix < WIDTH; ix++) {
        lx = (ix + WIDTH - 1) % WIDTH;      // left pixel wraps to other end
        rx = (ix + 1) % WIDTH;              // right pixel wraps to other end

        if ((t[lx] == 0) && (t[ix] == 0) && (t[rx] == 0)) {
            t1[ix] = 0;
        }
        else if ((t[lx] == 0) && (t[ix] == 0) && (t[rx] == 1)) {
            t1[ix] = 1; 
        }
        else if ((t[lx] == 0) && (t[ix] == 1) && (t[rx] == 0)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 0) && (t[ix] == 1) && (t[rx] == 1)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 1) && (t[ix] == 0) && (t[rx] == 0)) {
            t1[ix] = 0;
        }
        else if ((t[lx] == 1) && (t[ix] == 0) && (t[rx] == 1)) {
            t1[ix] = 1;
        }
        else if ((t[lx] == 1) && (t[ix] == 1) && (t[rx] == 0)) {
            t1[ix] = 1;
        }
        else 
            t1[ix] = 0;
    }
    for (i = 0; i < WIDTH; i++) {
        t[i] = t1[i];                   // error here was t[ix] = t1[ix]
    }
}