Catalog npm
PACKAGE EXPLORERView in registry

15puzzle-utils

npm

Sliding puzzle utility library

15-puzzlenxm-puzzles
Versions collected2
Direct dependencies3From release 1.0.1
Last observed20 Oct, 18:15 UTC

README

Current package documentation

15puzzle-utils

Utility library for creating and manipulating sliding puzzles

Installation

npm install 15puzzle-utils

Usage

import { Puzzle } from '15puzzle-utils';

// Pick one of these constructors:
const puzzle = new Puzzle();     // 4x4, randomly shuffled
const puzzle = new Puzzle(3, 5); // custom size, randomly shuffled
const puzzle = new Puzzle([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 0]
]); 

// Methods
puzzle.isSolved()         // true when puzzle is complete
puzzle.isSolvable()       // true when puzzle can be solved
puzzle.canSlide(row, col) // check if tile can slide
puzzle.slide(row, col)    // slide tile toward empty space
puzzle.getGrid()          // current state as number[][]
puzzle.getWidth()         // number of columns
puzzle.getHeight()        // number of rows
puzzle.getValue(row, col) // value at a specific cell
puzzle.getEmptyPosition() // { row, col } of the empty tile
puzzle.toAsciiString()    // text representation

Error Handling

All errors extend PuzzleError, so you can catch them generically or handle specific cases:

import { Puzzle, PuzzleError, BlockedPathError } from '15puzzle-utils';

// Catch any puzzle error
try {
    puzzle.slide(row, col);
} catch (e) {
    if (e instanceof PuzzleError) { ... }
}

// Or handle specific cases
try {
    puzzle.slide(row, col);
} catch (e) {
    if (e instanceof BlockedPathError) { ... }
}
ErrorThrown by
InvalidDimensionErrorconstructor(width, height), constructor(grid) invalid width/height
InvalidGridContentErrorconstructor(grid) duplicate or out-of-range tile values
InvalidPositionErrorslide(row, col), getValue(row, col) row/col out of bounds
EmptyTileSlideErrorslide(row, col) target cell is the empty tile
BlockedPathErrorslide(row, col) tile not in same row or column as empty space

License

MIT

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 2

Collected releases
VersionPublishedDirect dependenciesOpen version
1.0.1Registry latest6 Apr 20263
1.0.031 Mar 20263

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
ebrancati/15puzzle-utils

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 18:15 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:ce331e9ef4fb61684ac8cd7fabd6dcad80ec336239b733e255c8e1c2b33081a9