Message Boards Message Boards

GROUPS:

Rubiks Cubes and OOP in Mathematica

Posted 2 months ago
680 Views
|
3 Replies
|
7 Total Likes
|

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"[]

enter image description here

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}]

enter image description here

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
 ]

enter image description here

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"}
3 Replies

Can the objects be easily serialized?

Posted 2 months ago

If you look inside the things:

RubiksCubes`RubiksCube @ <|
    "Size" -> 4,
    "Origin" -> {0., 0., 0.},
    "Colors" -> {
       RGBColor[0.4, 0.4, 0.4],
       RGBColor[0.291989, 0.437977, 0.888609],
       RGBColor[0.800498, 0.201504, 0.192061],
       RGBColor[0.578462, 0.85539, 0.408855],
       RGBColor[0.658708, 0.492173, 0.842842],
       RGBColor[0.8913, 0.631904, 0.627399],
       RGBColor[0.546138, 0.844244, 0.892092]
    },
    "Cuboids" -> {
       {
         {
          RubiksCubes`Private`RubiksCuboid[
              <|
                 "Coordinates" -> {\[Ellipsis] " -> {1, 3, 5, 1, 1, 1},
              "Version" -> 1
          |>,
          RubiksCubes`Private`RubiksCuboid @ <|
              "Coordinates" -> {
                 {1.025, 1.025, 1.025},
                 {1.025, 1.025, 1.975},
                 {1.025, 1.975, 1.025},
                 {1.025, 1.975, 1.975},
                 {1.975, 1.025, 1.025},
                 {1.975, 1.025, 1.975},
                 {1.975, 1.975, 1.025},
                 {1.975, 1.975, 1.975}
              },
              "ColorIndices" -> {7, 3, 5, 1, 1, 1},
              "Version" -> 1
          |>
         }
       }
    },
    "Version" -> 1
|>

You see that in fact they're stored as just and Association of data, basically. Now, of course, Mathematica does treat every expression as an object that you can attach properties to and so if you use SetProperty on the thing to store its properties you won't actually recover those since I treat them as invisible state.

But in general, yes, these serialize very well.

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming, and consider contributing your work to the The Notebook Archive!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract