Two-dimensional cellular automaton

Objectives

  • Declare and use two-dimensional arrays
  • Implement a class to meet specifications
  • Create and use functions
  • Create a function that overloads the insertion operator
  • Pass arrays to functions
  • Declare parameters to functions as constant if they are not supposed to be modified

Resources

Assignment

The goal of this assignment is to implement a two-dimensional cellular automaton. This involves the use of a two-dimensional array.

  • Create a file named automata.cpp
  • Include documentation comments at the beginning of the source file.
  • Include the appropriate "#include" preprocessor directive(s).
  • You must implement the nine functions declared in automata.h. Details for each one are given below.

Function: automata

  • This is the default constructor, so there are NO parameters and NO return data type.
  • Call the reset function.

Function: reset

  • This function takes no arguments.
  • This function does not return anything.
  • This function should set every element of the grid array to DEAD. DEAD is a char constant which is already declared in automata.h
  • Set the generation instance variable to 0.

Function: setCell

  • This function takes two int arguments, a row and a column.
  • This function returns nothing.
  • If the row and column specified are valid (there is a function 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 that was declared in automata.h.

Function: clearCell

  • This function takes two int arguments, a row and a column.
  • This function returns nothing.
  • If the row and column specified are valid (there is a function 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 that was declared in automata.h.

Function: isValidCell

  • This function takes two int arguments, a row and a column.
  • This function returns a Boolean (true/false) value.
  • 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 declared in automata.h.

Function: getGeneration

  • This function has no parameters.
  • This function returns an int.
  • Return the value of the generation instance variable.

Function: getLiveNeighbors

  • This function has two int parameters, a row and a column.
  • Each cell in the grid has from three to eight neighbors (adjacent cells). This function 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 function to test for that.

Function: nextGeneration

  • This function has no parameters.
  • This function returns an int.
  • The purpose of this function 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.

Function: operator<<

  • This function has two parameters, a reference to an ostream, and a reference to an automata object.
  • Display the grid to the ostream using one row of the grid per output line.
  • Return the ostream reference passed to this function.

Notes

  • On kermit, you can compile using: g++ -o auto2d automata.cpp playAutomata.cpp kishio.cpp
  • Once compiled, you can run the program using: ./auto2d
  • On kermit, you can see what the program should work like using this: ~dklick/examples/cis250/auto2d
  • 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 variable 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 function works properly
  • 3 pts: setCell function works properly
  • 3 pts: clearCell function works properly
  • 3 pts: isValidCell function works properly
  • 3 pts: getGeneration function works properly
  • 10 pts: getLiveNeighbors function works properly
  • 10 pts: nextGeneration function works properly
  • 9 pts: operator<< function works properly