Question
C++ OPTION A (Basic): 1D Cellular Automata Implement the full class Automaton from the lectures. Test the class in a main() that works like the
C++
OPTION A (Basic): 1D Cellular Automata
Implement the full class Automaton from the lectures. Test the class in a main() that works like the main()from the module. Show at least four different sample runs of width 79, giving 100
Understand the Application
Implement the full class Automaton from the lectures. Test the class in a main() that works like the main()from the module. Show at least four different sample runs of width 79, giving 100 generations per run (to keep the output manageable). Show me the runs for rules 4, 126 and 131. Add at least one more rule of your choice, but 2 or 3 more are welcome.
Class Automaton Spec
Here is a repeat of the specification of the class.
class Automaton { private: static const int RULES_SIZE = 8; bool rule[RULES_SIZE]; string thisGen; string extremeBit; // bit, "*" or " ", implied everywhere "outside" int displayWidth; // an odd number so it can be perfectly centered public: static const int MAX_DISPLAY_WIDTH = 121; Automaton(int rule); string toStringCurrentGen(); bool setRule(int newRule); // change rule set void resetFirstGen(); bool setDisplayWidth(int width); void propagateNewGeneration(); };
The meaning and behavior of members and methods is in the modules, but here is a summary.
Automaton(int rule) - a constructor. Through the mutator, below, we'll sanitize rule and then convert it to our internal representation. We also need to establish the seed: a single 1 in a sea of 0s (in this assignment a 1 means an asterisk, '*', and a 0 means a blank, ' '. This is accomplished more directly by setting thisGento "*" and extremeBit to " ".
string toStringCurrentGen() - This returns a string consisting of the current generation, thisGen, but does so by embedding it in a returnString whose length is exactly displayWidthlong. If thisGen is smaller than displayWidth, it is positioned in the center of the larger returnString, with the excess padded byextremeBit characters. If thisGen is longer than displayWidth, it has to be truncated (on both ends) so that it is perfectly centered in the returned returnString, any excess trimmed off both ends, equally.
Two Mutators:bool setRule(int newRule) - converts the int newRule, a number from 0-255, to an array of eight bools. For example, if the newRule is 182, then this is the binary number 1 0 1 1 0 1 1 0, which means that the resultant array will be
rule[7] = true, rule[6] = false , rule[5] = true, rule[4] = true, rule[3] = false, rule[2] = true, rule[1] = true , rule[0] = false.
I have color coded two of the bits in 182 and their corresponding bools in the array rule[]so you can easily see how the conversion needs to work. As usual, this must sanitize the int so that only values in the legal range 0-255 are allowed.
bool setDisplayWidth(int width) - this is described by the earlier description of displayWidthandMAX_DISPLAY_WIDTH. I repeat that only odd widths are allowed (for centering purposes).
void propagateNewGeneration() - this is the workhorse function. It will use the three private membersthisGen, extremeBit and rule[], and create the next generation from them. This method must first append two extremeBits to each side of thisGen in order to provide it with enough bits (or chars) on each end needed by rule. This adds four chars to thisGen, temporarily. We then apply rule in a loop to the new, larger, thisGen, which creates a separate (local) nextGen string. If you do this correctly, nextGen will be two characters smaller than the recently augmented thisGen (but still two larger than the original thisGenthat entered the method). You must understand this statement, conceptually, before you write your first line of code, or you will be doomed. Then, you replace the old thisGen with our new nextGen. In brief, we pad thisGen with four extremeBits, then apply rule, and we have a new nextGen,two larger than we started out with. We copy that back to thisGen to complete the cycle. Finally, we have to apply rule to three consecutive extremeBits to figure out what the new extremeBit will be for the next generation (" " or "*"?) . What do I mean by "three consecutive"? We apply rule to an int representing a 3-bit pattern inside the old generation. In this case, we are doing this way out where all the bits are the same: extremeBit. So each input will be three 0s or three 1s -- an int 0 or an int 7 -- depending on the value of extremeBit. The result will enable us determine the new value of extremeBit. We must do this before we return, so extremeBit is correct for the next call to this method.
void resetFirstGen() - reloads thisGen and extremeBit to represent a 1 centered in a sea of 0s.
Supply a sample set of at least four runs which include the rules, 4, 126, 131, and one or more of your choice. One run would look something like this:
Enter Rule (0 - 255): 124 start * ** *** * ** ***** * ** ** *** *** * ** * ******* *** ** * ** *** ***** * ** * ** ***** ** *** * ** *** * **** *** * ***** ** * ** *** ** ******** * ** **** ** ***** * ** *** * **** *** * ** ** * *** ** ***** *** ** * ***** * ** * ******** ** ** *** *** ** ****** * ** * ** *** * ******* ***** * **** * ** * ** *** ** ** *** ** *** * ** *** *** * ** *** * ** ****** *** ** ***** * ******** *** ***** * ** *** ** * *** **** *** * ** *** *** ** * ** * ** ***** * ** * ***** ** ******** * ** ******** ****** ** ** *** * ** * ** *** *** * ** ** *** ** *** * ** * ********** * ***** * ** ***** end Press any key to continue . . .
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