繁體
|
簡體
Sclub交友聊天~加入聊天室當版主
(檢舉)
分享
新浪微博
QQ空间
人人网
腾讯微博
Facebook
Google+
Plurk
Twitter
Line
标题:
用Mathematica玩《关灯游戏》
[打印本页]
作者:
青青子衿
时间:
2014-6-28 14:16
标题:
用Mathematica玩《关灯游戏》
本帖最后由 青青子衿 于 2021-8-1 17:49 编辑
Lights Out(关灯游戏)是一款益智游戏,也算一种“康威生命游戏”。
https://mathworld.wolfram.com/LightsOutPuzzle.html
https://projecteuler.net/problem=707
Light_Out_Simplified_Editon.
k = 5;
Manipulate[
Module[{},
EventHandler[Dynamic[ArrayPlot[matrix, Mesh -> True]],
"MouseDown" :> Module[{}, clickPoint = MousePosition["Graphics"];
currentPoint = {k + 1 - Ceiling[clickPoint][[2]],
Ceiling[clickPoint][[1]]}; changeMethod[currentPoint];
If[Count[Flatten[matrix], 1] == k^2,
MessageDialog["Well done!"]]]]],
Button["New", matrix = Array[0 &, {k, k}]],
Initialization -> {matrix = Array[0 &, {k, k}];
changeMethod[point_] :=
Module[{a, b}, a = point[[1]]; b = point[[2]];
jud[k_, a_, b_] := If[1 <= a <= k && 1 <= b <= k,
Select[{{a + 1, b}, {a - 1, b}, {a, b - 1}, {a, b + 1}, {a,
b}}, 0 < #[[1]] < k + 1 && 0 < #[[2]] < k + 1 &],
Null];
list =
jud[k, a,
b]; (matrix[[#[[1]], #[[2]]]] =
1 - matrix[[#[[1]], #[[2]]]]) & /@ list]}]
复制代码
http://hi.baidu.com/guishidan/item/fd04601ba978f8f365eabfb1
作者:
青青子衿
时间:
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}}]);
}]
复制代码
欢迎光临 悠闲数学娱乐论坛(第2版) (http://kuing.orzweb.net/)
Powered by Discuz! 7.2