Message Boards Message Boards

Rubiks Cubes and OOP in Mathematica

Posted 5 years ago

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"}
POSTED BY: b3m2a1 ​ 
4 Replies

Is there a way of adding letter labels to each sticker on the cube so that they rotate together with the cube? I am trying to solve the following puzzle, and so far I don't really know how to approach it and want to visualize it. Here is the puzzle: Consider a Rubik's cube on which all stickers are white (no colors). A password (some combination of letters and numbers) is written on the stickers of such a Rubik's cube (one letter per sticker, the orientation of letters on every face is the same), and after that the cube is reshuffled. The task is to restore the password, i.e. solve the cube based not on the colors but on the orientation of stickers. In real life, some stickers will have a unique orientation (letters F, P, R, etc.) but some may have ambiguous orientation due to internal symmetries (letters N, S, T, etc.). I want to visualize that in Mathematica to play with this kind of puzzle. I am trying to get a feeling how many possible solutions exist and what the solution strategy could be.

POSTED BY: Dmytro Inosov

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!

POSTED BY: Moderation Team

Can the objects be easily serialized?

POSTED BY: Vincent Virgilio
Posted 5 years 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.

POSTED BY: b3m2a1 ​ 
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