44bool GameStep(vector<bool> *
b[],
int nx,
int ny);
50bool InitMFEM(vector<bool> &
b,
int nx,
int ny);
52int main(
int argc,
char *argv[])
63 bool visualization = 1;
66 args.
AddOption(&nx,
"-nx",
"--num-elems-x",
67 "Number of elements in the x direction.");
68 args.
AddOption(&ny,
"-ny",
"--num-elems-y",
69 "Number of elements in the y direction.");
70 args.
AddOption(&r,
"-r",
"--random-fraction",
71 "Fraction of randomly chosen live cells.");
72 args.
AddOption(&rs,
"-rs",
"--random-seed",
73 "Seed for the random number generator.");
74 args.
AddOption(&sketch_pad_params,
"-sp",
"--sketch-pad",
75 "Specify the starting coordinates and values on a grid"
76 " of cells. The values can be 0, 1, or 2. Where 0 and 1"
77 " indicate cells that are off or on and 2 represents a"
78 " newline character.");
79 args.
AddOption(&blinker_params,
"-b",
"--blinker",
80 "Specify the starting coordinates and orientation (0 or 1)"
81 " of the blinker. Multiple blinkers can be specified as "
82 "'x0 y0 o0 x1 y1 o1 ...'.");
83 args.
AddOption(&glider_params,
"-g",
"--glider",
84 "Specify the starting coordinates and "
85 "orientation (0,1,2, or 3) of the glider. "
86 "Multiple gliders can be specified as "
87 "'x0 y0 o0 x1 y1 o1 ...'.");
88 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
90 "Enable or disable GLVis visualization.");
91 args.
AddOption(&visport,
"-p",
"--send-port",
"Socket for GLVis.");
114 vector<bool> * vbp[2];
115 vector<bool> vb0(len);
116 vector<bool> vb1(len);
127 seed = (
unsigned int)rand();
131 seed = (
unsigned int)rs;
133 cout <<
"Using random seed: " << seed << endl;
140 for (
int i=0; i<len; i++)
149 for (
int i=0; i<len; i++)
154 if ( sketch_pad_params.
Size() > 2 )
158 if ( blinker_params.
Size() > 0 && (blinker_params.
Size() % 3 == 0 ) )
162 if ( glider_params.
Size() > 0 && (glider_params.
Size() % 3 == 0 ) )
164 init =
InitGlider(vb0, nx, ny, glider_params);
187 cout << endl <<
"Running the Game of Life..." << flush;
190 bool is_stable =
false;
191 while ( is_good && visualization && !is_stable )
197 std::swap(vbp[0], vbp[1]);
200 is_good = sol_sock.good();
202 if (visualization && is_good )
204 sol_sock <<
"solution\n" << mesh << x << flush;
209 sol_sock <<
"keys Ajlm\n";
210 sol_sock <<
"view 0 0\n";
211 sol_sock <<
"zoom 1.9\n";
212 sol_sock <<
"palette 24\n";
217 sol_sock <<
"valuerange 0 1\n";
222 cout <<
"done." << endl;
226 ofstream mesh_ofs(
"life.mesh");
227 mesh_ofs.precision(8);
228 mesh.
Print(mesh_ofs);
229 ofstream sol_ofs(
"life.gf");
230 sol_ofs.precision(8);
236inline int index(
int i,
int j,
int nx,
int ny)
238 return ((j + ny) % ny) * nx + ((i + nx) % nx);
243 bool is_stable =
true;
244 for (
int j=0; j<ny; j++)
246 for (
int i=0; i<nx; i++)
249 (int)(*
b[0])[
index(i+0,j+0,nx,ny)] +
250 (int)(*
b[0])[
index(i+1,j+0,nx,ny)] +
251 (int)(*
b[0])[
index(i+1,j+1,nx,ny)] +
252 (int)(*
b[0])[
index(i+0,j+1,nx,ny)] +
253 (int)(*
b[0])[
index(i-1,j+1,nx,ny)] +
254 (int)(*
b[0])[
index(i-1,j+0,nx,ny)] +
255 (int)(*
b[0])[
index(i-1,j-1,nx,ny)] +
256 (int)(*
b[0])[
index(i+0,j-1,nx,ny)] +
257 (int)(*
b[0])[
index(i+1,j-1,nx,ny)];
261 (*
b[1])[
index(i,j,nx,ny)] =
true;
267 (*
b[1])[
index(i,j,nx,ny)] =
false;
270 is_stable &= (*
b[1])[
index(i,j,nx,ny)] == (*
b[0])[
index(i,j,nx,ny)];
278 for (
int i=0; i<n; i++)
286 for (
int i=0; i<params.
Size()/3; i++)
288 int cx = params[3 * i + 0];
289 int cy = params[3 * i + 1];
290 int ornt = params[3 * i + 2];
295 b[
index(cx+0,cy+1,nx,ny)] =
true;
296 b[
index(cx+0,cy+0,nx,ny)] =
true;
297 b[
index(cx+0,cy-1,nx,ny)] =
true;
300 b[
index(cx+1,cy+0,nx,ny)] =
true;
301 b[
index(cx+0,cy+0,nx,ny)] =
true;
302 b[
index(cx-1,cy+0,nx,ny)] =
true;
311 for (
int i=0; i<params.
Size()/3; i++)
313 int cx = params[3 * i + 0];
314 int cy = params[3 * i + 1];
315 int ornt = params[3 * i + 2];
320 b[
index(cx-1,cy+0,nx,ny)] =
true;
321 b[
index(cx+0,cy+1,nx,ny)] =
true;
322 b[
index(cx+1,cy-1,nx,ny)] =
true;
323 b[
index(cx+1,cy+0,nx,ny)] =
true;
324 b[
index(cx+1,cy+1,nx,ny)] =
true;
327 b[
index(cx+0,cy-1,nx,ny)] =
true;
328 b[
index(cx-1,cy+0,nx,ny)] =
true;
329 b[
index(cx-1,cy+1,nx,ny)] =
true;
330 b[
index(cx+0,cy+1,nx,ny)] =
true;
331 b[
index(cx+1,cy+1,nx,ny)] =
true;
334 b[
index(cx+1,cy+0,nx,ny)] =
true;
335 b[
index(cx+0,cy-1,nx,ny)] =
true;
336 b[
index(cx-1,cy-1,nx,ny)] =
true;
337 b[
index(cx-1,cy+0,nx,ny)] =
true;
338 b[
index(cx-1,cy+1,nx,ny)] =
true;
341 b[
index(cx+0,cy+1,nx,ny)] =
true;
342 b[
index(cx+1,cy+0,nx,ny)] =
true;
343 b[
index(cx-1,cy-1,nx,ny)] =
true;
344 b[
index(cx+0,cy-1,nx,ny)] =
true;
345 b[
index(cx+1,cy-1,nx,ny)] =
true;
360 for (
int i=2; i<params.
Size(); i++)
362 if ( params[i]/2 == 1 )
369 b[
index(cx+ox,cy+oy,nx,ny)] = (bool)params[i];
380 int wx = (nx >= 23) ? 23 : 5;
381 int hy = (ny >= 7) ? 7 : 5;
389 for (
int j=0; j<hy; j++)
391 b[
index(ox + 0, oy+j,nx,ny)] =
true;
392 b[
index(ox + 4, oy+j,nx,ny)] =
true;
393 b[
index(ox + 6, oy+j,nx,ny)] =
true;
394 b[
index(ox + 12, oy+j,nx,ny)] =
true;
395 b[
index(ox + 18, oy+j,nx,ny)] =
true;
396 b[
index(ox + 22, oy+j,nx,ny)] =
true;
398 for (
int i=1; i<5; i++)
400 b[
index(ox + 6 + i, oy + hy - 1,nx,ny)] =
true;
401 b[
index(ox + 12 + i, oy + 0,nx,ny)] =
true;
402 b[
index(ox + 12 + i, oy + hy - 1,nx,ny)] =
true;
404 for (
int i=1; i<4; i++)
406 b[
index(ox + 6 + i, oy + hy/2,nx,ny)] =
true;
407 b[
index(ox + 12 + i, oy + hy/2,nx,ny)] =
true;
409 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
410 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
411 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
413 b[
index(ox + 19, oy + hy - 2,nx,ny)] =
true;
414 b[
index(ox + 20, oy + hy - 3,nx,ny)] =
true;
415 b[
index(ox + 21, oy + hy - 2,nx,ny)] =
true;
423 for (
int j=0; j<hy; j++)
425 b[
index(ox + 0, oy+j,nx,ny)] =
true;
426 b[
index(ox + 4, oy+j,nx,ny)] =
true;
428 b[
index(ox + 1, oy + hy - 2,nx,ny)] =
true;
429 b[
index(ox + 2, oy + hy - 3,nx,ny)] =
true;
430 b[
index(ox + 3, oy + hy - 2,nx,ny)] =
true;
435 b[
index(nx/2,ny/2,nx,ny)] =
true;
int Size() const
Return the logical size of the array.
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Class for grid function - Vector with associated FE space.
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Arbitrary order "L2-conforming" discontinuous finite elements.
virtual void Print(std::ostream &os=mfem::out, const std::string &comments="") const
static Mesh MakeCartesian2D(int nx, int ny, Element::Type type, bool generate_edges=false, real_t sx=1.0, real_t sy=1.0, bool sfc_ordering=true)
Creates mesh for the rectangle [0,sx]x[0,sy], divided into nx*ny quadrilaterals if type = QUADRILATER...
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
bool Good() const
Return true if the command line options were parsed successfully.
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
bool InitMFEM(vector< bool > &b, int nx, int ny)
void ProjectStep(const vector< bool > &b, GridFunction &x, int n)
int index(int i, int j, int nx, int ny)
bool InitBlinker(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
bool GameStep(vector< bool > *b[], int nx, int ny)
bool InitGlider(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)
bool InitSketchPad(vector< bool > &b, int nx, int ny, const Array< int > ¶ms)