import java.util.Random;
import java.util.Scanner;
//This program creates a board of -'s with random #'s on it.
public class Life {
// This method asks for user input and calls the initializing and printing
// methods
public static void main(String[] args) {
Scanner console = new Scanner(System.in); // Creates new scanner
System.out.println("Number of rows?");
int rows = console.nextInt(); // Stores user input as number of rows
System.out.println("Number of columns?");
int columns = console.nextInt(); // Stores user input as number of
// columns
System.out.println("Seed number?");
long seed = console.nextLong(); // Stores user input as number of seed
System.out.println("Birth minimum?");
int birthLow = console.nextInt(); // Store user input as the minimum
// number for birth
System.out.println("Birth maximum?");
int birthHigh = console.nextInt(); // Stores user input as maximum
// number for birth
System.out.println("Live minimum?");
int liveLow = console.nextInt(); // Stores user input as minimum for
// death
System.out.println("Live maximum?");
int liveHigh = console.nextInt(); // stores user input as maximum for
// death
boolean initMatrix[][] = new boolean[rows][columns]; // creates new
// boolean array
// sized
// according to
// user input
array(initMatrix, rows, columns, seed); // calls the initializing method
printArray(initMatrix, rows, columns); // calls the printing method
System.out.println();
for(int i=0; i<4; i++){
alterMatrix(initMatrix, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
printArray(initMatrix, rows, columns);
System.out.println();
}
}
// This method initializes the array with trues and falses
public static void array(boolean[][] Matrix, int rows, int columns,
long seed) {
Random generator = new Random(seed); // creates random number according
// to user seed
// loop goes through every row starting at 2nd and ending at 2nd to last
for (int i = 1; i < rows - 1; i++) {
// loop goes through every column starting at 2nd and ending at 2nd
// to last
for (int j = 1; j < columns - 1; j++) {
// generates random value
boolean x = generator.nextBoolean();
// if x is false, set that array spot as false
if (!x) {
Matrix[i][j] = false;
}
// if x is true, set that array spot as true
else {
Matrix[i][j] = true;
}
}
}
}
// This method prints the array
public static void printArray(boolean[][] Matrix, int rows, int columns) {
// these loops go through every value in the array
for (int k = 0; k < rows; k++) {
for (int m = 0; m < columns; m++) {
// if the array is false, print a -
if (!Matrix[k][m]) {
System.out.print("- ");
}
// if the array is true, print a #
else {
System.out.print("# ");
}
}
System.out.println(); // starts a new row
}
}
public static void alterMatrix(boolean[][] initialMatrix, int rows,
int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {
boolean matrixUpdate[][] = initialMatrix.clone();
for (int row = 0; row < initialMatrix.length; row++) {
matrixUpdate[row] = matrixUpdate[row].clone();
// loop goes through every row starting at 2nd and ending at 2nd to
// last
for (int i = 1; i < rows - 1; i++) {
// loop goes through every column starting at 2nd and ending at
// 2nd
// to last
for (int j = 1; j < columns - 1; j++) {
// if initMatrix was false, look to see if life can be born
if (!initialMatrix[i][j]) {
int counter = 0;
// These if statements test each neighboring spot for
// life
// if life is there, counter will increase by 1
if (initialMatrix[i - 1][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i - 1][j] == true) {
counter = counter + 1;
}
if (initialMatrix[i - 1][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j + 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j - 1] == true) {
counter = counter + 1;
}
if (initialMatrix[i + 1][j] == true) {
counter = counter + 1;
} else {
}
// if the counter is in the birth range, set that spot
// as true
if (counter >= birthLow && counter <= birthHigh) {
matrixUpdate[i][j] = true;
}
}
// if initMatrix was true, look to see if life will die
else {
int counter2 = 0;
// these if statements test each spot neighboring spot
// for
// life
// if life is found, counter will increase by 1
if (initialMatrix[i - 1][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i - 1][j] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i - 1][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j + 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j - 1] == true) {
counter2 = counter2 + 1;
}
if (initialMatrix[i + 1][j] == true) {
counter2 = counter2 + 1;
}
// if counter is outside of the death range, life is
// eliminated
if (counter2 >= liveHigh || counter2 <= liveLow) {
matrixUpdate[i][j] = false;
} else {
}
}
}
}
}
}
}