Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview This lab completes the processing for a game of Life that was started in the Life Matrix assignment. We have a matrix of organisms

Overview

This lab completes 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. During each time step 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 9 cells in its neighborhood. For a birth to occur, the cell must be empty and the neighborhood (of 9 cells) density be in the birth range. The birth range can be given as two integers from 1 to 9, 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, liveLow and liveHigh. As with the birth range, these values will range from 1 to 9. 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, i.e., they will always contain the value false.

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 parameters from standard input.

This second phase begins as the Life Matrix program began by reading

  1. the number of rows (int)
  2. the number of columns (int)
  3. a seed to randomly fill the matrix (long)
  4. birthLow (int)
  5. birthHigh (int)
  6. liveLow (int)
  7. liveHigh (int)

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 5 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 (of NINE cells), 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 = myMatrix.clone(); for (int row = 0; row < myMatrix.length; ++row) { myNewMatrix[row] = 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.

Check your work. Have you tested your program with large and small matrices? Have you included your name in the comments and expanded the comments to include a description of the problem? Did you use methods? Are their parameters clear, appropriate, and minimal? Does each method have a short comment describing what it does and perhaps pre- and post-conditions? Does the programs indenting match its control structure? Do the for loops have appropriate bounds?

Sample Input

6 8 7 3 8 3 8

Sample Output

NOTE: there should be exactly one System.out.println() after each board is printed (including the last)

- - - - - - - - - # # # - - - - - # # # # - # - - - - # # - # - - # - # - # # - - - - - - - - - - - - - - - - - - # # # # - - - - # # # # # - - - # # # # # # - - - # # # # # - - - - - - - - - - - - - - - - - - # # # # # - - - # - - # # # - - # # - - # # - - # # # # # # - - - - - - - - - - - - - - - - - - # # # # # # - - # # # # # # - - # # # # # # - - # # # # # # - - - - - - - - - - - - - - - - - - # # # # # # - - # - - - - # - - # - - - - # - - # # # # # # - - - - - - - - - - - - - - - - - - # # # # # # - - # # # # # # - - # # # # # # - - # # # # # # - - - - - - - - -

This is the code from the previous assignment:

import java.util.*;

public class Life { /** * This is the main method used to run the program. * * @param args */ public static void main(String[] args) { // New Scanner Scanner console = new Scanner(System.in); // Number of rows int rows = console.nextInt(); // Number of columns int columns = console.nextInt(); // Seed for random number generator long seed = console.nextLong(); // creates a 2d array boolean matrix[][] = new boolean[rows][columns]; rows = matrix.length; columns = matrix[0].length; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { matrix[r][c] = false;

}

} array(matrix, rows, columns, seed); }

/** * This method gets a true or false value and prints an array. * * @param matrix * @param rows * @param columns * @param seed */ public static void array(boolean[][] matrix, int rows, int columns, long seed) { Random generator = new Random(seed); // boundary for loop to get a 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; }

}

} // for loop to print a matrix array for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { // if false then print - if (matrix[r][c] == false) { System.out.print("- "); // if true then print # } else { System.out.print("# "); } } // starts a new row System.out.println();

} } }

language is java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions