CIS 260 - Two-dimensional cellular automaton
The objective of this assignment is to implement a two-dimensional
cellular automaton. This involves the use of a
two-dimensional array.
Objectives
- declare and use two-dimensional arrays
- implement a class to meet specifications
- create and use methods
- pass arrays to methods
- declare parameters to methods as constant if they are not supposed to be modified
Resources
- Keyboard.java: used for I/O
- PlayAutomata.java: the test driver for this assignment
- Automata.java: a partial class you have to complete
Assignment
- Start with the file named Automata.java
- Include documentation comments at the beginning of the source file.
- You must implement the nine methods described in this document.
- Once you are done, compile using: javac PlayAutomata.java
- Once the code is compiled, test the program using: java PlayAutomata
Method: Automata
- This is the default constructor, so there are NO parameters and NO return data type.
- This constructor should be accessible to all classes.
- Call the reset method.
Method: reset
- This method takes no arguments.
- This method does not return anything.
- This method should be accessible to all classes.
- This method should set every element of the grid array
to DEAD. DEAD is a char constant which is already declared in this class.
- Set the generation instance variable to 0.
Method: setCell
- This method takes two int arguments, a row and a column.
- This method returns nothing.
- This method should be accessible to all classes.
- If the row and column specified are valid (there is a method available to check
for that), then set the cell in the grid at the specified row and column to LIVE.
LIVE is a char constant which is already declared in this class.
Method: clearCell
- This method takes two int arguments, a row and a column.
- This method returns nothing.
- This method should be accessible to all classes.
- If the row and column specified are valid (there is a method available to check
for that), then set the cell in the grid at the specified row and column to DEAD.
DEAD is a char constant which is already declared in this class.
Method: isValidCell
- This method takes two int arguments, a row and a column.
- This method returns a Boolean (true/false) value.
- This method should only be accessible to this class.
- If the row specified is between 0 and ROWS-1 and the column specified is between
0 and COLS-1, then return true. Otherwise return false. ROWS and COLS are
int constants which are already declared in this class.
Method: getGeneration
- This method has no parameters.
- This method returns an int.
- This method should be accessible to all classes.
- Return the value of the generation instance variable.
Method: getLiveNeighbors
- This method has two int parameters, a row and a column.
- This method should only be accessible to this class.
- Each cell in the grid has from three to eight neighbors (adjacent cells).
This method must return the number of neighbors that contain the value
LIVE.
- Remember that many cells don't have all eight neighbors. For example,
the cell in row 0, column 0, has only three neighbors. Many of its possible
neighbors ((row-1, column-1), (row-1, column), (row-1 column+1),
(row, column-1), (row+1, column-1)) do not exist because those coordinates
are outside the bounds of the grid. You can use the isValidCell method to
test for that.
Method: nextGeneration
- This method has no parameters.
- This method returns an int.
- This method should be accessible to all classes.
- The purpose of this method is to modify the grid to represent the
next generation. Here's how you are supposed to do that for this assignment:
- Set each element of the count array to the number of live
neighbors that the corresponding element in the grid array has.
- For each element in the grid array, set it according to the
following rules: If the cell was LIVE and has less than two or more than three
live neighbors, then change it to DEAD. If the cell was DEAD and has exactly
three live neighbors, then change it to LIVE.
- Add one to the generation.
- Return the value of the generation instance variable.
Method: toString
- This method has no parameters.
- This method must return a String representation of the grid, with a newline
added after each row of the grid.
- This method should be accessible to all classes.
Note
The count array would normally be declared as a local variable
inside the nextGeneration method since that is the only method that uses it.
Its elevation in this case to an instance field is poor coding style.
Grading rubric
Note: Program must be able to be compiled and run to get any points.
- 3 pts: style conventions followed (no tabs, proper indentation, documentation comments)
- 3 pts: Automata constructor works properly
- 3 pts: reset method works properly
- 3 pts: setCell method works properly
- 3 pts: clearCell method works properly
- 3 pts: isValidCell method works properly
- 3 pts: getGeneration method works properly
- 10 pts: getLiveNeighbors method works properly
- 10 pts: nextGeneration method works properly
- 9 pts: toString method works properly