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

a-star-puzzle-solver

v2.1.0

Published

A* search algorithm in TypeScript that solves puzzle game problem

Downloads

10

Readme

A Star Puzzle Solver

Build Status

View an example of use here

A TypeScript lib that solves Puzzle Game problem using the A* Algorithm

Installation

Use the package manager npm to install A* Puzzle Solver.

npm install a-star-puzzle-solver

How to use

ES6

import AStarPuzzleSolver from 'a-star-puzzle-solver';

const solution = AStarPuzzleSolver.solvePuzzle(state);

CommonJS

const AStar = require('a-star-puzzle-solver').default;

AStar.solvePuzzle(state);

UMD in Browser

<!-- to import non-minified version -->
<script src="a-star-puzzle-solver.umd.js"></script>

<!-- to import minified version -->
<script src="a-star-puzzle-solver.umd.min.js"></script>

After that the library will be available to the Global as AStarPuzzleSolver. Follow an example:


const aStar = AStarPuzzleSolver;

const solution = aStar.solvePuzzle(state);

Methods

Follow the methods this library provides:

solvePuzzle(initialState)

Solves the 8 Puzzle Game with provided initial state.

Arguments

| Argument | Type | Options | |----------|---------|-------------------| |initialState|Array of Numbers | Any state possible|

Example

const state = [1, 2, 3, 4, 5, 6, 7, 0, 8];

const solution = AStar.solvePuzzle(state);
/*
Obs:
- Pay atention to the order of elements 
- Internally, this state is interpreted like this:
    [
    [1, 2, 3],
    [4, 5, 6],
    [7, 0, 8]
    ]
*/

Return

This method return an object containing:

  • pathCost: Total cost of the algorithm (also known as depth of final state)
  • expandedNodes: quantity of nodes visited by the algorithm from the initial state to the final state
  • iterations: quantity of interation loops inside the algorithm until arrive final state
  • solution: an array of objects (each object has the following properties):
    • state: an array with same format the one provided initially
    • operation: a String that represents the operation applied to achieve the state.
      • possible values:
        • 'UP_OPERATION'
        • 'RIGHT_OPERATION'
        • 'DOWN_OPERATION'
        • 'LEFT_OPERATION'
        • 'NONE'
  • finalNode: an object wich you can use to access all solution
    • properties:
      • operation: a String that represents the operation applied to achieve the state (values are same indicated above).
      • evaluationFunctionValue: an object that represents value of
        • properties:
          • g: depth of node
          • h: heuristic value
          • f: sum of 'g' and 'h'
      • state: state of node, but in a different format of states provided by solution's array (see convertStateInArray method below)
      • previousNode: another object with same properties (represents parent node of this one)

Obs:

  • For more examples of how to use, see './examples' folder
  • The goalState considered by the algorithm is:

convertStateInArray(state)

Receives a multidimensional array (3x3) and return an unidimensional (with 9 positions).

Arguments

| Argument | Type | Options | |----------|---------|-------------------| |state|Multidimensional array of Numbers | Any state possible|

Example

const state = [[1, 2, 3], [4, 5, 6], [7, 0, 8]];

const arr = AStar.convertStateInArray(state);

Return

This method return an unidimensional array similar to this:

//Considering argument of example above
[1, 2, 3, 4, 5, 6, 7, 0, 8]

convertArrayInState(arr)

Does the exact opposite of convertStateInArray. Receives an unidimensional array of 9 positions and return multidimensional one (3x3).

Arguments

| Argument | Type | Options | |----------|---------|-------------------| |arr|Unidimensional array of Numbers | Any state possible|

Example

const arr = [1, 2, 3, 4, 5, 6, 7, 0, 8];

const state = AStar.convertArrayInState(arr);

Return

This method return a multidimensional array similar to this:

//Considering argument of example above
[[1, 2, 3], [4, 5, 6], [7, 0, 8]]

isSolvable(state)

Receives a state and returns whether it is solvable or not.

Arguments

| Argument | Type | Options | |----------|---------|-------------------| |state|Multidimensional array of Numbers | Any state possible|

Example

const state = [[1, 2, 3], [4, 5, 6], [7, 0, 8]];

const isSolvable: boolean = AStar.isSolvable(state);

Return

This method return a boolean value indicating whether state is solvable or not:

true | false

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE.md file for details