cube

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 3 Imported by: 0

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

View Source
const (
	URF = iota
	UFL
	ULB
	UBR
	DFR
	DLF
	DBL
	DRB
)

Corner slots, in the standard Kociemba order.

View Source
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

func FormatMoves(ms []Move) string

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.

const (
	ColU Color = iota
	ColR
	ColF
	ColD
	ColL
	ColB
)

The colours, one per cube face on a solved cube.

func (Color) String

func (c Color) String() string

String returns the face letter (U, R, F, D, L or B) for the colour.

type Cube

type Cube struct {
	CornerPos [8]uint8
	CornerOri [8]uint8
	EdgePos   [12]uint8
	EdgeOri   [12]uint8
}

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

func FromFacelets(f Facelets) (Cube, error)

FromFacelets builds a cube from a 54-sticker colour view, returning an error if the stickers do not describe a legal cube.

func ScrambledCube

func ScrambledCube(n int, seed int64) Cube

ScrambledCube returns a solved cube with n random moves applied.

func Solved

func Solved() Cube

Solved returns a solved cube.

func (Cube) Applied

func (c Cube) Applied(ms ...Move) Cube

Applied returns a copy of c with the moves applied, leaving c unchanged.

func (*Cube) Apply

func (c *Cube) Apply(m Move)

Apply applies a single move to the cube.

func (*Cube) ApplySeq

func (c *Cube) ApplySeq(ms []Move)

ApplySeq applies a sequence of moves in order.

func (Cube) Clone

func (c Cube) Clone() Cube

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) IsSolved

func (c Cube) IsSolved() bool

IsSolved reports whether the cube is solved.

func (Cube) ToFacelets

func (c Cube) ToFacelets() Facelets

ToFacelets renders the cube as its 54-sticker colour view.

func (Cube) Validate

func (c Cube) Validate() error

Validate reports whether the cube is a legal, solvable state. It checks that the positions are genuine permutations, that corner/edge orientation sums are zero (mod 3 / mod 2), and that corner and edge permutation parities agree.

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

func ParseFacelets(s string) (Facelets, error)

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.

func (Facelets) String

func (f Facelets) String() string

String renders the facelets as a 54-character URFDLB string.

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).

const (
	U Move = iota
	U2
	Up // U'
	R
	R2
	Rp
	F
	F2
	Fp
	D
	D2
	Dp
	L
	L2
	Lp
	B
	B2
	Bp
	NumMoves
)

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

func InverseSeq(ms []Move) []Move

InverseSeq returns the moves that undo ms (reversed and each inverted).

func ParseMove

func ParseMove(tok string) (Move, error)

ParseMove parses a single token such as "R", "U'", "F2".

func ParseMoves

func ParseMoves(s string) ([]Move, error)

ParseMoves parses a whitespace-separated move sequence such as "R U R' U2".

func Scramble

func Scramble(n int, seed int64) []Move

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

func Simplify(ms []Move) []Move

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.

func (Move) Face

func (m Move) Face() int

Face returns the face index (0=U,1=R,2=F,3=D,4=L,5=B) of the move.

func (Move) Inverse

func (m Move) Inverse() Move

Inverse returns the move that undoes m.

func (Move) String

func (m Move) String() string

String returns the Singmaster notation for the move (e.g. "R'", "U2").

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL