npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-cube-model

v0.4.0

Published

node-cube-model is used for modeling a Rubiks Cube.

Downloads

4

Readme

node-cube-model

node-cube-model is used for modeling a Rubiks Cube.

Usage

Constants

node-cube-model provides constants to use for interacting with the cube model.

FACES has TOP, LEFT, FRONT, RIGHT, BACK, and BOTTOM properties used to identify faces of the cube.

FACEROTATIONS has CW and CCW properties used to indicate if you want to rotate a face clockwise or counter clockwise.

CUBEROTATIONS has FORWARD, BACK, LEFT, RIGHT, CW, and CCW properties used to indicate directions to rotate the entire cube.

const   constants = require('node-cube-model').constants;
        FACES = constants.FACES,
        FACEROTATIONS = constants.FACEROTATIONS,
        CUBEROTATIONS = constants.CUBEROTATIONS;
        

Creation

To create an instance of a cube it must be provided an array indicating the colors on each face. The following diagram can be used to understand how the array of faces is structured. Each face is an array, and the colors on that face are the values of the array.

          top(0)
        ---------
        | 0 1 2 |
        | 3 4 5 |
 left(1)| 6 7 8 | right(3) back(4)
---------------------------------
| 0 1 2 | 0 1 2 | 0 1 2 | 0 1 2 |
| 3 4 5 | 3 4 5 | 3 4 5 | 3 4 5 |
| 6 7 8 | 6 7 8 | 6 7 8 | 6 7 8 |
---------------------------------
        | 0 1 2 |
        | 3 4 5 |
        | 6 7 8 |
        ---------
        bottom(5)

 *front(2)
var CubeModel = require('node-cube-model');

// Currently, numbers must be used to specify the color of a sticker.
var faces = [
              [0,0,0,0,0,0,0,0,0],
              [1,1,1,1,1,1,1,1,1],
              [2,2,2,2,2,2,2,2,2],
              [3,3,3,3,3,3,3,3,3],
              [4,4,4,4,4,4,4,4,4],
              [5,5,5,5,5,5,5,5,5]
            ];
            
  var cubeModel = cubeModel.create(faces);

Manipulation

The cube can be manipulated by rotating a face or by rotating the entire cube.

// This will rotate the cube towards you so that the TOP face becomes the FRONT
cubeModel.rotateCube(CUBEROTATIONS.FORWARD);

// This will rotate the cube left so that the TOP face becomes the LEFT
cubeModel.rotateCube(CUBEROTATIONS.LEFT);

// This will rotate the cube clockwise so that the FRONT face becomes the LEFT
cubeModel.rotateCube(CUBEROTATIONS.CW);

// This will rotate the TOP face counter clockwise
cubeModel.rotateFace(FACES.TOP, FACEROTATIONS.CW);

Observing

The cube can be observed by getting an array representing the cube that is structured the same was as the array provided in the constructor.

var facesArray = cubeModel.getFacesArray();

The color of a specific face can also be retrieved from the cube model.

var topFaceColor = cubeModel.getFaceColor(FACES.TOP);

Pieces

The cube can also be observed by getting a Piece object representing a corner or an edge piece. A piece object is requested by providing an array of FACES constants that represent where the piece should be.

// This will get the corner piece that is made up from the TOP, BACK, and LEFT faces.
var topBackLeftCorner = cubeModel.getPiece([FACES.TOP, FACES.BACK, FACES.LEFT]);

// This will get the edge piece that is made up from the LEFT and FRONT faces.
var leftFrontEdge = cubeModel.getPiece([FACES.LEFT, FACES.FRONT]);

Piece objects contain Sticker objects. You can call .stickers to get an array of all the stickers that make up a piece. You can call .getStickerOfColor() with a color to get the sticker that is a specific color. Or, you can call .getStickerOnFace() with a constant FACE value to get the sticker that is currently on the specified face.

// This will get an array of all stickers making up a Piece.
var stickers = pieceObject.stickers;

// This will get the sticker that is the 0 color.
var sticker = pieceObject.getStickerOfColor(0);

// This will get the sticker that is currently on the TOP face.
var sticker = pieceObject.getStickerOnFace(FACES.TOP);

Stickers

Stickers have three attributes. The .face attribute indicates what face the sticker is on. The .position attribute indicates the position of that sticker on the face. The .color attribute indicates the color of the sticker.

Retrieving Moves

At any time you can retrieve an array of all moves performed on the cube since it was created.

// This will return an array of all moves performed on the cube, including cube rotations.
var totalMoves = cubeModel.getTotalMoves();

// This will return an array of all moves performed on the cube with cube rotations factored out.
var getTotalMovesSimplified = cubeModel.getTotalMovesSimplified();