Tino APCS

Lab 23.1 Life

Background

The "Game of Life" is a computer simulation of the life and death events of a population of bacterial organisms. This program will determine the life, death, and survival of bacteria from one generation to the next, assuming the starting grid of bacteria is considered generation zero. The rules for the creation of the next generation are as follows:

  1. A "neighbor" of a cell is defined as any living cell touching that cell. For example, the eight blue cells in the diagram are the neighbors of the cell in the middle.

  2. Any living cell with one or zero neighbors will die of loneliness, while any living cell with four or more living neighbors will die from overcrowding. These are "deaths".

  3. Any living cell with two or three neighbors will continue living into the next generation (no change).

  4. Any dead cell with exactly three neighbors will come to life in the next generation. These are "births".

  5. All births and deaths occur simultaneously.

    This means that births and deaths happen instantly. If a cell comes to life or dies, it should not have any effect on the number of living neighbors you calculate for other cells. In order to make this happen, you need two arrays. Loop through the original life2D array and count the number of living neighbors. Put your answer to that cell coming to life, dying, or staying the same into a temporary array. Once you are done filling the temporary array with answers of who is alive/dead, then copy these values back into the original array.

Assignment

  1. Write a program that implements the game of Life for any input file.

  2. The original grid of bacteria will be supplied to your program from a text file. Download and look at life100.txt now.

    1. The first line of the file contains the number of rows and number of columns in the grid.
    2. The grid locations will start with the upper lefter corner at (0, 0).
    3. All the bacteria locations will be valid positions in the grid.
    4. The remaining lines are the (row, column) locations of the initial bacteria.

  3. After your program has initialized the grid with generation 0, your program must allow Life to proceed for any number of generations specified.

  4. Display the final results on the screen (with row and column numbers as shown) and determine the following statistical information:

    1. The number of living cells in row 9.
    2. The number of living cells in column 9.
    3. The number of living cells in the entire board.

Program Requirements

  1. Your program must be declared as a public class and named PX_LastName_FirstName_Life.java

  2. Your program must include these methods exactly as given:

    1. // Constructor that initializes a game of Life from the given data file
      public PX_LastName_FirstName_Life(String fileName) {...}

    2. // Method that runs the Life simulation through the given generation
      // Generation 0 is the initial position, Generation 1 is the position after one round of Life, etc...
      public void runLife(int numGenerations) {...}

    3. // Method that returns the number of living cells in the given row
      // or returns -1 if row is out of bounds. The first row is row 0.
      public int rowCount(int row) {...}

    4. // Method that returns the number of living cells in the given column
      // or returns -1 if column is out of bounds. The first column is column 0.
      public int colCount(int col) {...}

    5. // Method that returns the total number of living cells on the board
      public int totalCount() {...}

    6. // Prints out the Life array with row and column headers as shown in the example below.
      public void printBoard() {...}

    7. // Advances Life forward one generation
      public void nextGeneration() {...}

  3. Other methods:

    You may include any other helper methods you wish.

Instructions

  1. A sample run output is given below. These are the correct answers for Generation 5 if you use the provided data file "life100.txt".

  1. Here are more data files to experiment with if you'd like. happy.txt, cross.txt, x.txt

  2. Submit your code below.

*The Game of Life was invented by John H. Conway of Princeton University. It was first described to a wide audience by Martin Gardner in his "Mathematical Games" column in Scientific American, October, 1970. You can play the game and find out more about it at www.math.com/students/wonders/life/life.html.

You must Sign In to submit to this assignment

Dark Mode

Outline