[英]Wolfram Elementary Cellular Automata Using Processing
I'm using a piece of code taken from the website The Nature of Code - https://natureofcode.com/book/chapter-7-cellular-automata/ - the portion I used was from example 7.1. 我正在使用从The Nature of Code网站获取的一段代码-https: //natureofcode.com/book/chapter-7-cellular-automata/-我使用的部分来自示例7.1。 I'm trying to create a one-dimensional cellular automaton using the Processing IDE, but I keep getting an error saying that brackets are missing from particular locations.
我正在尝试使用Processing IDE创建一维元胞自动机,但是我一直收到错误消息,指出特定位置缺少括号。
Errors: -Missing curlry bracket "}", line 32 -Syntax error on "}", delete this, line 40 错误:-缺少curry括号“}”,第32行-“}”上的语法错误,将其删除,第40行
I've gone over it multiple times, but I can't see how this is wrong. 我已经看过多次了,但是我看不出这是怎么回事。 Though I did try changing them as it says, only to get more errors.
尽管我确实尝试过更改它们,但只是为了获得更多错误。 I thought maybe they were just in the wrong place, but I also can't see how that's the case.
我以为也许他们只是在错误的地方,但我也看不出情况如何。 They seem to be correct as far as I can tell, but maybe I'm missing something.
据我所知,它们似乎是正确的,但也许我遗漏了一些东西。 This is my first time using Processing, and it's been a long time since I last used Java.
这是我第一次使用Processing,距上次使用Java已经很长时间了。 So maybe I'm mistaken.
所以也许我弄错了。
class CA {
int[] cells;
int[] ruleset;
int w = 10;
// The CA should keep track of how
// many generations.
int generation = 0;
CA() {
cells = new int[width/w];
ruleset = new int[]{0,1,0,1,1,0,1,0};
cells[cells.length/2] = 1;
}
// Function to compute the next generation
void generate() {
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++) {
int left = cells[i-1];
int me = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, me, right);
}
cells = nextgen;
// Increment the generation counter.
generation++;
}
int rules(int a, int b, int c) {
String s = "" + a + b + c;
int index = Integer.parseInt(s,2);
return ruleset[index];
}
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) fill(0);
else fill(255);
// Set the y-location according to the generation.
rect(i*w, generation*w, w, w);
}
}
The program is supposed to print each generation of the one-dimensional CA on top of the next. 该程序应该在下一代上打印每一代一维CA。
The for-loop in the end of the CA
class is not supposed to be there. CA
类末尾的for循环不应该存在。 That is why you get the error: the parser expects either a method declaration or the end of the class, hence a curly bracket. 这就是为什么会出现错误的原因:解析器期望方法声明或类的结尾,因此使用大括号。
It looks like this loop is actually drawing the CA
state, so you can try to wrap it in a void draw(){}
method. 看起来此循环实际上正在绘制
CA
状态,因此您可以尝试将其包装在void draw(){}
方法中。 Then it should be syntactically correct, not sure though whether it works as expected. 那么它在语法上应该是正确的,不确定它是否按预期工作。 Alternatively move the for-loop outside of the class and call
fill
based on the the cells
state of an instance of the CA
class. 或者,将for循环移到该类之外,然后根据
CA
类实例的cells
状态调用fill
。
In any case, will need some additional code that creates the CA
instance and invokes the generate
function. 无论如何,将需要一些其他代码来创建
CA
实例并调用generate
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.