hanoi_solver
v1.0.4
Published
tower of Hanoi game solver
Downloads
4
Maintainers
Readme
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)