Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Two dimensional cellular automata - can't fit everything, comment and I can send other resources. Objectives Declare and use two-dimensional arrays Implement a class
C++ Two dimensional cellular automata - can't fit everything, comment and I can send other resources.
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
- kishio.h: used for I/O
- kishio.cpp: used for I/O
- playAutomata.cpp: the test driver for this assignment
- automata.h: the header file for the class you must implement
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
- 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
playAutomata.cpp #include#include #include #include #include #include "automata.h" #include "kishio.h" using std::cin; using std::cout; using std::string; using std::vector; using kishio::getChar; using kishio::getInt; int cont(); class Point { public: int x, y; Point(const int n1, const int n2) : x(n1), y(n2) {} }; class Pattern { public: string name; vector coord; }; int main(int argc, char* argv[]) { char again; char quit; int i, j, k; int choice; char buf[500]; vector data; vector pattern; vector token; vector xy; data.push_back(string("block,1,1,1,2,2,1,2,2")); data.push_back(string("behive,1,2,1,3,2,1,2,4,3,2,3,3")); data.push_back(string("loaf,1,2,1,3,2,1,2,4,3,2,3,4,4,3")); data.push_back(string("boat,1,1,1,2,2,1,2,3,3,2")); data.push_back(string("blinker,2,1,2,2,2,3")); data.push_back(string("toad,2,2,2,3,2,4,3,1,3,2,3,3")); data.push_back(string("beacon,1,1,1,2,2,1,3,4,4,3,4,4")); data.push_back(string("pulsar,1,5,1,11,2,5,2,11,3,5,3,6,3,10,3,11,5,1,5,2,5,3,5,6,5,7,5,9,5,10,5,13,5,14,5,15,6,3,6,5,6,7,6,9,6,11,6,13,7,5,7,6,7,10,7,11,9,5,9,6,9,10,9,11,10,3,10,5,10,7,10,9,10,11,10,13,11,1,11,2,11,3,11,6,11,7,11,9,11,10,11,13,11,14,11,15,13,5,13,6,13,10,13,11,14,5,14,11,15,5,15,11,")); data.push_back(string("glider,1,2,2,3,3,1,3,2,3,3")); data.push_back(string("lwss,1,2,1,5,2,6,3,2,3,6,4,3,4,4,4,5,4,6")); data.push_back(string("r-pentomino,1,2,1,3,2,1,2,2,3,2")); data.push_back(string("diehard,1,7,2,1,2,2,3,2,3,6,3,7,3,8")); data.push_back(string("acorn,1,2,2,4,3,1,3,2,3,5,3,6,3,7")); data.push_back(string("gosper-glider-gun,1,25,2,23,2,25,3,13,3,14,3,21,3,22,3,35,3,36,4,12,4,16,4,21,4,22,4,35,4,36,5,1,5,2,5,11,5,17,5,21,5,22,6,1,6,2,6,11,6,15,6,17,6,18,6,23,6,25,7,11,7,17,7,25,8,12,8,16,9,13,9,14")); data.push_back(string("switch-engine-1,1,7,2,5,2,7,2,8,3,5,3,7,4,5,5,3,6,1,6,3")); data.push_back(string("switch-engine-2,1,1,1,2,1,3,1,5,2,1,3,4,3,5,4,2,4,3,4,5,5,1,5,3,5,5")); data.push_back(string("switch-engine-3,1,1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,10,1,11,1,12,1,13,1,14,1,18,1,19,1,20,1,27,1,28,1,29,1,30,1,31,1,32,1,33,1,35,1,36,1,37,1,38,1,39")); for (i=0; i<(int)(data.size()); i++) { token.clear(); strncpy(buf, data[i].c_str(), 500); buf[499] = '\0'; char* ptr = strtok(buf, ","); while (ptr != NULL) { token.push_back(string(ptr)); ptr = strtok(NULL, ","); } int numTokens = token.size(); if (numTokens < 1) { cout << "Error: No name supplied for start pattern " << (i+1) << ' '; } else if (numTokens < 2) { cout << "Error: No coordinates supplied for start pattern " << (i+1) << ' '; } else if (numTokens % 2 != 1) { cout << "Error: Invalid number of values for coordinates for start pattern " << (i+1) << ' '; } else { xy.clear(); bool coordsValid = true; for (j=1; j sungetc() != -1 && cin.get() != ' ') cin.ignore(80,' '); cout << "Press enter to continue, Q to quit..."; return cin.get(); }
automata.h
#ifndef __AUTOMATA_H__ #define __AUTOMATA_H__ #includeusing std::ostream; class automata { private: static const char LIVE = '*'; static const char DEAD = ' '; static const int ROWS = 20; static const int COLS= 60; int generation; char grid[ROWS][COLS]; int count[ROWS][COLS]; public: automata(); void reset(); void setCell(const int r, const int col); void clearCell(const int r, const int col); bool isValidCell(const int r, const int c) const; int getLiveNeighbors(const int r, const int c) const; int getGeneration() const; int nextGeneration(); friend ostream& operator<<(ostream& strm, const automata& brd); }; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started