【发布时间】:2017-09-01 19:49:22
【问题描述】:
我试图在处理过程中编写康威生命游戏的 OOP 实现。然而,它似乎是某种其他类型的自动机。有趣,但不是我想要的。我找不到我的代码有任何问题,这就是我希望你能帮助解决的问题。
class Cell {
int state;
int x;
int y;
Cell update()
{
try {
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
neighbors += currentBoard[x+i][y+j].state;
}
}
if ((state == 1) && (neighbors < 2))return new Cell(x, y, 0);
else if ((state == 1) && (neighbors > 3))return new Cell(x, y, 0);
else if ((state == 0) && (neighbors == 3))return new Cell(x, y, 1);
else return new Cell(x, y, state);
}
catch(ArrayIndexOutOfBoundsException exception) {
return new Cell(x, y, 0);
}
}
Cell( int _x, int _y, int _s)
{
state = _s;
x = _x;
y = _y;
}
}
Cell[][] currentBoard;
Cell[][] newBoard;
void setup() {
fullScreen();
//frameRate(100);
currentBoard = new Cell[width][height];
newBoard = new Cell[width][height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
currentBoard[i][j] = new Cell(i, j, int(random(2)));
}
}
}
void draw() {
print(frameCount);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
try {
newBoard[i][j] = currentBoard[i][j].update();
}
catch(ArrayIndexOutOfBoundsException exception) {
}
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
color setCol = color(255, 0, 0);
if (newBoard[i][j].state == 1)
{
setCol = color(0, 0, 0);
} else if (newBoard[i][j].state == 0)
{
setCol = color(255, 255, 255);
}
set(i, j, setCol);
}
}
currentBoard = newBoard;
}
怎么了?另外,我不小心创建的自动机上的任何信息都会很酷,它会创建一些漂亮的图案。
【问题讨论】:
-
一目了然,您似乎将一个单元格视为它自己的邻居之一。
-
您将不得不debug your code 将其缩小到与您预期的行为不同的代码行。
-
怎么了?你告诉我们。你期待什么?该程序实际上做了什么?有什么区别?
-
However, it seems to be some other sort of automaton. Interesting, but not what I was going for.这到底是什么意思?您期待什么,与您观察到的有何不同? -
@stackoverflowuser2010 这只是对实际发生的事情的评论。这是某种其他类型的自动机。运行代码,你就会明白我的意思了。
标签: java algorithm processing cellular-automata