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

klotski-ts

v1.1.0

Published

Welcome to `klotski-ts`, a typescript package to programatically generate, and solve, Klotski puzzles. This package doesn't provide a visual interface, but facilitates the capability to plug your own in.

Downloads

9

Readme

Klotski-ts (Typescript)

Welcome to klotski-ts, a typescript package to programatically generate, and solve, Klotski puzzles. This package doesn't provide a visual interface, but facilitates the capability to plug your own in.

The package exports two classes, Board and Piece, and also two additional exports, which contain instantiated board and pieces classes. These can be used as they have been in the example/demo.ts file, to get your Klotski board up and running with little friction.

Contents

Api

The reference for the Board and Piece objects, and other peripheral types

Board

new Board(boardSize: [number, number], completionCoords: number[][]): Board

addPiece(piece: Piece, startingPosition: Position, isEscapee?: boolean): Result<Piece>

movePiece(pieceId: string, direction: Direction, moves?: NumberOfMoves): Result<Position>

getBoardState(): BoardState

getCompletedState(): boolean

Piece

new Piece(id: string, shape: Shape): Piece

id: string

shape: Shape

Additional types

// A result type heavily influenced by Go's idiomatic error handling.
type Result<T> = {
  result: T | null;
  error: string | null;
}

type BoardState = {
  [key: string]: string | null;
}

type Shape = "1x1" | "1x2" | "2x1" | "2x2";

type Position = [number, number];

type NumberOfMoves = 1 | 2;

Using the initialised board

Install the package using yarn add klotski-ts

In your index.ts file, add the following:

import { initialisedBoard as board, piece } from 'klotski-ts

The pieces object contains a Piece object for each of the 10 pieces that are initialised in the board object. To gain file-level access of these objects, you can destructure the object like so:

const {
  smallSquare1,
  smallSquare2,
  smallSquare3,
  smallSquare4,
  pipe1,
  pipe2,
  pipe3,
  pipe4,
  line1,
  goldenCubeOfGlory
} = pieces;

Each piece has its own place on board, with the following diagram showing each piece on it's corresponding place on the board.

Klotski board

You can now start moving pieces on the board using the board.movePiece() method. The movePiece() method performs validation to ensure that a legal move is made:

A valid move is one where the piece: a) remains within the confides of the board. b) doesn't collide with existing pieces.

A piece can be moved in a single direction by either 1 or 2 spaces.

After moving pieces, you can display the current state of the board using the getBoardState() method. You can also check the completed state of the game using the getCompletedState() method.

The game completes when the escapee block, the goldenCubeOfGlory in this instance, reaches the designated exit point.

Creating your own board

You can create your own Klotski board to play with. Do note that this package doesn't ensure that your boards are solvable, or provide any validation aside from ensuring pieces are added to empty spaces within the board.

Start by creating a piece, like so:

const smallSquare1 = new Piece("ss1", "1x1");

The first argument denotes the key of the shape, which is used as a reference by the board object. The second argument is the shape of the piece, which is of type Shape.

Once you've added all of the required pieces for the game, you can create your board.

The traditional Klotski board is initalised through the following.

const board = new Board([6, 7], [[2, 1], [3, 1]]);

The board has a size of 6 horiztonal units, and 7 vertical units. The second argument is an array of all the coordinates that act as the exit point for the escapee.

After the board has been created, it's time to add all the pieces to their starting positions:

board.addPiece(smallSquare1, [1, 1])

Adds out small square to the bottom-left place on the board. Perform the same with each of your pieces. The addPiece method also offers an optional third argument, isEscapee: boolean.

Once the board has finished being initialised, you can start poerforming moving your pieces around. This is done in the exact same way as described in the Using the initialised board section.

Running the test suite

If you want to run the test suite, you can do so by running the following:

Backlog

  • [ ] Better visualisation of board state
  • [x] Reset the game
  • [x] Expose turns