• Stars
    star
    314
  • Rank 133,353 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

cube.js -- JavaScript library for modeling and solving the 3x3x3 Rubik's Cube

cube.js

Build Status npm version

cube.js is a JavaScript library for modeling and solving the 3x3x3 Rubik's Cube.

Most notably, it implements Herbert Kociemba's two-phase algorithm for solving any state of the cube very fast in 22 moves or less.

cube.js is written in CoffeeScript, and runs on node.js and modern browsers.

A full-fledged random state scrambler demo is available here.

Examples

cube.js gives you basic cube manipulation:

  • Web:
// 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();
  • Node:
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();

From solve.js you can use the methods below, to solve the cube using Herbert Kociemba's two-phase algorithm.

  • Web:
// This takes 4-5 seconds on a modern computer
Cube.initSolver();

// These typically take from 0.01s to 0.4s, rarely up to 2s
cube.solve();        // => "D2 B' R' B L' B ..."
randomCube.solve();  // => "R B' R U' D' R' ..."

To offload the solving to a web worker, use async.js and worker.js:

Cube.asyncInit('lib/worker.js', function() {
    // Initialized
    Cube.asyncSolve(randomCube, function(algorithm) {
        console.log(algorithm);
    });
});
  • Node:
const Cube = require('cubejs');

// This takes 4-5 seconds on a modern computer
Cube.initSolver();

// These typically take from 0.01s to 0.4s, rarely up to 2s
cube.solve();        // => "D2 B' R' B L' B ..."
randomCube.solve();  // => "R B' R U' D' R' ..."

API Reference

All functionality is implemented in the Cube object. There are a bunch of files, of which cube.js is always required.

cube.js

cube.js gives you basic cube manipulation.

Class methods

Cube([state])

The constructor:

  • without arguments, constructs an identity cube (i.e. a solved cube).
  • with an argument, clones another cube or cube state.
const cube = new Cube();
const other = new Cube(cube);
const third = new Cube(cube.toJSON());
Cube.fromString(str)

Returns a cube that represents the given facelet string. The string consists of 54 characters, 9 per face:

"UUUUUUUUUR...F...D...L...B..."

U means a facelet of the up face color, R means a facelet of the right face color, etc.

The following diagram demonstrates the order of the facelets:

             +------------+
             | U1  U2  U3 |
             |            |
             | U4  U5  U6 |
             |            |
             | U7  U8  U9 |
+------------+------------+------------+------------+
| L1  L2  L3 | F1  F2  F3 | R1  R2  R3 | B1  B2  B3 |
|            |            |            |            |
| L4  L5  L6 | F4  F5  F6 | R4  R5  R6 | B4  B5  B6 |
|            |            |            |            |
| L7  L8  L9 | F7  F8  F9 | R7  R8  R9 | B7  B8  B9 |
+------------+------------+------------+------------+
             | D1  D2  D3 |
             |            |
             | D4  D5  D6 |
             |            |
             | D7  D8  D9 |
             +------------+
Cube.random()

Return a new, randomized cube.

const cube = Cube.random();
Cube.inverse(algoritm)

Given an algorithm (a string, array of moves, or a single move), returns its inverse.

Cube.inverse("F B' R");    // => "R' B F'"
Cube.inverse([1, 8, 12]);  // => [14, 6, 1]
Cube.inverse(8);           // => 6

See below for numeric moves.

Instance methods

init(state)

Resets the cube state to match another cube.

const random = Cube.random();
const cube = new Cube();
cube.init(random);
identity()

Resets the cube to the identity cube.

cube.identity();
cube.isSolved();  // => true
toJSON()

Returns the cube state as an object.

cube.toJSON();  // => {cp: [...], co: [...], ep: [...], eo: [...]}
asString()

Returns the cube's state as a facelet string. See Cube.fromString().

clone()

Returns a fresh clone of the cube.

randomize()

Randomizes the cube in place.

isSolved()

Returns true if the cube is solved (i.e. the identity cube), and false otherwise.

move(algorithm)

Applies an algorithm (a string, array of moves, or a single move) to the cube.

const cube = new Cube();
cube.isSolved();  // => true
cube.move("U R F'");
cube.isSolved();  // => false

See below for numeric moves.

Numeric moves

Internally, cube.js treats moves as numbers.

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

solve.js

solve.js implements Herbert Kociemba's two-phase algorithm for solving the cube quickly in nearly optimal number of moves. The algorithm is a port of his simple Java implementation without symmetry reductions.

For the algorithm to work, a computationally intensive precalculation step is required. Precalculation results in a set of lookup tables that guide the heuristic tree search. Precalculation takes 4-5 seconds on a typical modern computer, but migh take longer on older machines.

After precalculation is done, solving a cube with at most 22 moves typically takes 0.01-0.4 seconds, but may take up to 1.5-2 seconds. Again, these figures apply for a modern computer, and might be bigger on older machines.

The maximum search depth (numer of moves) can be configured. The deeper search is allowed, the quicker the solving is. There's usually no reason to change the default of 22 moves.

Class methods

Cube.initSolver()

Perform the precalculation step described above. This must be called before calling solve().

Cube.scramble()

Generate a random scramble by taking a random cube state, solving it, and returning the inverse of the solving algorithm. By applying this algorithm to a cube, you end up to the random state.

Instance methods

solve([maxDepth])

Return an algorithm that solves the cube as a string. maxDepth is the maximum number of moves in the solution, and defaults to 22.

License

cube.js is licensed under the MIT License.

History

cube.js was created by Petri Lehtinen aka akheron. Thanks to him.

More Repositories

1

traefik-certs-dumper

Dump ACME data from Traefik to certificates
Go
352
star
2

prm

Pull Request Manager for Maintainers
Go
190
star
3

go-git-cmd-wrapper

A simple wrapper around git command in Go.
Go
45
star
4

gha-mjolnir

🔨 GitHub Action to close issues related to the merge of a pull request.
Go
41
star
5

tagliatelle

A linter that handles struct tags.
Go
25
star
6

seihon

A simple tool to build and publish multi-arch images on the Docker Hub.
Go
20
star
7

kata-starter

Starter for Kata in several languages (Java, Go, Groovy, JS, Python, Scala, PHP, ...)
JavaScript
17
star
8

stackoverflow-slack-bot

Track a tag on StackOverflow and push to Slack
JavaScript
16
star
9

ghactions

Create a Github Action in 5 seconds!
Go
16
star
10

traefik-certs-cleaner

A simple helper to clean the Traefik `acme.json` file by removing and revoking certificates.
Go
15
star
11

gcg

GCG is a GitHub Changelog Generator.
Go
13
star
12

kebab-kata

Kebab Kata created by @malk
13
star
13

motoko

Motoko: Based on Go modules, update a dependency to a major version.
Go
7
star
14

gomoddirectives

A linter that handle `replace`, `retract`, `exclude` directives into `go.mod`.
Go
6
star
15

vscode-language-ignore

Syntax highlighting for 'ignore' files : gitignore, npmignore, dockerignore, prettierignore, ...
6
star
16

gti

A port of gti in Go. (because it's fun)
Go
5
star
17

mimetype

This package provides mime-types as constants.
Go
5
star
18

jar2pom

Convert Jar to POM
Java
5
star
19

atom-language-ignore

Syntax highlighting for 'ignore' files : gitignore, npmignore, dockerignore, coffeelintignore, ...
CoffeeScript
4
star
20

deptomod

dep to go modules with sugar
Go
3
star
21

go-capillarity

A simple object filler
Go
3
star
22

semgo

Keeps Go version in SemaphoreCI up to date.
Go
2
star
23

ghwebhook

GHWebHook: create a GitHub Web Hook in 5 seconds with Go.
Go
2
star
24

go-semaphoreci

Go library for accessing the Semaphore CI API
Go
2
star
25

rebaese

Rebaese is a tool made for rebasing pull requests on GitHub
Go
2
star
26

atom-grammar-live-reload

Language grammars Live Reload for Atom (Async)
CoffeeScript
2
star
27

jaelon

Jaelon is a GitHub Milestone checker and filler.
Go
2
star
28

travis-continuous-delivery-hugo-publish

Travis continuous delivery - Hugo - Only for testing purpose
Shell
2
star
29

ghban

To block multiple accounts across multiple organizations.
Go
2
star
30

homebrew-tap

Homebrew Formulae to binaries
Ruby
1
star
31

grammar-to-plist-syntax

Convert Atom grammar to VSCode syntax
JavaScript
1
star
32

generator-atom-package-plus

A Yeoman generator to build and develop Atom packages
JavaScript
1
star
33

samples-go-libs-string-cases

Comparison of Word Cases Manipulations Libraries
Go
1
star
34

back-web

CSS
1
star
35

vscode-language-asciidoc

AsciiDoc(tor) language package for the VSCode editor.
1
star
36

grignotin

A collection of small helpers around Go proxy, Go meta information, etc.
Go
1
star