关灯游戏

非原创,几乎完全借鉴了此处的代码,我主要添加了几个关卡.

暑假里就尝试过搞这个,一直没成功,今天看到这个代码眼前一亮啊,感谢paradox2同学.

code:

stagelist = {{{1, 1}, {1, 2}, {1, 4}, {1, 5}, {2, 1}, {2, 3}, {2, 
     5}, {3, 2}, {3, 3}, {3, 4}, {4, 1}, {4, 3}, {4, 5}, {5, 1}, {5, 
     2}, {5, 4}, {5, 5}}, {{1, 1}, {1, 2}, {1, 4}, {2, 3}, {2, 4}, {2,
      5}, {3, 1}, {3, 2}, {4, 3}, {4, 4}, {4, 5}, {5, 1}, {5, 2}, {5, 
     4}}, {{1, 1}, {1, 2}, {1, 4}, {1, 5}, {2, 4}, {3, 1}, {3, 2}, {3,
      3}, {4, 1}, {4, 4}, {5, 4}, {5, 5}}, {{1, 1}, {1, 3}, {1, 
     5}, {2, 1}, {2, 3}, {2, 5}, {4, 3}, {4, 5}, {5, 2}, {5, 3}, {5, 
     5}}, {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 2}, {2,
      3}, {2, 4}, {2, 5}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {4, 
     1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 
     4}, {5, 5}}};

stage = Table[
   MapIndexed[If[MemberQ[i, #2], 0, 1] &, 
    Array[1 &, {5, 5}], {2}], {i, stagelist}];

Manipulate[
 EventHandler[Dynamic[ArrayPlot[matrix, Mesh -> True]], 
  "MouseDown" :> (clickPoint = MousePosition["Graphics"];
    currentPoint = {6 - Ceiling[clickPoint][[2]], 
      Ceiling[clickPoint][[1]]}; changeMethod[currentPoint];
    If[Count[Flatten[matrix], 1] == 25, MessageDialog["你赢了!"]])], 
 Row[Button[Style[#, 40, Bold], matrix = stage[[#]], 
     Background -> Pink, ImageSize -> {74, 58}] & /@ Range[5]], 
 Initialization -> {matrix = stage[[1]];
   changeMethod[point_] := 
    Module[{a, b}, a = point[[1]]; b = point[[2]];
     list = 
      Select[{{a + 1, b}, {a - 1, b}, {a, b - 1}, {a, b + 1}, {a, b}},
        0 < #[[1]] < 6 && 
         0 < #[[2]] < 6 &]; (matrix[[#[[1]], #[[2]]]] = 
         1 - matrix[[#[[1]], #[[2]]]]) & /@ list]}]

原文地址:https://www.cnblogs.com/mymma/p/2680505.html