/* playAutomata.cpp CIS 250 Dave Klick 2015-01-31 This program can be used to test out the class that implements the 2-D cellular automata assignment. It uses a slighlty modified version of the cont() function supplied as a resource for the CIS 250 class. This program requires that kishio.h, kishio.cpp, and automata.h be in the same directory for compiling. */ #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; // used to play a new 2-d automata pattern char quit; // used to keep playing an existing pattern int i, j, k; int choice; // keeps track of user menu item choice char buf[500]; vector data; // pattern data supplied as strings vector pattern; // pattern data parsed into Pattern objects vector token; // keeps track of tokens when parsing // pattern data into a Pattern object vector xy; // stores coordinate data during parsing // Starting pattern data for 2-d cellular automata. This is used // like a DATA statement in old Basic programs. The alternative, // which would be much more flexible, would be to use a data file - // but fewer pieces was a higher priority for this assignment. 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")); // This section parses the strings of pattern data into // Pattern objects so they are easier to use. for (i=0; i<(int)(data.size()); i++) { // Break a string into tokens using "," as the delimiter 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, ","); } // Check for and reject obvious errors. int numTokens = token.size(); if (numTokens < 1) { cout << "Error: No name supplied for start pattern " << (i+1) << '\n'; } else if (numTokens < 2) { cout << "Error: No coordinates supplied for start pattern " << (i+1) << '\n'; } else if (numTokens % 2 != 1) { cout << "Error: Invalid number of values for coordinates for start pattern " << (i+1) << '\n'; } else { // Now parse the tokenized coordinate values into int values. // The first token must be skipped since it is a pattern name. xy.clear(); bool coordsValid = true; for (j=1; jsungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue, Q to quit..."; return cin.get(); }