-
Notifications
You must be signed in to change notification settings - Fork 0
/
Farica Zhuang Pattern Generator.txt
269 lines (225 loc) · 5.76 KB
/
Farica Zhuang Pattern Generator.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//Farica Zhuang
//Pattern Generator program
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
/* Program class uses arrays to generate triangular patterns according to the
* specified input that ranges from 0 - 255. The width of the patterns are
* restriction to 80 characters long.
*/
class Automaton
{
private:
static const int RULES_SIZE = 8;
static const int DEFAULT_RULES_SIZE = 8;
static const int DEFAULT_WIDTH_SIZE = 79;
bool rules[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();
};
int main()
{
string userInput;
int rule, k;
// get rule from user
do
{
cout << "Enter Rule (0 - 255): ";
getline(cin, userInput);
//convert to int
istringstream iss(userInput);
iss >> rule;
} while (rule < 0 || rule > 255);
// create automaton with this rule and single central dot
Automaton aut(rule);
// now show it
cout << " start" << endl;
for (k = 0; k < 100; k++)
{
cout << aut.toStringCurrentGen() << endl;
aut.propagateNewGeneration();
}
cout << " end" << endl;
return 0;
}
//constructor takes the rule (as an int).
Automaton::Automaton(int rule)
{
//Through the mutator, below, we'll sanitize rule
if(!setRule(rule))
{
rule = DEFAULT_RULES_SIZE;
resetFirstGen();
setDisplayWidth(DEFAULT_WIDTH_SIZE);
}
else
{
resetFirstGen();
setDisplayWidth(DEFAULT_WIDTH_SIZE);
}
}
//returns a string consisting of the current generation, thisGen,
// If thisGen is smaller than displayWidth, it is positioned
//in the center of the larger returnString, with the excess padded by extremeBit
//characters.
string Automaton::toStringCurrentGen()
{
string tempString, displayString, tempChar;
int padLength1, padLength2, i;
displayString = "";
if( thisGen.length() <= displayWidth )
{
//thisGen.length will always be odd
padLength1 = (DEFAULT_WIDTH_SIZE - thisGen.length())/2;
for(i = 0; i < padLength1 ; i++)
{
tempString = tempString + " ";
}
tempString = tempString + thisGen;
for(i = 0; i < padLength1 ; i++)
{
tempString = tempString + " ";
}
displayString = tempString;
}
//If thisGen is longer than displayWidth, it has to be truncated
else
{
padLength2 = (thisGen.length() - displayWidth)/2;
tempString = thisGen.substr(padLength2, padLength2 + displayWidth);
tempString.resize(displayWidth);
displayString = tempString;
}
return displayString;
}
//mutator
//converts the int newRule, a number from 0-255, to an array of eight bools
//this must sanitize the int so that only values in the legal range 0-255
bool Automaton::setRule(int newRule)
{
if (newRule < 0 || newRule >255)
{
return false;
}
else
{
for(int i = 0; i < RULES_SIZE; i++)
{
if(newRule%2 == 0)
{
rules[i] = false;
}
else
{
rules[i] = true;
}
newRule = newRule/2;
}
}
return true;
}
//set thisGen to * and extremeBit to a space
void Automaton::resetFirstGen()
{
thisGen = "*";
extremeBit = " ";
}
//with each generation, keep the width to the number of generation so that it
//creates a triangle
void Automaton::propagateNewGeneration()
{
string nextGen,threeBinary, threeExtreme, firstChar, secondChar,
thirdChar;
int toDecimal;
bool valid;
nextGen = "";
//append two extremeBits to each side of thisGen in order to provide it with
//enough bits (or chars) on each end needed by rule.
thisGen = extremeBit + extremeBit + thisGen + extremeBit + extremeBit;
//We then apply rule in a loop to the new, larger, thisGen.
for (int i = 0 ; i < thisGen.length()-2; i++)
{
stringstream ss;
toDecimal = 0;
threeBinary = "";
//convert to decimal
firstChar = thisGen.at(i);
secondChar = thisGen.at(i+1);
thirdChar = thisGen.at(i+2);
if(firstChar == "*")
{
toDecimal = toDecimal + pow (2, 2);
}
if(secondChar == "*")
{
toDecimal = toDecimal + pow (2, 1);
}
if(thirdChar == "*")
{
toDecimal = toDecimal + pow (2, 0);
}
valid = rules[toDecimal];
if(valid == true)
{
nextGen = nextGen + "*";
}
else
{
nextGen = nextGen + " ";
}
}
//apply rule to three consecutive extremeBits to figure out what the new
//extremeBit will be for the next generation (" " or "*"?)
threeExtreme = extremeBit + extremeBit + extremeBit;
toDecimal = 0;
for(int k=0; k< threeExtreme.length() ; k++)
{
if(threeExtreme.at(k) =='*')
{
toDecimal = toDecimal + pow (2, 2-k);
}
}
valid = rules[toDecimal];
if(valid == true)
{
extremeBit = "*";
}
else
{
extremeBit = " ";
}
thisGen = nextGen;
}
//Mutator
//only odd widths are allowed (for centering purposes).
bool Automaton::setDisplayWidth(int width)
{
//if width is invalid, return false
if(width > MAX_DISPLAY_WIDTH )
{
return false;
}
//check if width is odd
if(width % 2 == 1)
{
displayWidth = width;
return true;
}
//if width is even, make it odd
else
{
displayWidth = width--;
return false;
}
}