I recently got nerd sniped on a question on Stack Exchange
Originally I just wanted to show off my immutable OOP framework that uses a bunch of Mathematica's built in features to make something with a nice interface like CloudExpression
but then that devolved into redoing the entirety of Roman Maeder's Rubiks Cube Demo as an object.
To make this work you need my InterfaceObjects
package, which implements Mathematica OOP. You can single-line install that here.
Then load the Rubik's cube package off GitHub here (you can also go there to see how this OOP package works):
Get["https://github.com/b3m2a1/mathematica-tools/raw/master/RubiksCube.wl"]
Now we can make an object:
new = RubiksCube[]
Visualize it:
new@"Show"[]
Make a new one of a different size and change its colors:
r1 = RubiksCube["Size" -> 4];
r1@"Colors" = ColorData["Atoms"] /@ {6, 7, 8, 9, 11, 13, 18};
r1@"Show"[Method -> {"ShrinkWrap" -> True}]
Or make two and plot them side by side with some twists:
r2 = RubiksCube["Origin" -> {10, 0, 0}, "Size" -> 10];
Show[
r1@"Twist"[.5, {"Y", 2}]@"Twist"[.5, {"Y", 4}]@"Show"[],
r2@"Show"[],
PlotRange -> All
]
You can also Manipulate
the twisting, if you want:
Manipulate[
Fold[
#@"Twist"[#2[[1]], #2[[2]]] &,
new,
Thread[
{
{b, f, l, r, d, u},
{"Back", "Front", "Left", "Right", "Down", "Up"}
}
]
]@"Show"[],
{b, 0, 2 ?, .01},
{f, 0, 2 ?, .01},
{l, 0, 2 ?, .01},
{r, 0, 2 ?, .01},
{d, 0, 2 ?, .01},
{u, 0, 2 ?, .01},
DisplayAllSteps -> True
]
The OOP interface itself is actually very nice, as it allows us to only have to provide and document a single symbol and then some "Methods"
, which actually are discoverable by name as well, along with the "Properties"
:
r1@"Methods"
{"Show", "Twist"}
r1@"Properties"
{"Size", "Origin", "Colors", "Cuboids", "Version", "Properties", "Methods"}