Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

ldez/cubejs

Repository files navigation

Build Statusnpm version

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

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

cube.js is written inCoffeeScript, and runs onnode.js and modern browsers.

A full-fledged random state scrambler demo is availablehere.

Examples

cube.js gives you basic cube manipulation:

  • Web:
// Create a new solved cube instanceconstcube=newCube();// Apply an algorithm or randomize the cube statecube.move("U F R2 B' D2 L'");cube.randomize();// Create a new random cubeconstrandomCube=Cube.random();
  • Node:
constCube=require('cubejs');// Create a new solved cube instanceconstcube=newCube();// Apply an algorithm or randomize the cube statecube.move("U F R2 B' D2 L'");cube.randomize();// Create a new random cubeconstrandomCube=Cube.random();

Fromsolve.js you can use the methods below, to solve the cube usingHerbert Kociemba's two-phase algorithm.

  • Web:
// This takes 4-5 seconds on a modern computerCube.initSolver();// These typically take from 0.01s to 0.4s, rarely up to 2scube.solve();// => "D2 B' R' B L' B ..."randomCube.solve();// => "R B' R U' D' R' ..."

To offload the solving to a web worker, useasync.js andworker.js:

Cube.asyncInit('lib/worker.js',function(){// InitializedCube.asyncSolve(randomCube,function(algorithm){console.log(algorithm);});});
  • Node:
constCube=require('cubejs');// This takes 4-5 seconds on a modern computerCube.initSolver();// These typically take from 0.01s to 0.4s, rarely up to 2scube.solve();// => "D2 B' R' B L' B ..."randomCube.solve();// => "R B' R U' D' R' ..."

API Reference

All functionality is implemented in theCube object.There are a bunch of files, of whichcube.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.
constcube=newCube();constother=newCube(cube);constthird=newCube(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.

constcube=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.

constrandom=Cube.random();constcube=newCube();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. SeeCube.fromString().

clone()

Returns a fresh clone of the cube.

randomize()

Randomizes the cube in place.

isSolved()

Returnstrue if the cube is solved (i.e. the identity cube), andfalse otherwise.

move(algorithm)

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

constcube=newCube();cube.isSolved();// => truecube.move("U R F'");cube.isSolved();// => false

See below for numeric moves.

Numeric moves

Internally, cube.js treats moves as numbers.

MoveNumber
U0
U21
U'2
R3
R24
R'5
F6
F27
F'8
D9
D210
D'11
L12
L213
L'14
B15
B216
B'17

solve.js

solve.js implementsHerbert Kociemba's two-phase algorithmfor 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 callingsolve().

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 isthe maximum number of moves in the solution, and defaults to 22.

License

cube.js is licensed under theMIT License.

History

cube.js was created byPetri Lehtinen akaakheron.Thanks to him.

About

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp