I have a problem with a program thats supposed to create a two dimensional array, and then re-create it 1000 times based on the "neighbours" of each element of the array.
The problem is, the way I've done it, it takes longer to run through each iteration of the loop. I can tell because I've put a "System.out.prinln" command on every loop.
When the arrays are small its not too much of a problem, but when the arrays are 100x100 and get checked 1000 times the whole thing doesnt work.
I probably dont quite understand what I've done: Am I somehow re-creating or copying the array? How else can I do this?
import javax.swing.*;
import java.awt.*;
public class GameOfLife3
{
public int array[][];
public
String output;
public GameOfLife3()
{
array = new int[100][100];
output = "";
}
public static void main (String args[])
{
JTextArea outputArea = new JTextArea(20, 60);
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
JScrollPane scroller = new JScrollPane(outputArea);
//create an instance of class GameOfLife3
GameOfLife3 game3 = new GameOfLife3();
//call method that initialises elements of 2d array
game3.initArray(game3.array);
game3.output = game3.writeArray(game3.array, game3.output);
//call method that checks the values of the "neighbours" of elements
//in 2d array and then checks whether they should live or die.
//call method that puts these values in a String. Loop 1000x
for(int count = 0; count <=1000; count++)
{
game3.checkNeighbour(game3.array);
game3.output = game3.writeArray(game3.array, game3.output);
}
outputArea.setText(game3.output);
//Display scrollable JTextArea containing the string "output"
JOptionPane.showMessageDialog(null, scroller,
"Game Of Life 3", JOptionPane. PLAIN_MESSAGE);
System.exit(0);
} //end main
//method initArray: initialises 2d-array
//to random int values 1 & 0
public void initArray( int array2[][])
{
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column++)
{
array2[row][column] = 0 + (int)(Math.random()*2);
}
}
}
//method check Neighbour does 3 things:
//1. checks values of "neighbour" elements in array
//2. creates new array according to game of life rules
//3. places values ofnew array back in first array
public void checkNeighbour(int array2[][])
{
int array3[][] = new int[100][100];
int neighbour = 0;
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column++)
{
if(row ==0 && column == 0)
{
if( array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
}
if(row == 0 && column > 0 && column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
}
if(row == 0 && column == array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
}
if(column == 0 && row > 0 && row < array2.length-1)
{
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
}
if( column == 0 && row == array2.length-1)
{
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
}
if(column == array2.length-1 && row == array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
}
if(row == array2.length-1 && column > 0 &&
column < array2.length-1)
{
if(array2[row][column-1]==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1] ==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
}
if(column == array2.length-1 && row >0 &&
row < array2.length -1)
{
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
}
if(column > 0 && column < array2.length-1 &&
row > 0 && row< array2.length-1)
{
if(array2[row-1][column-1]==1)
neighbour++;
if(array2[row-1][column]==1)
neighbour++;
if(array2[row-1][column+1]==1)
neighbour++;
if(array2[row][column+1]==1)
neighbour++;
if(array2[row+1][column+1]==1)
neighbour++;
if(array2[row+1][column]==1)
neighbour++;
if(array2[row+1][column-1]==1)
neighbour++;
if(array2[row][column-1] ==1)
neighbour++;
}
if(array2[row][column] == 0)
{
if(neighbour == 3)
array3[row][column] = 1;
else
array3[row][column] = 0;
}
if(array2[row][column] == 1)
{
if(neighbour == 3 || neighbour == 2)
array3[row][column] = 1;
else
array3[row][column] = 0;
}
neighbour = 0;
}//close loop column
}//close loop row
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column ++)
{
array2[row][column] = array3[row][column];
}
}
}
//method writeArray: puts elements of 2d array in string to be displayed
public String writeArray( int array2[][], String output1)
{
for(int row = 0; row < array2.length; row ++)
{
for(int column = 0; column < array2[row].length; column ++)
{
output1 += array2[row][column];
}
output1 += "\n";
}
output1 += "\n";
for(int count = 0; count <= 100; count ++)
output1+= "-";
output1 += "\n";
return output1;
}
} // end GameOfLife1