Documentation
¶
Overview ¶
Package cube provides the core 3x3x3 Rubik's cube data model: a cubie-level representation (piece permutation + orientation), move application, Singmaster notation, facelet conversion for I/O, validation and scrambling.
The central type is Cube, a small comparable value type. Build one with Solved, FromFacelets or ScrambledCube; turn it with Cube.Apply and Cube.Applied over Move values (parse and print them with ParseMoves and FormatMoves); and read it back as Facelets via Cube.ToFacelets.
It is the shared state model behind the headless solver, the Ebiten visualizer and the LEGO EV3 robot, and is reusable on its own by any program that needs to model and turn a cube.
Example ¶
Example builds a solved cube, turns it by a sequence parsed from Singmaster notation, then undoes the sequence with cube.InverseSeq to solve it again.
package main
import (
"fmt"
"github.com/danielriddell21/rubix/pkg/cube"
)
func main() {
moves, err := cube.ParseMoves("R U R' U'")
if err != nil {
panic(err)
}
c := cube.Solved().Applied(moves...)
fmt.Println("after R U R' U':", c.IsSolved())
c = c.Applied(cube.InverseSeq(moves)...)
fmt.Println("after the inverse:", c.IsSolved())
}
Output: after R U R' U': false after the inverse: true
Index ¶
Examples ¶
Constants ¶
const ( URF = iota UFL ULB UBR DFR DLF DBL DRB )
Corner slots, in the standard Kociemba order.
const ( UR = iota UF UL UB DR DF DL DB FR FL BL BR )
Edge slots, in the standard Kociemba order.
Variables ¶
This section is empty.
Functions ¶
func FormatMoves ¶
FormatMoves renders a move sequence as space-separated Singmaster notation.
Types ¶
type Color ¶
type Color uint8
Color identifies a sticker colour by the face it belongs to on a solved cube: 0=U, 1=R, 2=F, 3=D, 4=L, 5=B.
The colours, one per cube face on a solved cube.
type Cube ¶
Cube is a cubie-level cube state.
- CornerPos[i] is the corner cubie currently in slot i (a permutation of 0..7).
- CornerOri[i] is that corner's twist: 0, 1 or 2.
- EdgePos[i] is the edge cubie currently in slot i (a permutation of 0..11).
- EdgeOri[i] is that edge's flip: 0 or 1.
Centres are fixed, so the cube frame is fixed and whole-cube rotations are not represented.
func FromFacelets ¶
FromFacelets builds a cube from a 54-sticker colour view, returning an error if the stickers do not describe a legal cube.
func ScrambledCube ¶
ScrambledCube returns a solved cube with n random moves applied.
func (Cube) Clone ¶
Clone returns a copy of the cube. Cube is a value type with no pointers, so a plain assignment copies it; Clone exists for call-site clarity.
func (Cube) ToFacelets ¶
ToFacelets renders the cube as its 54-sticker colour view.
type Facelets ¶
type Facelets [54]Color
Facelets is the 54-sticker view of a cube, ordered U0..8, R9..17, F18..26, D27..35, L36..44, B45..53 (each face row-major).
func ParseFacelets ¶
ParseFacelets parses a 54-character string in the URFDLB scheme. Each facelet may be given as a face letter (U, R, F, D, L, B) or the equivalent default colour (W, R, G, Y, O, B); both are accepted case-insensitively. Whitespace is ignored.
type Move ¶
type Move uint8
Move is one of the 18 outer-face quarter/half turns. Moves are grouped by face (face = m/3) and power (m%3: 0 = 90° CW, 1 = 180°, 2 = 90° CCW / prime).
The 18 face turns, three per face: 90° CW, 180°, and 90° CCW (prime). NumMoves is the count of real moves.
const NoMove Move = 0xff
NoMove is a sentinel that is not one of the 18 turns. It is handy for APIs that take an optional move (e.g. a renderer that animates a turn) to mean "no move".
func InverseSeq ¶
InverseSeq returns the moves that undo ms (reversed and each inverted).
func ParseMoves ¶
ParseMoves parses a whitespace-separated move sequence such as "R U R' U2".
func Scramble ¶
Scramble returns n random outer-face moves, avoiding consecutive turns of the same face (and of the opposite face right after) so the scramble is non-trivial. A fixed seed yields a reproducible scramble.
func Simplify ¶
Simplify rewrites a move sequence into a shorter one with the same overall effect on the cube. It cancels and merges turns of the same face — directly adjacent or separated only by turns of the opposite face, since moves on the same axis commute (U/D, R/L, F/B). For example "R R'" disappears, "U U" becomes "U2", and "U F F' U" becomes "U2".
It is transform-preserving: applying Simplify(ms) to any cube yields the same result as applying ms. It never returns a longer sequence than its input.