A full-featured Python command-line Rubik's Cube 3×3 simulator written in pure Python.
- Full 3×3 cube state representation
- All basic face moves:
U,D,F,B,L,R(including'and2) - Whole-cube rotations:
Y,Y',Y2 - Random scramble generator
- Check if the cube is solved
- Save cube state to JSON (
save state.json) - Load cube state from JSON (
load state.json) - Reset to solved state (
clear) - Export cube as PNG image (
export cube.png)
- Apply move sequences directly:
R U R' U2 - Undo last move (
undo) - View move history (
history) - Built-in help (
help)
define name <moves...>— save a named algorithmrun name— execute a saved algorithmalgos— list all saved algorithmsundef name— delete a saved algorithminvert <moves...>— print the inverse of a raw algorithminvert <name>— print the inverse of a saved algorithm
Algorithms are stored persistently in algos.json.
- Basic tests using
pytest - Modern
pyproject.tomlusing setuptools - Installable locally with
pip install . - Provides a CLI command:
rubikscube
From the project root, start the interactive mode:
python3 main.pyIf installed via pip install .:
rubikscubeScramble the cube with 20 moves and print the state:
python3 main.py --scramble 20 --no-interactiveLoad state, apply an algorithm, save the result:
python3 main.py \
--load state.json \
--apply "R U R' U'" \
--save new_state.json \
--no-interactiveWhen running:
python3 main.pyYou can use the following commands:
R U R' U2– apply any move sequencescramble– scramble with default length (20 moves)scramble 30– scramble with 30 random moves
undo– undo the last movehistory– show all moves performed in this sessionclear– reset cube to solved stateexport cube.png– export the current cube as a PNG image
save state.json– save cube state to a JSON fileload state.json– load cube state from a JSON file
define algo R U R' U'– save algorithm under the namealgorun algo– execute the algorithmalgoon the cubealgos– list all saved algorithmsundef algo– delete the algorithmalgoinvert "R U R' U'"– print the inverse of a raw algorithminvert algo– print the inverse of the saved algorithmalgo
help– show a summary of available commandsquit,exit,q– exit the program
Notes:
- Moves must be space-separated (e.g.
R U R' U2). - Colors depend on ANSI support of your terminal.
Run tests with:
pytestCreate and activate a virtual environment (recommended):
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install --upgrade pipInstall the project:
pip install .After installation, you can run:
rubikscubeto start the CLI.
{
"U": ["W","W","W","W","W","W","W","W","W"],
"D": ["Y","Y","Y","Y","Y","Y","Y","Y","Y"],
"F": ["G","G","G","G","G","G","G","G","G"],
"B": ["B","B","B","B","B","B","B","B","B"],
"L": ["O","O","O","O","O","O","O","O","O"],
"R": ["R","R","R","R","R","R","R","R","R"]
}MIT
