Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab continues the processing for a game of Life that was started in the Lif

ID: 668336 • Letter: T

Question

This lab continues the processing for a game of Life that was started in the Life Matrix assignment. We have a matrix of organisms in a rectangular area, represented by a matrix of booleans. The organisms reproduce and die out according to rules based on population density. At each time advance some new organisms may be born and existing ones may die. Existing organisms may die due to overcrowding or isolation. New ones are born when nearby cells reflect an appropriate population density.

The game is thus parameterized by the particular population density ranges that allow organisms to be born or to continue to thrive. Each cell has up to eight neighbors (above, below, left, right, and four diagonals). Thus, if you count the cell as well, there are nine cells in its neighborhood. For a birth to occur, the cell must be empty and the neighborhood density be in the birth range. The birth range can be given as two integers from zero to nine, birthLow and birthHigh, so that a birth will occur if the cell is empty and the neighborhood population is at least birthLow and no more than birthHigh. Similarly, a death occurs in a cell if there is an organism there and the neighborhood has less than the minimum population for survival, or greater than the maximum. Hence there is a live range provided as two integers from zero to nine, liveLow and liveHigh. The border of the area is not compatible with life, so the top and bottom rows and the left and right columns will never have organisms. This part of the assignment will take the matrix built in the previous assignment and make discrete time increments to advance the state of the game.

Specifications

Your program will read the parameters from standard input. Do NOT include any prompts – prompts will cause the program to fail when tested against my output! This second phase begins as the Life Matrix program began by reading the number of rows, the number of columns, and a seed to randomly fill the matrix. The number of rows and columns are integers, the seed is a long. The new inputs for this part are the values for the ranges, birthLow, birthHigh, liveLow, and liveHigh. You must read the values in exactly that order with no other input. After building the matrix as in the previous assignment, you should print the matrix, as in the previous assignment, and then repeat for five iterations of updating the matrix and printing the result. To calculate whether an entity lives at a given cell for the next iteration, you will need to calculate the number of entities in the neighborhood and compare the count with the birth range (if the cell is empty) or live range (if there was already an entity there). Note that the count must be based on the values as of the previous iteration. This means that you will need to make a copy of the matrix before doing the updates so you can count entities in neighborhoods based on original values. You should run five update and print cycles.

Instructions

You should use several public static methods with appropriate parameters in this assignment. Choose methods that are clean and do not mix functionality. That is, you should not print from inside a method that will update the state, nor should you change the state in a print method. You should not use any non-local (class or instance) variables. To count entities in the neighborhood, you will need to make a copy (clone) of the matrix. This is tricky because asking the matrix for its clone, with myMatrix.clone(), will result in a matrix with the same rows so that updates to the original rows will change the rows in the clones! What you need to do is create a new matrix by cloning the original and then cloning the rows of the original as in:

boolean[][] myNewMatrix = (boolean[][])myMatrix.clone();

for (int row=0; row < myMatrix.length; row++) {

myNewMatrix[row] = (boolean[])myMatrix[row].clone();

}

Remember that the borders of the area are toxic so entities cannot be born along the border. So in doing the update, start at row 1, column 1 and stop one less than the last row and column of the matrix.

Sample Input

6 8

7

3 8

3 8

This is my code so far and i dont know what to change:

import java.util.Random;
import java.util.Scanner;

public class chegg {
   public static void main(String[] args) {
       // New Scanner
       Scanner console = new Scanner(System.in);
       // Gets number of rows from user
       int rows = console.nextInt();
       // Gets number of columns from user
       int columns = console.nextInt();
       // Gets seed number from user
       long seed = console.nextLong();
       // Gets least number in birth range from user
       // must be >=0
       int birthLow = console.nextInt();
       if (birthLow < 0) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // Gets greatest number in birth range from user
       // must be <= 9
       int birthHigh = console.nextInt();
       if (birthHigh > 9) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // birthHigh >= birthLow
       if (birthHigh < birthLow) {
           throw new IllegalStateException(
                   "birthHigh must be greater than birthLow");
       }
       // Gets least number in live range from user
       // must be >=0
       int liveLow = console.nextInt();
       if (liveLow < 0) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // Gets greatest number in live range from user
       // must be <= 9
       int liveHigh = console.nextInt();
       if (liveHigh > 9) {
           throw new IllegalStateException("Range must be between 0 and 9");
       }
       // liveHigh >= liveLow
       if (liveHigh < liveLow) {
           throw new IllegalStateException(
                   "liveHigh must be greater than liveLow");
       }

       // creates 2d array
       boolean matrix[][] = new boolean[rows][columns];
      
  

       // call methods
       array(matrix, rows, columns, seed);
       printArray(matrix, rows, columns);
       System.out.println();
       for (int i = 0; i <= 3; i++) {
           // get matrix to update
           updateMatrix(matrix, rows, columns, birthLow, birthHigh, liveLow,
                   liveHigh);
           printArray(matrix, rows, columns);
           System.out.println();
       }

       }
  

   public static void array(boolean[][] matrix, int rows, int columns,
           long seed) {
       Random generator = new Random(seed);
       // for loop to get true or false value
       for (int i = 1; i < rows - 1; i++) {
           for (int j = 1; j < columns - 1; j++) {
               boolean random = generator.nextBoolean();
               if (random == false) {
                   matrix[i][j] = false;
               } else {
                   matrix[i][j] = true;
               }
           }
       }
   }

   public static void printArray(boolean[][] matrix, int rows, int columns) {
       // for loop to print matrix array
       for (int r = 0; r < rows; r++) {
           for (int c = 0; c < columns; c++) {
               // if false print -
               if (matrix[r][c] == false) {
                   System.out.print("- ");
                   // if true print #
               } else {
                   System.out.print("# ");
               }
           }
           // start new row
           System.out.println();
       }
   }

   public static void updateMatrix(boolean[][] matrix, int rows,
           int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {
       // creates clone of matrix
       boolean[][] newMatrix = (boolean[][]) matrix.clone();
       for (int row = 0; row < matrix.length; row++) {
           newMatrix[row] = (boolean[]) matrix[row].clone();
       }
      
       // nested for loop for rows and columns
       for (int i = 1; i < rows - 1; i++) {
           for (int j = 1; j < columns - 1; j++) {
               // if dead or "- " determine if born again or stay dead
               if (!matrix[i][j]) {
                   // test for life, if life is there increase by 1
                   int count = 0;
                   if (matrix[i - 1][j - 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i - 1][j] == true) {
                       count = count + 1;
                   }
                   if (matrix[i - 1][j + 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i][j - 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i][j + 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i + 1][j + 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i + 1][j - 1] == true) {
                       count = count + 1;
                   }
                   if (matrix[i + 1][j] == true) {
                       count = count + 1;
                   } else {

                   }
                   // if >=birthLow && <=birthHigh birth will occur
                   if (count >= birthLow && count <= birthHigh) {
                       newMatrix[i][j] = true;
                   }
                   // if live or "# " determine if dies or stays alive
               }
               else {
                   int count2 = 1;
                   // if true count2 increase by 1
                   if (matrix[i - 1][j - 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i - 1][j] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i - 1][j + 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i][j - 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i][j + 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i + 1][j + 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i + 1][j - 1] == true) {
                       count2 = count2 + 1;
                   }
                   if (matrix[i + 1][j] == true) {
                       count2 = count2 + 1;
                   }
                   // if <=liveLow or >=liveHigh death will occur
                   if (count2 >= liveHigh || count2 <= liveLow) {
                       newMatrix[i][j] = false;
                   } else {

                   }
               }
           }
       }
   }

}

Explanation / Answer

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 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
{
}
}
}
}
}
}
}

           

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote