Rubik's Cube Solver
ArchivedThree languages, a wooden rig and six stepper motors
May 2017
- Turing
- Java
- C++
- Arduino



- Input
- Turing GUI
- Solver
- Java
- Hardware
- C++ on Arduino
- Solve + turn
- ~1 minute
What it is
A machine that solves a physical Rubik's cube. You enter the scrambled state by clicking squares in a GUI; a solver works out the move sequence; the moves go to an Arduino that drives stepper motors gripping each axis of the cube. Working out the solution and turning the cube take about a minute between them.
The distinction matters. A cube solver that prints a move list is an afternoon's work once you know the method. Making the moves happen in the physical world is a different project, and it is where all the interesting failure modes live.
Three languages, one pipeline
Turing draws the input program and captures the scramble. Java holds the solver. C++ runs on the Arduino and drives the motors. Each language sits on the side of a boundary where it belongs — Turing because this was a TEJ shop-class project and that was the tool, Java because the solving logic is ordinary object-oriented code that is pleasant to debug, C++ because that is what talks to stepper motors.
The handoffs are deliberately dumb. Turing writes the cube state out as six faces of three rows of colour letters; the Java side reads exactly that and turns each letter into a face index. A boundary that simple is one you can inspect by opening a text file, which matters a lot when the thing downstream of you has moving parts.
Entering the cube
The input program draws the cube as two isometric views — one showing three faces, the other the opposite three — so all six are reachable without rotating anything. Clicking a square cycles its colour, and a Send State button hands the result off.
It also refuses invalid input, which is less obvious than it sounds and more necessary. Most colour arrangements you can click into a net are not reachable on a real cube: wrong colour counts, impossible corner twists, swapped edge pairs. Without that check the solver would happily chase a state that cannot exist, and you would only find out when the motors started turning.
The solve, expressed as an inheritance chain
The solver follows the beginner's method — cross, then corners, then the second-layer edges, then the top edges, then permuting the corners, then orienting them. What is unusual is how that is structured in the code: each stage is a subclass of the one before it. RubixBase holds the cube and its moves, RubixCross extends it to solve the cross, Rubix2nd extends that for the middle layer, and so on up the chain.
It is not how I would organise it now. Inheritance is a strange way to model a pipeline, and I was still figuring out what structure was for — that is what 2017 code looks like, and I would not write it this way today. It does have a logic to it as a way to learn the method, though, since each class can only do what every class below it already could. The program prints its answer as six lines, one per stage, and the moves assume red at the front and white on top.
The method, and what it costs
The beginner's method produces far more moves than a two-phase solver would, and it does not matter much here. When a motor is doing the turning, a longer sequence costs seconds. What you gain is a solve you can verify by eye at every stage, which is worth a great deal when the thing you are debugging has moving parts.