hanoi_solver
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

This package gives you access to methods allowing to create, play and solve a Tower of Hanoi game.

Installation

pnpm add hanoi_solver

Usage

import Hanoi, { pathToSolution } from "hanoi_solver";

// create new game
const h = new Hanoi(5, "BBCCA");

// what are the possible moves from this position ?
const possibles = h.possibleMoves();

console.log(`moves = ${JSON.stringify(possibles)}`);
// output : moves = [{"from":1,"to":2},{"from":1,"to":3},{"from":3,"to":2}]

// As well as creating a game, you can also create a graph of all possible positions,
// which will allow you to determine the shortest path from any position to the final position.
const net = Hanoi.discoverAllPositions(5);

// with this Network, you can easily get the path from ```h``` current position to the final pos.
const path = pathToSolution(net, h.ABC_Notation, "BBBBB");

console.log("path", path);
// output : path BBCCA - BBCCB (1) - BBCAB (2) - BBCAA (3) - BBBAA (4) - BBBAC (5) - BBBBC (6) - BBBBB (7)

Package Sidebar

Install

npm i hanoi_solver

Weekly Downloads

4

Version

1.0.4

License

MIT

Unpacked Size

249 kB

Total Files

39

Last publish

Collaborators

  • eshome33