-1

我想在处理3中编写自己的“生命游戏”版本,但是我遇到了一个我似乎并不明白的错误。每当代码运行时,屏幕会随着几个像素的变化而持续黑白,但它看起来不像生活的游戏。处理中的生命游戏

任何帮助?

int windowW, windowH, percentAlive, gen; 
//windowW is the width of the window, windowH is the height 
//percentVlive is the initial percent of alive pixel 
//gen is the counter for the generation 
color alive, dead;//alive is white and dead is black to represent their respective colors 
boolean[][] cells0, cells1;//two arrays for the state of the cells, either alive or dead 
boolean zeroOrOne = true;//this is to check which array should be iterated over 

void setup() { 
    size(700, 700); 

    int width = 700; 
    int height = 700; 

    windowW = width; 
    windowH = height; 

    percentAlive = 15; 

    alive = color(255, 255, 255); 
    dead = color(0, 0, 0); 

    cells0 = new boolean[width][height]; 
    cells1 = new boolean[width][height]; 

    frameRate(2); 

    background(alive); 

    for (int x=0; x<width; x++) {//set the percent of live pixels according to the precentAlive varriable 
    for (int y=0; y<height; y++) { 
     int state = (int)random (100); 
     if (state > percentAlive) 
     cells0[x][y] = true; 
     else 
     cells0[x][y] = false; 
    } 
    } 
} 



void draw() { 
    gen += 1;//increases the generation every time it draws 
    drawLoop(zeroOrOne); 
    WriteGeneration(gen); 

    if(zeroOrOne){//changes the zeroOrOne value to change the array being iterated over 
    zeroOrOne = false; 
    } 
    else { 
    zeroOrOne = true; 
    } 
} 

void WriteGeneration(int number) {//changes the label on top 
    fill(0); 
    rect(0, 0, windowW, 100); 
    fill(255); 
    textFont(loadFont("BerlinSansFB-Reg-100.vlw")); 
    text("Generation " + number, 10, 90); 
} 

void drawLoop(boolean check) { 
    loadPixels(); 
    if (check) {//checks which array to iterate thrgough 

    for (int x = 0; x < windowW; x++) {//iterates through the array 
     for (int y = 0; y < windowH; y++) { 
     if (cells0[x][y]) {//checks wether the pixel is alive or dead 
      pixels[x * 700 + y] = alive;//gets the current pixel 
      int lives = lives(x, y, check);//checks how many cells are alive around the current cell 

      if (lives<2) {//these are supposed to put in place the game of life rules 
      cells1[x][y] = false; 
      } else if (lives>4) { 
      cells1[x][y] = false; 
      } else { 
      cells1[x][y] = true; 
      } 
     } else { 
      pixels[x * 700 + y] = dead;//gets the current pixel 
      int lives = lives(x, y, check);//checks how many cells are alive around the current cell 
      if (lives == 3) {//turns the pixel alive if the condition is met 
      cells1[x][y] = true; 
      } 
     } 
     } 
    } 
    } else {//the same as the top but instead the arrays being updated and read are switched 

    for (int x = 0; x < windowW; x++) { 
     for (int y = 0; y < windowH; y++) { 
     if (cells1[x][y]) { 
      pixels[x * 700 + y] = alive; 
      int lives = lives(x, y, check); 
      if (lives<2) { 
      cells0[x][y] = false; 
      } else if (lives>4) { 
      cells0[x][y] = false; 
      } else { 
      cells0[x][y] = true; 
      } 
     } else { 
      pixels[x * 700 + y] = dead; 
      int lives = lives(x, y, check); 
      if (lives == 3) { 
      cells0[x][y] = true; 
      } 
     } 
     } 
    } 
    } 
    updatePixels(); 
} 

int lives(int x, int y, boolean check) {//this just checks how many live pixels are around a given pixel 
    int lives = 0; 
    if (x > 1 && y >1 && x < 699 && y < 699) { 
    if (check) { 
     if (cells0[x-1][y-1]) 
     lives++; 
     if (cells0[x][y-1]) 
     lives++; 
     if (cells0[x+1][y-1]) 
     lives++; 
     if (cells0[x-1][y]) 
     lives++; 
     if (cells0[x+1][y]) 
     lives++; 
     if (cells0[x-1][y+1]) 
     lives++; 
     if (cells0[x][y+1]) 
     lives++; 
     if (cells0[x+1][y+1]) 
     lives++; 
    } else { 
     if (cells1[x-1][y-1]) 
     lives++; 
     if (cells1[x][y-1]) 
     lives++; 
     if (cells1[x+1][y-1]) 
     lives++; 
     if (cells1[x-1][y]) 
     lives++; 
     if (cells1[x+1][y]) 
     lives++; 
     if (cells1[x-1][y+1]) 
     lives++; 
     if (cells1[x][y+1]) 
     lives++; 
     if (cells1[x+1][y+1]) 
     lives++; 
    } 
    } 
    return lives; 
} 
+0

“它看起来不像生活的游戏”几乎不是一个问题的描述。你尝试过调试吗?或让它运行速度更慢? – f1sh

+0

'zeroOrOne'和'cells0' /'cell1'东西是我看的地方。你有两个不同的游戏在同一时间运行吗?那肯定会解释屏幕似乎在两种不同的事情之间交替......在你一次尝试两次之前让它跑步。 –

+0

我试图慢运行它,我可以给出的最好的描述是,屏幕在一次迭代中变黑,然后用另一个看起来像人生游戏的版本返回到白色。它每次都这样做 –

回答

2

请发表您的代码作为MCVE。当我尝试运行你的代码时,我得到一个错误,因为我没有你想要在第59行加载的字体文件。该字体与你的问题没有关系,所以你应该真正摆脱它张贴问题。

你在这段代码中有很多事情要做。我明白为什么你有两个数组,但是在草图层次上让它们只是让你的代码过于复杂。你不应该不断地在这样的数组之间切换。相反,我会像这样组织代码:

  • 您应该只在草图级别有一个数组。您也可以摆脱zeroOrOne变量。
  • 然而,你想要初始化该数组。
  • 创建一个基于当前数组返回新数组的nextGeneration()。这可能会调用其他函数来计算邻居和什么。但重点是你可以每次创建一个新的数组,而不是在两个全局数组之间切换。
  • 这将删除所有重复的逻辑。

一般注意事项:

  • 有8个if语句来检查邻居是有点矫枉过正。为什么不使用嵌套for循环?
  • 你应该养成遵循适当命名约定的习惯。函数应该以小写字母开头,变量应该是描述性的 - 命名check并不真正告诉读者什么。

如果你仍然无法正常工作,那么你将不得不做一些调试。添加print()语句,或者使用Processing编辑器的调试器遍历代码。哪条线的行为与您期望的不同?然后你可以发布一条MCVE(仅仅是那行代码),然后我们将从那里开始。祝你好运。

+0

感谢您的建议。 –

0

您遇到的问题是双重的:

  1. 两个单元阵列,你已经干扰,使两个独立的游戏,当你只想要一个。

  2. 在检查哪些需要修改之前,您正在更新数组中的单元格。

同时解决这两个问题的方法是重新调整cells1数组的用途。而不是每隔一次检查一次,将其设置为完全错误的数组。然后,无论何时要修改单元格0中的正方形,将单元格1中的该位置设置为true,并在为要更改的每个单元格创建标记后,在drawLoop()的末尾使用单独的for循环一次全部更改它们方法。这一举解决了这两个问题。

完成此操作后,您可以删除checkzeroAndOne变量,因为您不再需要它们。这是我得到的drawLoop()方法之后我所做的修改,我建议:

void drawLoop() { 
    loadPixels(); 
    for (int x = 0; x < windowW; x++) { 
    for (int y = 0; y < windowH; y++) { 
     if (cells0[x][y]) { 
     pixels[x * 700 + y] = alive; 
     int lives = lives(x, y); 
     if (lives<2) { 
      cells1[x][y] = true; 
     } else if (lives>4) { 
      cells1[x][y] = true; 
     } 
     } else { 
     pixels[x * 700 + y] = dead; 
     int lives = lives(x, y); 
     if (lives == 3) { 
      cells1[x][y] = true; 
     } 
     } 
    } 
    } 
    for (int x = 0; x < windowW; x++) { 
    for (int y = 0; y < windowH; y++) { 
     if (cells1[x][y]) { 
     cells0[x][y] = !cells0[x][y]; 
     cells1[x][y] = false; 
     } 
    } 
    } 
    updatePixels(); 
} 

我敢肯定,你可以计算出其余部分。祝你好运!