[Prolog] Tower of Hanoi – M discs & N poles version

The Tower of Hanoi (also called the Tower of Brahma or Lucas’ Tower, and sometimes pluralized) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

  • Only one disk can be moved at a time.
  • Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  • No disk may be placed on top of a smaller disk.

(Source: Wikipedia)

Here I use the state space search method, you may want to read [Prolog] State space search method before continuing. Each state in the state space is represented by a list in Prolog in below structure:

[D1, D2, … , Dm]

So Di is the pole index that the i-th disc is lying on (1 <= i <= m). If the problem has n poles then: 1 <= Di <= n. Beside, if i < j then the i-th disc’s size will be smaller than the j-th disc’s.

The only valid action on this state space is moving a disc to another pole with some constraints:

  1. You cannot move the disc that is not lying at the top of the pole. => Choose disc has pole index first appeared in the list.
  2. You cannot move the disc to a pole that already has a disc with smaller size lying there. => Choose the pole has index never appeared in other smaller discs standing before the chosen disc.
  3. The target pole is not the same as the source pole.

And here is our f – state transfer predicate:

  • A is the source pole index of considering disc
  • D is the destination pole index of considering disc.
  • C is the collection of all pole indices of smaller discs standing before A.
  • pole is the global variable contains the number of poles (also the possible greatest pole index of the game): If the game has 3 poles then pole = 3. N is the local variable version of pole.
  • L1 is a list contains N elements that are [N,N-1,…,3,2,1] . For example: range(L, 1, 5) will have L = [5, 4, 3, 2, 1].
  • L is a list contains all elements in L1 but not in [A|C] – all possible indices of poles that the considering disc can be moved to. For example: difference([7,3,5,8,9], [3,8], X) will have X = [7,5,9].

I will have a different article about the predicate range, difference and removedup. For the moment, I will only describe its function if necessary.
As you can see:

  • The first constraint is satisfied by:
  • Before continuing, I want to make an observation: If you run:

    It will return X = 1 ; X = 2 ; X = 3.
    The second and third constraints are then satisfied by:

    It means to choose a pole index that has never appeared before the considering disc. It also excludes the pole index that the considering disc is currently on.

May be we do not want to move the first disc in the list but also the other discs in the list with the same rules. Hence, we will use the list traversal technique:

C parameter actually breaks our f(S, X) structure so we will wrap it up:

Full program:

Run the program:


Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *