I am making John Conway's Game of Life and I have been using 2 Boolean 2D array's in my game along with If statements to determine what the next move for how the cells in my game should replicate based on the current move and number of neighbors for each cell. The first Boolean 2D array is called current move and it keeps track of how many cells there are on the grid and their specific location by displaying them as true if on grid and false if no cells are on that specific lactation on grid the second Boolean 2D array called next move and the if statements determine what the next current move should be through the if statements determining if their are enough neighbors for a new cell to form or if the cells will remain in same spot and if a new cell can be formed or cells stay in same spot next move becomes true making current move become true for the next generation and the same process repeats again and again. My Question is how i could i manipulate my code to count the number of cells alive in my game, i have added a variable called intpopulation to count the number of cells at specif points by incrementing value when cell is born and decrementing when cell dies but I know i did it wrong because it is not displaying right number of cells and i am unable to figure out my problem. Also current move becomes true at that specific location if grid square on grid is clicked by mouse making current move become true at that specific location. I display the intpopulation value in a jtextfield and there is jbutton which resets the value of intpopulation and gets rid of all cells on grid by making a new current move 2d array. Could someone please help me solve this problem.
public class GameOfLife extends javax.swing.JFrame {
int timelength = 100;
int wid = 500, hei = 250;
boolean[][] currentMove = new boolean[hei][wid], nextMove = new boolean[hei][wid];
boolean play;
public static int intpopulation = 0;
Image offScrImg;
Graphics offScrGraph;
public static int intGeneration = 0;
public GameOfLife() {
initComponents();
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();
Timer time = new Timer();
TimerTask task = new TimerTask(){
public void run() {
if(play == true){
intGeneration++;
for(int i = 0; i < hei; i++){
for(int j = 0; j < wid; j++){
nextMove[i][j] = decide(i,j);
}
}
for(int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
currentMove[i][j] = nextMove[i][j];
}
}
repain();
}
}
};
time.scheduleAtFixedRate(task, 0,timelength);
repain();
}
private boolean decide(int i, int j){
int neighbors = 0;
if(j > 0){
if(currentMove[i][j-1]) neighbors++;
if(i>0) if(currentMove[i-1][j-1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j-1]) neighbors++;
}
if(j < wid - 1){
if(currentMove[i][j+1]) neighbors++;
if(i>0) if(currentMove[i-1][j+1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j+1]) neighbors++;
}
if(i>0) if(currentMove[i-1][j]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j]) neighbors++;
if(currentMove[i][j] && neighbors < 2) intpopulation--;
if(neighbors > 3) intpopulation--;
if(neighbors == 3) return true; intpopulation++;
if(currentMove[i][j] && neighbors == 2) return true;intpopulation++;
return false;
}
private void repain(){
offScrGraph.setColor(Color.BLACK);
offScrGraph.fillRect(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
for (int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
if(currentMove[i][j])
{
offScrGraph.setColor(Color.getHSBColor((float) Math.random(), .8f, .8f));
int x = j * jPanel2.getWidth()/wid;
int y = i * jPanel2.getHeight()/hei;
offScrGraph.fillRect(x, y, jPanel2.getWidth()/wid, jPanel2.getHeight()/hei);
}
}
}
offScrGraph.setColor(Color.BLACK);
for (int i = 1; i < hei; i++){
int y = i * jPanel2.getHeight()/hei;
offScrGraph.drawLine(0, y, jPanel2.getWidth(), y);
}
for (int j = 1; j < wid; j++){
int x = j * jPanel2.getWidth()/wid;
offScrGraph.drawLine(x, 0, x, jPanel2.getHeight());
}
jPanel2.getGraphics().drawImage(offScrImg, 0, 0, jPanel2);
}
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
currentMove[i][j] = !currentMove[i][j];
intpopulation++;
repain();
}
private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
if(SwingUtilities.isLeftMouseButton(evt)){
currentMove[i][j] = true;
intpopulation++;
}else currentMove[i][j] = false;
}
private void jFormattedTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
if(play)
{
String myString = Integer.toString(intpopulation);
jFormattedTextField2.setText( "population: " + myString);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currentMove = new boolean[hei][wid];
intGeneration = 0;
intpopulation = 0;
jFormattedTextField1.setText( "Generation: " + intGeneration );
jFormattedTextField2.setText( "population: " + intpopulation );
repain();
}