cubejs

repository·master·Indexed 18 days ago

https://github.com/ldez/cubejs

A JavaScript library for modeling and solving the 3x3x3 Rubik's Cube using Herbert Kociemba's two-phase algorithm. It provides functionality to manipulate cube states via move notation, generate random cubes, and solve cubes both synchronously in Node.js and asynchronously via Web Workers in web environments. The library supports state serialization to JSON and 54-character facelet strings.

Tokens
2.3K
Snippets
16
Records
16
Agent score
13%

What's inside cubejs

  1. Solve a Rubik's Cube using Kociemba's algorithm

    master

    To solve a cube, you must first run the computationally intensive precalculation step using Cube.initSolver(). This typically takes 4-5 seconds. Once initialized, you can call .solve() on a cube instance to get a string representing the moves required to solve it. The algorithm aims for a solution in 22 moves or less.

    const Cube = require('cubejs');
    
    // Perform precalculation (required once before solving)
    Cube.initSolver();
    
    const cube = new Cube();
    cube.randomize();
    
    // Returns an algorithm string, e.g., "D2 B' R' B L' B ..."
    const solution = cube.solve();
    console.log(solution);
  2. Install and use cube.js in Node.js

    master

    To use cube.js in a Node.js environment, require the cubejs package. You can then instantiate a new Cube to manipulate its state or generate random states.

    const Cube = require('cubejs');
    
    // Create a new solved cube instance
    const cube = new Cube();
    
    // Apply an algorithm or randomize the cube state
    cube.move("U F R2 B' D2 L'");
    cube.randomize();
    
    // Create a new random cube
    const randomCube = Cube.random();
  3. Solve a Rubik's Cube asynchronously via Web Worker

    master

    In web environments, solving can block the main thread. To offload the solving process to a web worker, use Cube.asyncInit to load the worker script and Cube.asyncSolve to perform the solve asynchronously.

    // Offload solving to a web worker
    Cube.asyncInit('lib/worker.js', function() {
        // Initialized
        Cube.asyncSolve(randomCube, function(algorithm) {
            console.log(algorithm);
        });
    });
  4. Invert an algorithm

    master

    Use Cube.inverse(algorithm) to get the reverse of a given algorithm. The input can be a string, an array of moves, or a single numeric move.

    Cube.inverse("F B' R");    // => "R' B F'"
    Cube.inverse([1, 8, 12]);  // => [14, 6, 1]
    Cube.inverse(8);           // => 6
  5. Instantiate a Cube from a state string

    master

    Use Cube.fromString(str) to create a cube from a 54-character facelet string. Each character represents a color/facelet (e.g., 'U' for Up, 'R' for Right). The string follows a specific order: 9 characters per face in the order: Up (U), Left (L), Front (F), Right (R), Back (B), and Down (D).

    // Example facelet string (54 characters)
    const state = "UUUUUUUUUR...F...D...L...B...";
    const cube = Cube.fromString(state);
  6. Manipulate Cube state with moves and algorithms

    master

    You can change the state of a Cube instance using several methods:

    • move(algorithm): Applies an algorithm (string, array of moves, or a single move) to the cube.
    • randomize(): Randomizes the cube in place.
    • identity(): Resets the cube to the solved (identity) state.
    • init(state): Resets the cube state to match another cube instance.
    const cube = new Cube();
    
    // Apply a string algorithm
    cube.move("U R F'");
    
    // Check if solved
    if (cube.isSolved()) {
      console.log("Solved!");
    }
  7. Export Cube state as JSON or String

    master

    To save or transmit the cube state, use:

    • toJSON(): Returns the state as an object containing cp, co, ep, and eo arrays.
    • asString(): Returns the state as a 54-character facelet string.
    const cube = Cube.random();
    const jsonState = cube.toJSON();
    const stringState = cube.asString();
  8. Reference: Numeric Move Mappings

    master

    Internally, cube.js treats moves as numbers. This is useful for programmatic manipulation or when using Cube.inverse with numeric arrays.

    | Move | Number |
    |------|--------|
    | U    | 0      |
    | U2   | 1      |
    | U'   | 2      |
    | R    | 3      |
    | R2   | 4      |
    | R'   | 5      |
    | F    | 6      |
    | F2   | 7      |
    | F'   | 8      |
    | D    | 9      |
    | D2   | 10     |
    | D'   | 11     |
    | L    | 12     |
    | L2   | 13     |
    | L'   | 14     |
    | B    | 15     |
    | B2   | 16     |
    | B'   | 17     |
  9. Convert Cube to and from strings

    master

    You can serialize a cube state to a string representation or reconstruct a cube from a string.

    • asString(): Returns a string representing the colors of all facelets on the cube.
    • static fromString(str): Creates a new Cube instance from a color string.
    const cube = Cube.random();
    const cubeString = cube.asString();
    
    const reconstructedCube = Cube.fromString(cubeString);
  10. Serialize Cube to JSON

    master

    The toJSON() method returns a plain object representing the cube's internal state, which is useful for saving or transmitting the state.

    The object contains:

    • center: Array of center colors.
    • cp: Array of corner permutations.
    • co: Array of corner orientations.
    • ep: Array of edge permutations.
    • eo: Array of edge orientations.
    const state = cube.toJSON();
    // state = { center: [...], cp: [...], co: [...], ep: [...], eo: [...] }
  11. Perform moves on a Cube

    master

    The move(arg) method applies one or more moves to the cube. The argument can be a space-separated string of moves (e.g., 'R U R'), an array of move identifiers, or a single move identifier.

    Supported move notation:

    • Single letter: U, R, F, D, L, B (standard faces)
    • Double letter: E, M, S (slice moves)
    • Rotation: x, y, z (cube rotations)
    • Modifiers: 2 for 180-degree turns, ' for counter-clockwise turns.

    Example moves: 'R U R', 'U2', 'L''.

    const cube = new Cube();
    
    // Apply a sequence of moves
    cube.move("R U R' U'");
    
    // Apply a single move
    cube.move("U2");