Skip to content

Instantly share code, notes, and snippets.

@AlephAlpha
Last active November 24, 2016 04:45
Show Gist options
  • Save AlephAlpha/948cf2e16799efc6cdb4 to your computer and use it in GitHub Desktop.
Save AlephAlpha/948cf2e16799efc6cdb4 to your computer and use it in GitHub Desktop.
(*_*)
挪[左] = # //. {{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] -> 2] &;
按键 = If[格子 != 挪[#][格子], 格子 = 加块瓦[挪[#][格子]];
Which[格子 == 挪[#]@格子 & /@ And[左, 右, 上, 下],
状态 = "呜……动不了了……",
! FreeQ[格子, 2048], 状态 = "2048!"]] &;
格子 = 加块瓦[ConstantArray[1, {4, 4}]];
状态 = "";
EventHandler[Dynamic[ArrayPlot[Log2@格子/11,
ColorFunction -> "StarryNightColors",
ColorFunctionScaling -> False,
Mesh -> All,
Epilog -> {MapIndexed[
Text[If[#1 == 1, "", Style[#1, "Section"]], #2 - {0.5, 0.5}] &,
Transpose@Reverse@格子, {2}],
Text[Style[状态, "Section"], {2, 2}]}]],
{"LeftArrowKeyDown" :> 按键[左],
"RightArrowKeyDown" :> 按键[右],
"UpArrowKeyDown" :> 按键[上],
"DownArrowKeyDown" :> 按键[下]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment