I got the program to run, but the output is not the way it should be and I don't know where I went wrong.
The output is supposed to look like this:
Sample Input
6 8
7
3 8
3 8
Sample Output
- -
- @ @ @ - - - -
- @ @ @ @ - @ -
- @ @ - @ -
- @ - @ - @ @ -
- -
- -
- @ @ @ @ - - -
- @ @ @ @ @ - -
- @ @ @ @ @ @ -
- @ @ @ @ @ -
- -
- -
- @ @ @ @ @ - -
- @ - - @ @ @ -
- @ @ - - @ @ -
- @ @ @ @ @ @ -
- -
- -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
- -
- -
- @ @ @ @ @ @ -
- @ - - - - @ -
- @ - - - - @ -
- @ @ @ @ @ @ -
- -
- -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
- @ @ @ @ @ @ -
import java.util.Random;
import java.util.Scanner;public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println(" "); int rows = console.nextInt(); System.out.println(" "); int columns = console.nextInt(); System.out.println(" "); long seed = console.nextLong(); int birthLow = console.nextInt(); int birthHigh = console.nextInt(); int liveLow = console.nextInt(); int liveHigh = console.nextInt(); boolean myMatrix[][] = new boolean[rows][columns]; array(myMatrix, rows, columns, seed); printArray(myMatrix, rows, columns); System.out.println(); for(int i=0; i<4; i++) { alterMatrix(myMatrix, rows, columns, birthLow, birthHigh, liveLow, liveHigh); printArray(myMatrix, rows, columns); System.out.println(); } }
//------------------------------------------------------------------------------------------
public static void array(boolean[][] Matrix, int rows, int columns,
long seed) {Random generator = new Random(seed); for (int i = 1; i < rows - 1; i++) { for (int j = 1; j < columns - 1; j++) { boolean x = generator.nextBoolean(); if (!x) { Matrix[i][j] = false; } else { Matrix[i][j] = true; } } } }
//-----------------------------------------------------------------------------------------------
// Prints the array
public static void printArray(boolean[][] Matrix, int rows, int columns) {for (int k = 0; k < rows; k++) { for (int m = 0; m < columns; m++) { if (!Matrix[k][m]) { System.out.print("- "); } else { System.out.print("@ "); } } System.out.println(); } }
//---------------------------------------------------------------------------------------------------------------------
public static void alterMatrix(boolean[][] myMatrix, int rows,
int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {boolean[][] myNewMatrix = myMatrix.clone(); for (int row = 0; row < myMatrix.length; row++) { myNewMatrix[row] = myNewMatrix[row].clone(); for (int i = 1; i < rows - 1; i++) { for (int j = 1; j < columns - 1; j++) { if (!myMatrix[i][j]) { int counter = 0; if (myMatrix[i - 1][j - 1] == true) { counter = counter + 1; } if (myMatrix[i - 1][j] == true) { counter = counter + 1; } if (myMatrix[i - 1][j + 1] == true) { counter = counter + 1; } if (myMatrix[i][j - 1] == true) { counter = counter + 1; } if (myMatrix[i][j + 1] == true) { counter = counter + 1; } if (myMatrix[i + 1][j + 1] == true) { counter = counter + 1; } if (myMatrix[i + 1][j - 1] == true) { counter = counter + 1; } if (myMatrix[i + 1][j] == true) { counter = counter + 1; } else { } if (counter >= birthLow && counter <= birthHigh) { myNewMatrix[i][j] = true; } } else { int counter2 = 0; if (myMatrix[i - 1][j - 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i - 1][j] == true) { counter2 = counter2 + 1; } if (myMatrix[i - 1][j + 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i][j - 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i][j + 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i + 1][j + 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i + 1][j - 1] == true) { counter2 = counter2 + 1; } if (myMatrix[i + 1][j] == true) { counter2 = counter2 + 1; } if (counter2 >= liveHigh || counter2 <= liveLow) { myNewMatrix[i][j] = false; } else { } } } } } }
}