Package: hanoi-python-solver
Tower of Hanoi solver package.A package for an old mathematical game :Invented by the french mathematician Edouard Lucas and published in 1889, this game presents three rods and a number of disks that can be sled on the rods. There are also three easy rules :only one disk can be moveda move is done from a rod to another oneyou always can slide a disk on an empty rod. If a destination rod contains disks, you must not slide a disk over a smaller disk.This package constructs data structures representing the game (with methods likeexecuteMove()orpossibleMoves()) and a static function nameddiscoverAllPositions()that that allows you to discover all the positions the game can take. This is particularly useful when you're not in the starting position and want to know the shortest route to a final position.ABC notation :A game position can be represented by anABC notationstring : rods a namedA,BandC. The letter placed in the most left position in the string is the name of the rod where the biggest disk is place. The letter placed in the most right position in the string gives the position of the smallest disk. Thus, the length of an ABC notation string is equal to the total number of disks in the game.Here's an example fromMathoutils.fr:this position is described by the ABC notation string"AABCB"Combination of all n letters of an ABC notation string (when dealing with n disks) :When dealing with 3 rods and n disks, there are $3^n$ different positions of n disks on the 3 rods. It also possible to create a graph that connects each position to the 2 or 3 legal allowed moves starting from this position (thus joining two or three other positions).This package contains a method that constructs such a graph, in which each node is labelled with an ABC notation string. This allows you to easily get the shortest path from a position to another one.I implemented my own graph structure and the only methods needed to solve the problem of a Tower of Hanoi game : that is why this package depends on no other package.how to use this package :Install it from PyPI :pipinstallhanoi-python-solverAfter installation succeeded, open a new *.py file and create a python program. Create a new Tower of Hanoi game by instancianting a newHanoiobject and indicate the total desired number of disks. The starting situation of disks is that all disks are stacked on theArod.Then, get all the possible legal moves from this new position or use themove()method to play as you like.After a move, try to get the new ABC notation of the game withgetABC()method from Hanoi.If you want to get the path to the goal (solution, end position, ...) or any other position of your choice : call Hanoi classpathToSolution(position)method to get a dictionnary with "path" and "moves" keys, that lists all different ABC notation string to follow in order to reach the end position, and a list of moves to play from current position to reach the end position.Here is a simple Tower of Hanoi player in Python language :fromhanoi_python_solver.hanoiimportHanoih=Hanoi(3)print("You are playing with Tower of Hanoi game and 3 disks all placed on 1st rod ! Enjoy ...\n")s=""whiles!="END":s=input("a move or SIT or SOL or END : ")ifs=="SIT":print(h.getABC())elifs=="SOL":t=input(f" ==> position to reach ({h.nbDisks}letters) : ")result=h.pathToSolution(t)print(f"path ={result["path"]}")print(f"moves ={result["moves"]}")elifs=="END":breakelse:mvt=s.split(",")iflen(mvt)==2:m1=mvt[0]m2=mvt[1]result_move=h.move((int(m1),int(m2)))ifnotresult_move:print("Move not done !")else:print(f"after move ({m1},{m2}) :{h.getABC()}")else:print("Bad move ... try 1,2")© ESHome33 - feb 2024