I was working on a Conway's game of life clone because it is good practice, but I ran into a problem. I see there are pixels deleting and rebirthing, but all the pixels just spread out the very end of the screen and then other pixels are rebirthing, but it is just idling at that point.
Here are some screenshots:
I'll show you some of my code for the logic of this. It is all handled in the change
method:
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 i = 0; i < 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 i = 0; i < 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);
}
}
}
}
}
thanks for all the help. Sadly it still isn't working correctly.
Now it is doing the same thing but in diamond formation like so: