Interactive Rubik’s Cube Solver & ML Explainer 🧩

Published:

Rubik's Cube Simulator & Solver
Initializing 3D Environment...
Manual Moves
Status / Move Feed
Ready. Cube is Solved.

How It Works

Solving a Rubik’s Cube programmatically is a classic problem in computer science. There are two primary ways algorithms solve this puzzle:

  1. Rule-Based Search (Kociemba’s Algorithm): Solves the Rubik’s cube in 20 moves or less by breaking down the $4.3 \times 10^{19}$ states into subgroups.
  2. Reinforcement Learning (RL): Using deep neural networks to learn representations of state orientation and using Deep Q-Learning or Pathfinding with Value Iteration to find optimal paths back to the solved state.

The Python Desktop App (Option B)

If you want to train your own Reinforcement Learning Agent or run a native interactive solver on your computer, check out our Python implementation inside the python_app subdirectory.

To get started, clone the repository and run:

cd _MachineLearningProjects/05_Rubik/python_app
pip install -r requirements.txt
python gui.py

Reinforcement Learning Implementation Details

We define the Rubik’s Cube state space as a flattened vector representing color mapping of stickers. The reward structure:

  • Solved State: $+100$
  • Non-solved State: $-1$ per move to encourage finding the shortest path.

Using Deep Q-Networks (DQN) or double-DQN with experience replay, the agent learns sequence behaviors to untangle the cube.


📘 Step-by-Step Guide to Solving a Rubik’s Cube (Beginner’s Method)

Understanding Cube Notation 📖

To follow Rubik’s Cube algorithms, you need to understand Singmaster Notation. Each letter represents a $90^\circ$ clockwise rotation of a specific face (as if you are looking directly at that face):

U (Up) Clockwise top layer
D (Down) Clockwise bottom layer
F (Front) Clockwise front face
R (Right) Clockwise right layer
L (Left) Clockwise left layer

Suffix Modifiers:

  • Prime ($’$) Suffix (e.g., $R’$, $U’$): Rotate the face counter-clockwise (e.g., $R’$ is Right counter-clockwise).
  • Number $2$ Suffix (e.g., $F2$, $U2$): Rotate the face $180^\circ$ (direction does not matter since two turns result in the same position).

If you want to solve the Rubik’s Cube manually, here is a breakdown of the standard Layer-by-Layer method:


1. The White Cross ⬜

Find the yellow center piece. Move the 4 white edge pieces around the yellow center to form a “daisy”. Then, align the non-white color of each edge with its matching center piece and rotate that layer $180^\circ$ (a double turn) down to form a clean white cross on the bottom where the white edges match their side centers.

Daisy Pattern Target
Bottom White Cross Target

2. The First Layer Corners 🧩

Find white corner pieces on the top layer. Position them above the slot they belong to (determined by the other two colors of the corner). Execute the key algorithm (the Sexy Move) until the corner is correctly placed: \(\text{Algorithm: } R \ U \ R' \ U'\)

Solved First Layer Target (Bottom Face + Edges match Center)
✓ Bottom layer is solid White
✓ T-shapes formed on all 4 sides

3. Middle Layer (Second Layer Edges) 🟩

Find edge pieces on the top layer that do not contain yellow. Align the front color of the edge with its matching center.

  • To insert the edge to the Right: \(\text{Algorithm: } U \ R \ U \ R' \ U' \ F' \ U' \ F\)
  • To insert the edge to the Left: \(\text{Algorithm: } U' \ L' \ U' \ L \ U \ F \ U \ F'\)

4. Yellow Cross (Orienting Edges) 🟨

Look at the top face. You will have a dot, an ‘L’ shape, a horizontal line, or a cross. Repeat this algorithm to progress towards the cross: \(\text{Algorithm: } F \ R \ U \ R' \ U' \ F'\)

1. Dot Case
2. L-Shape Case
3. Line Case
4. Cross Target

5. Aligning the Yellow Cross (Make a Cross Correctly with the 2nd Layer Below) 🟨

Once you have the yellow cross, you must align its side colors with the center pieces of the middle layer.

Turn the top layer to match as many side colors to their corresponding centers as possible.

  • If two adjacent edges match (e.g., Back and Right): Hold the cube so the matching edges are at the Back and Right faces, then execute the algorithm (known as Sune): \(\text{Algorithm: } R \ U \ R' \ U \ R \ U2 \ R' \ [U]\)
  • If two opposite edges match: Hold them on the Left and Right faces, execute the algorithm once, then re-align and follow the adjacent edges rule.
Before: Mismatched Edges
Right edge (Orange) does not match center (Red).
After: Aligned Cross
Top face shows a solved yellow cross; side colors not yet aligned.
After: Aligned Edges
All four edge colors match their corresponding side centers.

Step 3: Position the Corners 🧩

Now, look at the yellow corners. The goal in this step is to get all four corners into their correct positions, even if they are not rotated facing the correct way (for example, a corner is in the correct spot if its three colors match the three surrounding side colors).

Standard Formula: \(U \ R \ U' \ L' \ U \ R' \ U' \ L\)

  • What it means: Top left, Right up, Top right, Left up, Top left, Right down, Top right, Left down

How to apply it:

  • If zero corners are in the correct place: Hold the cube in any rotation (with yellow on top) and perform the formula once. This will place at least one corner in the correct spot.
  • If one corner is in the correct place: Rotate the cube so that this correct corner is positioned in the bottom-right of the yellow top face (which is the Front-Right-Top / UFR slot). Perform the formula again. Check if all corners are in the correct place; if not, repeat the formula one more time with the correct corner still in the bottom-right.
Before: Scrambled Corners
Corners are in the wrong slots (e.g. Green face corner is Orange).
After: Corners in Correct Slots
Corners belong to these slots (colors match), but are twisted.

Step 4: Solve the Corners (The Core 4-Move Formula) ✨

In this final step, you will rotate the corner pieces to face the correct way, solving the entire cube. Make sure the yellow cross is still matched up and all corners are in their correct positions before starting.

The Formula (The “Four Moves”):

  1. Right side down ($R’$)
  2. Bottom to the left ($D’$)
  3. Right side up ($R$)
  4. Bottom to the right ($D$)

How to apply it:

  • Step A: Position one unsolved yellow corner in the front-right spot of the yellow top face (the UFR slot).
  • Step B: Perform the 4-move formula twice ($2\times$) and check if that corner is solved (with yellow facing up). If it is not solved, perform the 4-move formula another two times (making four times total).
  • Step C: Do not rotate the entire cube. Once the first corner is solved, rotate only the top layer ($U$ or $U’$) to bring the next unsolved yellow corner into that exact same front-right spot.
  • Step D: Repeat the 4-move formula (either 2 or 4 times) for this corner until it is solved.
  • Step E: Continue rotating the top layer to bring any remaining unsolved corners into the front-right spot and apply the formula until all corners are solved.

Finally, rotate the top layer to align the sides, and your Rubik’s Cube is fully solved!

Before: Twisted Corners
Corners are correct on sides, but yellow face points sideways.
After: Fully Solved!
All layers and sides are aligned and solved.