1
$\begingroup$

I'm interested in modifying this interactive implementation of the Game of Life

http://demonstrations.wolfram.com/RealTimeSimulationOfTheGameOfLife/

to make it sensible to click events on the borders of the array. The array doesn't update when clicked in elements in the first/last rows and columns. I haven't been able to find out why.

enter image description here

Try running the code below.

Manipulate[
 Refresh[
  If[oldlen != len || oldp != p,
   oldlen = len;
   oldp = p;
   updating = False;
   config = Table[If[RandomReal[] < p, 1, 0], {len}, {len}];
   t = 0
   ];
  If[
   updating || onestep,
   t++;
   onestep = False;
   config = 
    Last[CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 
          2}}}, {1, 1}}, If[bg, {config, 0}, config], {{0, 1}}]];
   If[config == {},
         config = {{0}}
    ]
   ];
  EventHandler[
   Dynamic[
    ArrayPlot[Reverse@config,
     PlotLabel -> Row[{Style["t", Italic], " = " <> ToString[t]}],
     ImageSize -> {400, 400},
     AspectRatio -> Automatic,
      Mesh -> True]], 
   "MouseClicked" :> (If[
        1 < #[[1]] < Length[config] && 
         1 < #[[2]] < Length[First[config]], 
        config[[#[[2]], #[[1]]]] = 1 - config[[#[[2]], #[[1]]]]] &)[
     Ceiling@MousePosition["Graphics"]]]

  , UpdateInterval -> If[updating, 0, Infinity]
  ]
 ,
 {{updating, False, "run simulation"}, {True, False}},
 Button["update one step", onestep = True, ImageSize -> Medium],
 Button["reset", updating = False; 
  config = Table[If[RandomReal[] < p, 1, 0], {len}, {len}]; t = 0, 
  ImageSize -> Small],
 {{bg, False, "infinite space"}, {True, False}},
 {{len, 10, "edge length"}, 10, 200, 10, Appearance -> "Labeled", 
  ImageSize -> Tiny},
 {{oldlen, 50}, ControlType -> None},
 {{p, 0.1, "initial density"}, 0, 1, .01, Appearance -> "Labeled", 
  ImageSize -> Tiny},
 {{oldp, 0.1}, ControlType -> None},
 {{t, 0}, ControlType -> None},
 {{config, Table[If[RandomReal[] < p, 1, 0], {50}, {50}]}, 
  ControlType -> None},
 {{onestep, False}, ControlType -> None},
 ControlPlacement -> Left,
 TrackedSymbols :> {updating, onestep, config, len, t, bg, p}, 
 AutorunSequencing -> {3, 5}, SynchronousUpdating -> True
 ]
$\endgroup$
3
$\begingroup$

Use <= instead of < here:

"MouseClicked" :> (If[
     1 <= #[[1]] <= Length[config] && 
      1 <= #[[2]] <= Length[First[config]], 
     config[[#[[2]], #[[1]]]] = 1 - config[[#[[2]], #[[1]]]]] &)[
  Ceiling@MousePosition["Graphics"]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.