Cell[CellGroupData[{Cell[BoxData[ RowBox[{ RowBox[{"lightstep", "[", RowBox[{"m_", ",", "p_", ",", "_", ",", "True"}], "]"}], ":=", RowBox[{"Abs", "[", RowBox[{"m", "-", RowBox[{"Total", "[", RowBox[{ RowBox[{ RowBox[{"RotateLeft", "[", RowBox[{ RowBox[{"ReplacePart", "[", RowBox[{ RowBox[{"Array", "[", RowBox[{ RowBox[{"0", "&"}], ",", RowBox[{"Dimensions", "[", "m", "]"}]}], "]"}], ",", RowBox[{"p", "->", "1"}]}], "]"}], ",", "#"}], "]"}], "&"}], "/@", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "0"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", "0"}], "}"}]}], "}"}]}], "]"}]}], "]"}]}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, InitializationCell->True, CellChangeTimes->{ 3.35696210375764*^9, {3.374257210478874*^9, 3.3742572158946733`*^9}, 3.374258787721485*^9, 3.759797935946867*^9}, CellID->1304029270], Cell[BoxData[ RowBox[{ RowBox[{"lightstep", "[", RowBox[{"m_", ",", "p_", ",", "q_", ",", "False"}], "]"}], ":=", RowBox[{"Abs", "[", RowBox[{"m", "-", RowBox[{"Total", "[", RowBox[{ RowBox[{ RowBox[{"RotateLeft", "[", RowBox[{ RowBox[{"ReplacePart", "[", RowBox[{ RowBox[{"Array", "[", RowBox[{ RowBox[{"0", "&"}], ",", RowBox[{"Dimensions", "[", "m", "]"}]}], "]"}], ",", RowBox[{"p", "->", "1"}]}], "]"}], ",", "#"}], "]"}], "&"}], "/@", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], "}"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"q", "+", "1"}], ">", RowBox[{"p", "[", RowBox[{"[", "1", "]"}], "]"}], ">", "1"}], ",", RowBox[{"{", RowBox[{"{", RowBox[{"1", ",", "0"}], "}"}], "}"}], ",", RowBox[{"{", "}"}]}], "]"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{"0", "<", RowBox[{"p", "[", RowBox[{"[", "1", "]"}], "]"}], "<", "q"}], ",", RowBox[{"{", RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", "0"}], "}"}], "}"}], ",", RowBox[{"{", "}"}]}], "]"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"1", "+", "q"}], ">", RowBox[{"p", "[", RowBox[{"[", "2", "]"}], "]"}], ">", "1"}], ",", RowBox[{"{", RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], "}"}], ",", RowBox[{"{", "}"}]}], "]"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{"0", "<", RowBox[{"p", "[", RowBox[{"[", "2", "]"}], "]"}], "<", "q"}], ",", RowBox[{"{", RowBox[{"{", RowBox[{"0", ",", RowBox[{"-", "1"}]}], "}"}], "}"}], ",", RowBox[{"{", "}"}]}], "]"}]}], "}"}], ",", "1"}], "]"}]}], "]"}]}], "]"}]}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, InitializationCell->True, CellChangeTimes->{ 3.35696210375764*^9, {3.374259517717643*^9, 3.3742595230175867`*^9}, {3.374259592641829*^9, 3.374259640092701*^9}, {3.374259693160904*^9, 3.374259706209607*^9}, {3.374259862321306*^9, 3.374259862846643*^9}, {3.374259904974065*^9, 3.374259908909771*^9}, {3.374259961685251*^9, 3.374260082589501*^9}, {3.374260138604089*^9, 3.374260145861401*^9}, {3.3742602219065247`*^9, 3.374260222674576*^9}, {3.374260367234603*^9, 3.3742603769316883`*^9}, 3.759797935946989*^9}, CellID->1540903848], Cell[BoxData[ RowBox[{"Manipulate", "[", RowBox[{ RowBox[{ RowBox[{"SeedRandom", "[", "seed", "]"}], ";", RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"mm", "=", RowBox[{"Array", "[", RowBox[{ RowBox[{ RowBox[{"{", "##", "}"}], "&"}], ",", RowBox[{"{", RowBox[{"q", ",", "q"}], "}"}]}], "]"}]}], ",", RowBox[{"m", "=", RowBox[{"RandomInteger", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"q", ",", "q"}], "}"}]}], "]"}]}], ",", RowBox[{"gg", "=", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}]}], "}"}], ",", RowBox[{"Dynamic", "@", RowBox[{"(", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"gg", "==", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}], ",", RowBox[{"{", "}"}], ",", RowBox[{ RowBox[{"m", "=", RowBox[{"lightstep", "[", RowBox[{"m", ",", "gg", ",", "q", ",", "pb"}], "]"}]}], ";", RowBox[{"gg", "=", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}]}]}], "]"}], ";", RowBox[{"Pane", "[", RowBox[{ RowBox[{"TableForm", "[", RowBox[{"MapIndexed", "[", RowBox[{ RowBox[{ RowBox[{"Button", "[", RowBox[{"\"\< \>\"", ",", RowBox[{"gg", "=", "#2"}], ",", RowBox[{"ImageSize", "->", RowBox[{"{", RowBox[{ RowBox[{"420", "/", RowBox[{"(", RowBox[{"q", "+", "1"}], ")"}]}], ",", RowBox[{"420", "/", RowBox[{"(", RowBox[{"q", "+", "1"}], ")"}]}]}], "}"}]}], ",", RowBox[{"Background", "->", RowBox[{ RowBox[{"{", RowBox[{"Black", ",", RowBox[{"RGBColor", "[", RowBox[{"1", ",", ".81", ",", "0"}], "]"}]}], "}"}], "[", RowBox[{"[", RowBox[{"1", "+", "#1"}], "]"}], "]"}]}]}], "]"}], "&"}], ",", "m", ",", RowBox[{"{", "2", "}"}]}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"450", ",", "450"}], "}"}], ",", RowBox[{"Alignment", "->", "Center"}]}], "]"}]}], ")"}]}]}], "]"}]}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"q", ",", "6", ",", "\"\\""}], "}"}], ",", "3", ",", "9", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"seed", ",", "1", ",", "\"\\""}], "}"}], ",", "1", ",", "100000", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "pb", ",", "False", ",", "\"\\""}], "}"}], ",", RowBox[{"{", RowBox[{"False", ",", "True"}], "}"}]}], "}"}], ",", " ", RowBox[{"SaveDefinitions", "->", "True"}], ",", RowBox[{"AutorunSequencing", "->", RowBox[{"{", "1", "}"}]}]}], "]"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35757176568782*^9, {3.36923182557301*^9, 3.36923183298958*^9}, 3.36937940170145*^9, 3.36938067565753*^9, {3.3709048062174873`*^9, 3.37090486206066*^9}, {3.370904911439968*^9, 3.3709049527896214`*^9}, {3.370905050520063*^9, 3.370905064109676*^9}, {3.370905139117625*^9, 3.3709051851882877`*^9}, {3.3709052571019697`*^9, 3.370905340097289*^9}, {3.370905400266094*^9, 3.3709054191982927`*^9}, {3.370906693272941*^9, 3.370906697473196*^9}, 3.3709068103867493`*^9, { 3.3710695573150883`*^9, 3.371069583248221*^9}, 3.371077599945698*^9, 3.371592579615781*^9, {3.371592631662272*^9, 3.371592637166875*^9}, {3.371983942422117*^9, 3.371983967630344*^9}, {3.372116851379757*^9, 3.3721168579192963`*^9}, {3.372907841771763*^9, 3.372907843684291*^9}, 3.372967399259552*^9, {3.373862712980719*^9, 3.3738627653028793`*^9}, {3.373862802213633*^9, 3.373862827318376*^9}, {3.374257170461165*^9, 3.37425717929953*^9}, 3.374257221607348*^9, 3.374258825432304*^9, {3.374258900663521*^9, 3.374258976492854*^9}, {3.374259009397696*^9, 3.374259017613535*^9}, {3.3851449937492833`*^9, 3.385145011194368*^9}, {3.3851451602186546`*^9, 3.3851451807782173`*^9}, {3.385145221086178*^9, 3.3851452240203967`*^9}, {3.386299556698708*^9, 3.386299564210033*^9}, {3.3862996144357595`*^9, 3.386299637099931*^9}, {3.3862996960988846`*^9, 3.386299779033928*^9}, {3.3862998177222595`*^9, 3.386299831983762*^9}, {3.3864436477492123`*^9, 3.3864436479989476`*^9}, 3.759797935935557*^9}, CellID->1496021318], Cell[BoxData[ TagBox[ StyleBox[ DynamicModuleBox[{$CellContext`pb$$ = False, $CellContext`q$$ = 6, $CellContext`seed$$ = 1, Typeset`show$$ = True, Typeset`bookmarkList$$ = {}, Typeset`bookmarkMode$$ = "Menu", Typeset`animator$$, Typeset`animvar$$ = 1, Typeset`name$$ = "\"untitled\"", Typeset`specs$$ = {{{ Hold[$CellContext`q$$], 6, "size"}, 3, 9, 1}, {{ Hold[$CellContext`seed$$], 1, "new game"}, 1, 100000, 1}, {{ Hold[$CellContext`pb$$], False, "board without boundaries"}, { False, True}}}, Typeset`size$$ = {450., {222., 228.}}, Typeset`update$$ = 0, Typeset`initDone$$, Typeset`skipInitDone$$ = False, $CellContext`q$242336$$ = 0, $CellContext`seed$242337$$ = 0, $CellContext`pb$242338$$ = False}, DynamicBox[Manipulate`ManipulateBoxes[ 1, StandardForm, "Variables" :> {$CellContext`pb$$ = False, $CellContext`q$$ = 6, $CellContext`seed$$ = 1}, "ControllerVariables" :> { Hold[$CellContext`q$$, $CellContext`q$242336$$, 0], Hold[$CellContext`seed$$, $CellContext`seed$242337$$, 0], Hold[$CellContext`pb$$, $CellContext`pb$242338$$, False]}, "OtherVariables" :> { Typeset`show$$, Typeset`bookmarkList$$, Typeset`bookmarkMode$$, Typeset`animator$$, Typeset`animvar$$, Typeset`name$$, Typeset`specs$$, Typeset`size$$, Typeset`update$$, Typeset`initDone$$, Typeset`skipInitDone$$}, "Body" :> (SeedRandom[$CellContext`seed$$]; DynamicModule[{$CellContext`mm = Array[{ SlotSequence[ 1]}& , {$CellContext`q$$, $CellContext`q$$}], \ $CellContext`m = RandomInteger[{0, 1}, {$CellContext`q$$, $CellContext`q$$}], \ $CellContext`gg = {0, 0}}, Dynamic[ If[$CellContext`gg == {0, 0}, {}, $CellContext`m = \ $CellContext`lightstep[$CellContext`m, $CellContext`gg, \ $CellContext`q$$, $CellContext`pb$$]; $CellContext`gg = {0, 0}]; Pane[ TableForm[ MapIndexed[ Button[" ", $CellContext`gg = #2, ImageSize -> { 420/($CellContext`q$$ + 1), 420/($CellContext`q$$ + 1)}, Background -> Part[{Black, RGBColor[1, 0.81, 0]}, 1 + #]]& , $CellContext`m, { 2}]], {450, 450}, Alignment -> Center]]]), "Specifications" :> {{{$CellContext`q$$, 6, "size"}, 3, 9, 1}, {{$CellContext`seed$$, 1, "new game"}, 1, 100000, 1}, {{$CellContext`pb$$, False, "board without boundaries"}, {False, True}}}, "Options" :> {AutorunSequencing -> {1}}, "DefaultOptions" :> {ControllerLinking -> True}], ImageSizeCache->{495., {293., 299.}}, SingleEvaluation->True], Deinitialization:>None, DynamicModuleValues:>{}, Initialization:>({$CellContext`lightstep[ Pattern[$CellContext`m, Blank[]], Pattern[$CellContext`p, Blank[]]] := Abs[$CellContext`m - Total[ Map[RotateLeft[ ReplacePart[ Array[0& , Dimensions[$CellContext`m]], $CellContext`p -> 1], #]& , {{0, 1}, {0, -1}, {0, 0}, {1, 0}, {-1, 0}}]]], $CellContext`lightstep[ Pattern[$CellContext`m, Blank[]], Pattern[$CellContext`p, Blank[]], Blank[], True] := Abs[$CellContext`m - Total[ Map[RotateLeft[ ReplacePart[ Array[0& , Dimensions[$CellContext`m]], $CellContext`p -> 1], #]& , {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}}]]], $CellContext`lightstep[ Pattern[$CellContext`m, Blank[]], Pattern[$CellContext`p, Blank[]], Pattern[$CellContext`q, Blank[]], False] := Abs[$CellContext`m - Total[ Map[RotateLeft[ ReplacePart[ Array[0& , Dimensions[$CellContext`m]], $CellContext`p -> 1], #]& , Flatten[{{{0, 0}}, If[$CellContext`q + 1 > Part[$CellContext`p, 1] > 1, {{1, 0}}, {}], If[ 0 < Part[$CellContext`p, 1] < $CellContext`q, {{-1, 0}}, {}], If[ 1 + $CellContext`q > Part[$CellContext`p, 2] > 1, {{0, 1}}, {}], If[ 0 < Part[$CellContext`p, 2] < $CellContext`q, {{ 0, -1}}, {}]}, 1]]]]}; Typeset`initDone$$ = True), SynchronousInitialization->True, UnsavedVariables:>{Typeset`initDone$$}, UntrackedVariables:>{Typeset`size$$}], "Manipulate", Deployed->True, StripOnInput->False], Manipulate`InterpretManipulate[1]]], "Output", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{3.759797935935788*^9}, CellID->247375766] }, {4}]]