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

knightp

v3.0.0

Published

A CLI that finds the shortest path a knight can take between two positions on a chessboard

Downloads

1

Readme

Knight's Path

CircleCI

A command line program that finds the shortest path a knight can take between two positions on a chessboard.



Quickstart

You need node v8 or higher and npm v5.2 or higher to run this program.

npx knightp

The program accepts instructions in algebraic chess notation, comprising of two space-separated positions. For each instruction, it will print the shortest path a knight can take, e.g.:

🐴 D4 G7
🐴 D4 F5 G7

Running locally

Clone this repository and run:

  1. npm i to install dependencies
  2. npm t to run the Jest test suite
  3. npm run build to compile the TypeScript source code
  4. npm start to compile and run the program

Algorithm

In chess, knights move in an L-shape: 2 squares along one dimension, 1 square along the other.

There are several ways to approach the problem of finding a knight's shortest path between two positions. For example:

We could generate all extant moves, one step at a time, disregarding already-visited squares, until we reach the destination.

It takes at least 2 steps to move from D4 to G7, via D4 E6 G7 or D4 F5 G7. The complexity of this approach is illustrated below, by the sequence ♞⟶⚫⟶🔴:

Illustration of knight moves on chessboard

Knights move in predictable ways. On a two-dimensional chessboard, its moves are symmetric along all axes. A knight can reach any square* on a chessboard. We can find a formula for the number of moves it takes to reach a square (x,y).

Knight movement patterns on an infinite chessboard

  1. Calculate the "distance" (Δ) between current position (s) and target (t)
  2. Consider Δ(s,t)
    • If Δ=0, the target is reached
    • If Δ=1, move straight to target
    • If Δ>1, generate a maximum of 8 possible moves (m) from the starting position, until one satisfies the constraint Δ(m,t) = Δ(s,t)-1
  3. Move to (m). Repeat all steps until we have reached (t).

The program implements a calculus-based approach, which allows it to be performant for extensions such as infinite chessboards.

Assumptions

  1. The knight moves on an 8x8 chessboard
  2. There are no blocking pieces on the board
  3. The optimal path between two positions is determined by the fewest moves; equivalent routes are acceptable (e.g., to get from D4 to G7, D4 F5 G7 and D4 E6 G7 are equivalent solutions)

Alternative chessboard sizes

This program considers a 8x8 chessboard by default, and supports chessboards from 5x5 to 26x26 (using algebraic notation to name the squares, from A1 through to Z26).

To change the chessboard size, set the --boardSize flag followed by an integer value or explicitly set the environment variable BOARD_SIZE:

# execute the published package
npx knightp --boardSize 25

# run from local source code
export BOARD_SIZE=16 && npm start