Interactive Rubik’s Cube Solver & ML Explainer 🧩
Published:
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:
- 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.
- 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):
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.
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'\)
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'\)
5. Position the Yellow Corners (Permutation) 🔄
Swap the corner positions so they sit in their correct corner slots (even if the colors are twisted). Keep repeating this sequence: \(\text{Algorithm: } U \ R \ U' \ L' \ U \ R' \ U' \ L\)
6. Orient the Yellow Corners (Final Solve) ✨
Turn the cube upside down (white center faces up). Look at the bottom right corner (the yellow side). Repeat the Sexy Move ($R \ U \ R’ \ U’$) until the yellow sticker faces down. Rotate the bottom layer to bring the next unsolved corner to the bottom right and repeat. Do not rotate the whole cube, only the bottom layer!