java - Conway's Game of life overpopulation? -



java - Conway's Game of life overpopulation? -

i working on conway's game of life clone because practice, ran problem. see there pixels deleting , rebirthing, pixels spread out end of screen , other pixels rebirthing, idling @ point.

here screenshots:

i'll show of code logic of this. handled in change method:

class="lang-java prettyprint-override">package main; import java.awt.color; import java.awt.graphics; public class functions { public static int pixelsize=6,gridwidth=800/6,gridheight=600/6; static int[][] pixels = new int[gridwidth][gridheight]; static boolean first = true; public static void change(){ for(int = 0; < gridwidth; i++){ for(int j = 0; j < gridheight; j++){ int neighbors = 0; //check each cell try{ if(pixels[i+1][j] == 1){neighbors++;} if(pixels[i-1][j] == 1){neighbors++;} if(pixels[i+1][j-1] == 1){neighbors++;} if(pixels[i][j+1] == 1){neighbors++;} if(pixels[i][j-1] == 1){neighbors++;} if(pixels[i+1][j+1] == 1){neighbors++;} if(pixels[i-1][j-1] == 1){neighbors++;} if(pixels[i-1][j+1] == 1){neighbors++;} }catch(arrayindexoutofboundsexception e){ } if(neighbors == 3 || neighbors == 2 ){ pixels[i][j] = 1; }else if(neighbors < 2 || neighbors >= 4){ pixels[i][j] = 0; } } } } public static void render(graphics g){ for(int = 0; < gridwidth;i++){ for(int j = 0; j < gridheight; j++){ if(pixels[i][j] == 1){ g.setcolor(color.red); g.fillrect(i*6, j*6, 6, 6); } } } } }

help. sadly still isn't working correctly.now doing same thing in diamond formation so:

the main problem see here fact updating values while discovering them.

you should cache whole grid (or @ to the lowest degree neighbours count) before updating, otherwise, while update element @ (x, y), alterating neighbours count element successive (x+1,y), (x+1,y+1), (x,y+1) counting outcome of current iteration.

for illustration update separate array called cachedpixels so:

for(int = 0; < gridwidth; i++){ for(int j = 0; j < gridheight; j++){ int neighbors = 0; // find proper boundaries int mini = math.max(0, - 1); int maxi = math.min(gridwidth, + 2) int minj = math.max(0, j - 1); int maxj = math.min(gridheight, j + 2) (int i2 = mini; i2 < maxi; i2++) { (int j2 = minj; j2 < maxj; j2++) { if (i2 != || j2 != j) { if (pixels[i2][j2] == 1) { neighbors++; } } } } if (neighbors == 2 || neighbors == 3) { cachedpixels[i][j] = 1; } else { cachedpixels[i][j] = 0; // not necessary 0 default value } } }

then after completing process entire array set using arraycopy function:

for (int = 0; < length; i++) { system.arraycopy(cachedpixels[i], 0, pixels[i], 0, cachedpixels[i].length); }

simply setting pixels = cachedpixels point "pixels" "cachedpixels" array , changing 1 alter other, , scheme collapse.

p.s. rules you're using gol not same john h. conway's. cells live on next time step if have 3 neighbours, , live on next time step 2 neighbours if live in time step, else die:

cachedpixels[i][j] = 0; // default value - death. if (neighbors == 3) { cachedpixels[i][j] = 1; } else if (thiscell = 1 && neighbors == 2) { cachedpixels[i][j] = 1; }

java simulation

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -