Mathematica 566548 1056
编辑:这是一个完整的重写。我放弃尝试获取最短的代码,而是决定构建最有意义的功能。
r
指示网格中的行数。c
指示网格中的列数。
m
:地雷数量。
通过鼠标单击按钮来玩游戏。如果玩家单击地雷,则该单元会变成黑色,程序将显示“ You Lose!”(您输了!)。
复选框“ u”允许玩家随时查看完整的解决方案。标记“?” 和“!” 可以根据需要放置在任何单元格中。
DynamicModule[{s, x, f, l},
Manipulate[
Column[{
Grid[s],
If[u, Grid@f, Null]
}],
Grid[{{Control@{{r, 8}, 4, 16, 1, PopupMenu},
Control@{{c, 8}, 4, 16, 1, PopupMenu},
Control@{{m, 10}, 1, 50, 1, PopupMenu}},
{Button["New", i],
Control@{{e, 0}, {0 -> "play", 1 -> "?", 2 -> "!"}, SetterBar},
Control@{{u, False}, {True, False}}}}],
Deployed -> True,
Initialization :>
(p = ReplacePart;
q = ConstantArray;
z = Yellow;
w = White;
b := Array[Button[" ", v[{#, #2}], Background -> z] &, {r, c}];
a := RandomSample[l = Flatten[Array[List, {r, c}], 1], m];
d[m1_] :=
p[ListConvolve[BoxMatrix@1, p[q[0, {r, c}], (# -> 1) & /@ m1], 2,
0], (# -> "*") & /@ (x)];
n[y_] := Complement[Select[l, ChessboardDistance[y, #] == 1 &], x];
d[m1_] :=
p[ListConvolve[BoxMatrix@1, p[q[0, {r, c}], (# -> 1) & /@ m1], 2,
0], (# -> "*") & /@ (x)];
v[{r_, c_}] :=
Switch[e,
1, If[s[[r, c, 3, 2]] == z,
s = p[s, {{r, c, 1} -> If[s[[r, c, 1]] == "?", " ", "?"]}],
Null],
2, If[s[[r, c, 3, 2]] == z,
s = p[s, {{r, c, 1} -> If[s[[r, c, 1]] == "!", " ", "!"]}],
Null],
3, Null,
0, Switch[f[[r, c]],
"*", (Print["You lose!"]; (s = p[s, {r, c, 3, 2} -> Black])),
0, (s = p[s, {{r, c, 1} -> " ", {r, c, 3, 2} -> w}];
f = p[f, {{r, c} -> ""}]; v /@ n[{r, c}]),
" ", Null,
_, (s = p[s, {{r, c, 1} -> f[[r, c]], {r, c, 3, 2} -> w}])]];
i :=
(x = a;s = b;f = d[x]);i) ] ]
初始状态
稍后...