cube

This class models the state of Rubik's cubes. To get started, install the library through Yarn or NPM.
yarn add bedard-cube
npm install bedard-cube
API
To instantiate a cube, use the Cube
constructor and define the size
of the cube. The size
parameter must be an integer greater than 1
.
const cube = new Cube(3);
To perform one or more turns to the cube, use the turn
method. To manually convert a turn string to an object, use the parseTurn
method.
cube.turn('F R- L');
The cube can be scrambled via the scramble
function. This function optionally accepts a number of turns to perform. If omitted, the scramble depth will be determined by the size of the cube. Additionally, a scramble may be generated using the generateScramble
function. The only difference between these two functions is that generateScramble
does not perform the turns.
cube.scramble();
To test if the cube is solved, use the isSolved
method. This function returns true
or false
.
cube.isSolved();
The cube can be returned to it's original state via the reset
method.
cube.reset();
To itterate over all of the cube's stickers, use the stickers
method.
cube.stickers(function(sticker) {
});
State
To read the current state of the cube, access the state
property. This property holds an object with properties for each face, each containing an array of sticker values. The face arrays start from the top left sticker and read sequentially to the bottom right. So for example, a newly instantiated 3x3 cube would have the following state.
{
u: [
0, 0, 0,
0, 0, 0,
0, 0, 0,
],
l: [
1, 1, 1,
1, 1, 1,
1, 1, 1,
},
f: [
2, 2, 2,
2, 2, 2,
2, 2, 2,
],
r: [
3, 3, 3,
3, 3, 3,
3, 3, 3,
],
b: [
4, 4, 4,
4, 4, 4,
4, 4, 4,
],
d: [
5, 5, 5,
5, 5, 5,
5, 5, 5,
],
}
To picture how these values would map to an actual cube, imagine unfolding a cube while looking at the F
face. Notice that the B
face has the same orientation as the L
, F
, and R
faces.
U
L F R B
D
To store additional data with the stickers, toggle the useObjects
option. Setting this flag will store the sticker values as { originalIndex, value }
objects. Note that the originalIndex
key does not change as the cube is turned.
new Cube(size, { useObjects: true });
Notation
Turn notation has 3 basic parts.
Depth, defines how many layers from the outer face to turn. By default, this value is 1
.
Face / Axis, defines which face or axis is being turned. For face turns, this value may be U
, L
, F
, R
, B
, or D
, and if lower cased the turn will include all layers from the depth to the face being turned. For axis turns, this value may be X
, Y
, or Z
.
Direction, defines which direction to turn the face. A value of -
will turn the face/axis 90 degrees counter-clockwise, and a value of 2
will turn the face/axis 180 degrees. If omitted, the face/axis will be turned 90 degrees clockwise. A single-quote may also be used to indicate a counter-clockwise turn.
'F'
'F-'
'F2'
'2R'
'2R-'
'2R2'
'2d'
'2d-'
'2d2'
'X'
'X-'
'X2'
License
MIT
Copyright (c) 2018-present, Scott Bedard