1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 挪[左] = # //. {{x___, 1, a_ /; a > 1, y___} :> {x, a, 1, y}, {x___, a_ /; a > 1, a_, y___} :> {x, 2 抓住[a], 1, y}} /. 抓住[a_] :> a &; 挪[上] = Composition[Transpose, 挪[左], Transpose]; 挪[下] = Composition[Reverse, 挪[上], Reverse]; 挪[右] = Composition[Transpose, 挪[下], Transpose]; 加块瓦 = ReplacePart[#, RandomChoice@Position[#, 1] -> RandomChoice[{2, 3}]] &; 按键 = If[格子 != 挪[#][格子], 格子 = 加块瓦[挪[#][格子]]; 总分 = Total[Total[格子]]; Which[And @@ (格子 == 挪[#][格子] & /@ {左, 右, 上, 下}), 状态 = "你的得分为" ToString[总分]]] &; 格子 = 加块瓦[ConstantArray[1, {5, 5}]]; 状态 = ""; EventHandler[ Dynamic[ArrayPlot[Log2@格子/17, ColorFunction -> Hue, ColorFunctionScaling -> False, Mesh -> All, Epilog -> {MapIndexed[ Text[If[#1 == 1, "", Style[#1, "Section"]], #2 - {0.5, 0.5}] &, Transpose@Reverse@格子, {2}], Text[Style[状态, Blue, 24], {2.5, 2}]}]], {"LeftArrowKeyDown" :> 按键[左], "RightArrowKeyDown" :> 按键[右], "UpArrowKeyDown" :> 按键[上], "DownArrowKeyDown" :> 按键[下]}]
|