|
楼主 |
青青子衿
发表于 2021-7-31 16:58
本帖最后由 青青子衿 于 2021-8-3 00:56 编辑 
Light_Out_Express_Editon.- k = 5;
- Manipulate[
- Module[{},
- EventHandler[
- Dynamic[ArrayPlot[matrix, Mesh -> True,
- PlotRangePadding -> Scaled[0.05]]],
- "MouseDown" :> Module[{}, clickPoint = MousePosition["Graphics"];
- AppendTo[points,
- If[1 <= #1 <= k && 1 <= #2 <= k & @@ #, steps = 0;
- backup = {}; #, {}] &[{k + 1 - #2, #1} & @@
- Ceiling@clickPoint]];
- points = DeleteCases[points, {}];
- changeMethod @@ points;
- If[Count[Flatten[matrix], 1] == k^2, MessageDialog["Well done!"]]
- ]
- ]
- ],
- Row@
- {Button["New", matrix = Array[0 &, {k, k}]; points = {};
- backup = {};],
- Button["Undo",
- If[points != {}, changeMethod@Last@points;
- AppendTo[backup, Last@points]; points = Most@points;
- steps += 1;]],
- Button["Redo",
- If[steps > 0, changeMethod@Last@backup;
- AppendTo[points, Last@backup]; backup = Most@backup;
- steps -= 1;]]},
- Initialization -> {
- matrix = Array[0 &, {k, k}]; points = {}; backup = {}; steps = 0;
- Clear[fun];
- fun[k_, u_, v_] :=
- fun[k, u, v] =
- SparseArray[{i_, j_} /; Norm[{i, j} - {u, v}] <= 1 :> 1, {k, k}];
- changeMethod[{u_, v_}] := (matrix = Abs[matrix - fun[k, u, v]]);
- changeMethod[points___] := (matrix = Array[0 &, {k, k}];
- Do[changeMethod[i], {i, {points}}]);
- }]
复制代码 |
|