-
Notifications
You must be signed in to change notification settings - Fork 1
/
conway.cpp
458 lines (398 loc) · 13.6 KB
/
conway.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* This shares much with the 1D version, so some parts are still attributable to
* Rick Neff. The degree of departure is much higher, so look to boolnet.cpp to
* see what was originally there.
*/
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdarg>
using namespace std;
#define DEFAULT_CELLS_ROW 10
#define DEFAULT_CELLS_COL 10
#define DEFAULT_STEPS 10
#define LOOP_EDGES
#define TERM
#define BARRIER_VALUE 0
const bool prettyPrint = true;
#ifdef TERM
const char * pretty1 = "\E[44m \E[0m";
const char * pretty0 = " ";
#else
const char * pretty1 = "#";
const char * pretty0 = ".";
#endif
// Logic string for conways simple rules
const char * gameLogic =
"00000001000101100001011101111110000101100110100001111110111010000001011001101000011111101110100001101000100000001110100010000000000101100110100001111110111010000110100010000000111010001000000001101000100000001110100010000000100000000000000010000000000000000001011001101000011111101110100001101000100000001110100010000000011010001000000011101000100000001000000000000000100000000000000001101000100000001110100010000000100000000000000010000000000000001000000000000000100000000000000000000000000000000000000000000000";
/*******************************************************************
* A class that represents a Wire with a current and a new boolean
* value. The new value is set by the operation of a Cell, and it
* will become the current value after being updated, which typically
* happens right before showing (outputting) the value).
*******************************************************************/
class Wire
{
private:
bool mValue;
bool mNewValue;
public:
/****************************************************************
* Constructs a new wire instance with a default value (false);
****************************************************************/
Wire()
{
setValue(false);
setNewValue(false);
}
/****************************************************************
* Constructs a new wire instance with a given value.
****************************************************************/
Wire(bool pValue)
{
setValue(pValue);
setNewValue(false);
}
/******************************
* Gets this wire's value.
*******************************/
bool getValue() const { return mValue; }
/******************************
* Gets this wire's new value.
*******************************/
bool getNewValue() const { return mNewValue; }
/******************************
* Sets this wire's value
*******************************/
void setValue(bool pValue) { mValue = pValue; }
/******************************
* Sets this wire's new value
*******************************/
void setNewValue(bool pNewValue) { mNewValue = pNewValue; }
/******************************
* Sets this wire's value to its new value.
*******************************/
void setValueToNew() { mValue = mNewValue; }
/****************************************************
* Gets the character representing this wire's value.
****************************************************/
const char * getCharacterRepresentingValue() const
{
return
((prettyPrint) ?
(getValue() ? pretty1 : pretty0) :
(getValue() ? "1" : "0"));
}
};
/****************************************************************
* Outputs a Wire.
****************************************************************/
ostream& operator<<(ostream& os, const Wire& w)
{
os << w.getCharacterRepresentingValue();
return os;
}
/****************************************************************
* A base class representing a Boolean function of arbitrary degree.
****************************************************************/
class BooleanFunction
{
protected:
// If we are going to have a large number of these, storing the
// representation as integers is more efficent. Also: less work in most
// cases, especially as we are being passed the value as an integer anyway
bool * mValues;
int mValuesLen;
int degree;
public:
BooleanFunction(int deg)
{
degree = deg;
mValues = 0;
mValuesLen = 0;
}
~BooleanFunction()
{
if (mValues)
delete mValues;
}
// Note: p must point to an array of length degree
bool evaluate(bool * p)
{
// This builds our index, basically converting our inputs into an
// integer as if they were the binary representation of the index of an
// array.
int index = 0;
int mask = 0x1;
for (int i = 0; i < degree; ++i)
{
if (p[i])
index |= mask;
mask <<= 1;
}
return mValues [index];
}
void populate(string values)
{
mValuesLen = values.length();
if (mValues)
delete mValues;
mValues = new bool [mValuesLen];
for (int i = 0; i < mValuesLen; ++i)
mValues[i] = (values[i] == '1');
}
int getDegree()
{
return degree;
}
};
/****************************************************************
* A Cell implementing a boolean function of arbitrary degree
* (How many of them are there?)
****************************************************************/
class Cell
{
protected:
BooleanFunction* mBooleanFunction;
public:
/****************************************************************
* Changes were made here to allow working with an arbitrary number of
* wires. Primarily it was set up to facilitate simpler variable passing
* between this and the BooleanFunction. It will also reduce errors caused
* by mis-judging the output wire by maping that to an exclusive
* input. Finally the wires are now passed as pointers, to comply with stdarg
*
* Constructs a new Cell instance with an arbitrary number of wires
* and a pointer to a Boolean Function of arbitrary degree.
*
* This merely has the side effect of computing the boolean
* function on the wire inputs, storing the result in the
* first (output) wire
****************************************************************/
Cell(BooleanFunction* pBooleanFunction, Wire & output, ...)
{
setBooleanFunction(pBooleanFunction);
// These feed all of our input wire values into an array of bools for
// the boolean function to use
int numWires = pBooleanFunction->getDegree();
bool * wireVals = new bool [numWires];
va_list ap;
va_start (ap, output);
for (int i = 0; i < numWires; ++i)
{
Wire * argWire = va_arg (ap, Wire *);
wireVals[i] = argWire->getValue();
}
va_end(ap);
output.setNewValue(getBooleanFunction()->evaluate(wireVals));
}
/****************************************************************
* Gets the pointer to the Boolean Function of arbitrary degree.
****************************************************************/
BooleanFunction* getBooleanFunction()
{
return mBooleanFunction;
}
/****************************************************************
* Sets the pointer to the Boolean Function of arbitrary degree.
****************************************************************/
void setBooleanFunction(BooleanFunction* pBooleanFunction)
{
mBooleanFunction = pBooleanFunction;
}
};
void runSimulation (string edenFile, int numCellsRow, int numCellsCol, int numSteps)
{
cerr << "F:" << edenFile
<< " on " << numCellsRow << "x" << numCellsCol
<< " cells for ";
if (~numSteps)
cerr << numSteps;
else
cerr << "unlimited";
cerr << " generations\n";
// TODO: create numCells Wires.
Wire ** wires = new Wire * [numCellsRow];
for (int i = 0; i < numCellsRow; ++i)
wires[i] = new Wire[numCellsCol];
ifstream eden (edenFile.c_str());
if (eden.fail())
{
cerr << "Unable to open start file\n";
return;
}
{
int x = 0;
int y = 0;
string tempS;
char tempC;
// I'm doing this this way so that I can accurately detect the end of
// line. This lets me only fill out part of an eden file if I have a
// smaller initial condition. I can fill out each line only as far as the
// last cell I want to mark as live.
while (eden.good() && x < numCellsRow)
{
getline (eden, tempS);
stringstream ss (tempS);
while (ss.good() && y < numCellsCol)
{
ss >> tempC;
if (tempC == 'x')
wires[x][y].setValue(1);
++y;
}
y = 0;
++x;
}
}
eden.close();
BooleanFunction rule(9);
rule.populate(gameLogic);
#ifndef LOOP_EDGES
for (int i = 0; i < numCellsRow; ++i)
{
wires[0][i].setValue(BARRIER_VALUE);
wires[numCellsCol - 1][i].setValue(BARRIER_VALUE);
}
for (int i = 0; i < numCellsCol; ++i)
{
wires[i][0].setValue(BARRIER_VALUE);
wires[i][numCellsRow - 1].setValue(BARRIER_VALUE);
}
#endif
// Print out the initial board
for (int x = 0; x < numCellsRow; ++x)
{
for (int y = 0; y < numCellsCol; ++y)
cout << wires[x][y];
cout << endl;
}
for (int step = 0; (!~numSteps) || step < numSteps; ++step)
{
// TODO: A) create the network of Cells implementing the BooleanFunction
// and connect them to the numCells Wires, as per the write-up.
#ifdef LOOP_EDGES
for (int x = 0; x < numCellsRow; ++x)
for (int y = 0; y < numCellsCol; ++y)
Cell(&rule, wires[x][y],
&wires[x ? x-1 : numCellsRow - 1][y ? y-1 : numCellsCol - 1],
&wires[x ? x-1 : numCellsRow - 1][y],
&wires[x ? x-1 : numCellsRow - 1][(y+1) % numCellsCol],
&wires[x][y ? y-1 : numCellsCol - 1],
&wires[x][y],
&wires[x][(y+1) % numCellsCol],
&wires[(x+1) % numCellsRow][y ? y-1 : numCellsCol - 1],
&wires[(x+1) % numCellsRow][y],
&wires[(x+1) % numCellsRow][(y+1) % numCellsCol]);
#else
for (int i = 0; i < numCellsRow; ++i)
{
wires[0][i].setNewValue(BARRIER_VALUE);
wires[numCellsCol - 1][i].setNewValue(BARRIER_VALUE);
}
for (int i = 0; i < numCellsCol; ++i)
{
wires[i][0].setNewValue(BARRIER_VALUE);
wires[i][numCellsRow - 1].setNewValue(BARRIER_VALUE);
}
for (int x = 1; x < numCellsRow - 1; ++x)
for (int y = 1; y < numCellsCol - 1; ++y)
Cell(&rule, wires[x][y],
&wires[x+1][y+1],
&wires[x+1][y],
&wires[x+1][y-1],
&wires[x][y+1],
&wires[x][y],
&wires[x][y-1],
&wires[x-1][y+1],
&wires[x-1][y],
&wires[x-1][y-1]);
#endif
// TODO: B) update values to new values and show (output) all wires.
// Convert the values and print
for (int x = 0; x < numCellsRow; ++x)
{
for (int y = 0; y < numCellsCol; ++y)
{
wires[x][y].setValueToNew();
cout << wires[x][y];
}
cout << endl;
}
}
for (int i = 0; i < numCellsRow; ++i)
delete wires[i];
delete wires;
}
/****************************************************************
* Main is for you to explore...
****************************************************************/
int main(int argc, char* argv[])
{
if (argc <= 1)
{
cout << "Usage: " << argv[0] << " function [numCells row] [numCells col] [numSteps]" << endl
<< "or" << endl
<< "Input a series of commands, one per line, ending with Ctrl-D" << endl;
}
// Variables are common between the cases
string sourceFile;
//Fairly sane defaults
int numCellsRow = DEFAULT_CELLS_ROW;
int numCellsCol = DEFAULT_CELLS_COL;
int numSteps = DEFAULT_STEPS;
// Handling cli input
if (argc > 1)
{
sourceFile = argv[1];
if (argc > 2)
numCellsRow = atoi(argv[2]);
if (argc > 3)
numCellsCol = atoi(argv[3]);
if (argc > 4)
numSteps = atoi(argv[4]);
runSimulation (sourceFile, numCellsRow, numCellsCol, numSteps);
}
// Handle input from terminal
else
{
while (cin.good())
{
// Pass a line from cin to a string stream for parsing
string temp;
getline(cin, temp);
stringstream ss (temp);
// If no number, ignore this line
ss >> sourceFile;
if (ss.fail()) continue;
// I noticed a tendency to put an extra space after the input, so I
// am handling this by ignoring it before each existance test on the
// stream
while (isspace(ss.peek())) ss.ignore();
// If no cells, use default. If bad input, ignore this line
if (ss.eof())
numCellsRow = DEFAULT_CELLS_ROW;
else
ss >> numCellsRow;
if (ss.fail()) continue;
if (!ss.eof())
while (isspace(ss.peek())) ss.ignore();
// If no cells, use default. If bad input, ignore this line
if (ss.eof())
numCellsCol = DEFAULT_CELLS_COL;
else
ss >> numCellsCol;
if (ss.fail()) continue;
if (!ss.eof())
while (isspace(ss.peek())) ss.ignore();
// If no steps, use default. If bad input, ignore this line
if (ss.eof())
numSteps = DEFAULT_STEPS;
else
ss >> numSteps;
if (ss.fail()) continue;
runSimulation (sourceFile, numCellsRow, numCellsCol, numSteps);
}
}
return 0;
}