我正在尝试基于此创建一个元胞自动机。我设法以一种仅遵循规则 90 的简单方式做到了这一点,但是当我将其更改为接受任何规则时,我做错了。
这就是结果的样子。 http://natureofcode.com/book/imgs/chapter07/ch07_12.png
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *myFile = fopen ( "input.txt" , "r" );
int i, j, cell, iterations=100, current[80], previous[80], ruleCase, rule=90;
for(i=0;i<80;i++){
fscanf(myFile, "%1d", &previous[i]);
if(previous[i]==0)printf("%c",176);
else printf("%c",178);
}
for(i=0;i<=iterations;i++){
for(cell=0;cell<80;cell++)
{
if((cell>0) && (cell<79))
{
ruleCase=1*previous[cell-1]+2*previous[cell]+3*previous[cell+1];
}
else if(cell==0)
{
ruleCase=1*previous[79]+2*previous[cell]+3*previous[cell+1];
}
else if(cell==79)
{
ruleCase=1*previous[cell-1]+2*previous[cell]+3*previous[0];
}
if(rule & (128 >> ruleCase))
{
current[cell]=1;
printf("%c",178);
}
else
{
current[cell]=0;
printf("%c",176);
}
}
for(j=0;j<80;j++){
previous[j]=current[j];
}
}
return 0;
}
使用以下 input.txt
00000000000000000000000000000000000000100000000000000000000000000000000000000000
谢谢!