ARCHIVE SERIES PART 23 : SOLVING THE TOWERS OF HANOI PUZZLE WITH ARCHIVE
Although ARCHIVE is not a language designed for general programming purposes it does work quite well for most anything you wish. As I was working through some problems while teaching myself the C language I came across the Towers of Hanoi puzzle. It is not difficult and It is a very nice example of how recursion can really simplify certain programming problems. If you remember, recursion is where a function (procedure) calls itself until some criteria is met and all the calls can gradually work their way back out to a solution. After writing a C program to solve the puzzle, I thought that it would be fun to write a little ARCHIVE program to solve it also. The only “trick” needed for the ARCHIVE solution was to use a database to hold values that would normally be held in a variable array in most languages. You might think this to be crude but if you set up the database in RAM it works very fast and has the advantage to being able to exist outside of the programs normal data area. There are some cases where this could be a very powerful tool. Maybe next time we can explore it more.
The Towers of Hanoi puzzle goes like this. You have three pegs and a set of disks with holes in them so they will fit on the pegs. The disks go in size from a smallest one to a largest one with no two alike. We will call the smallest disk number 1 and the next number 2 and so one. Peg 1 is on your left and peg 2 is in the middle and peg 3 is on your right. You start with all the disks on the left peg with the largest on the bottom, the next largest right above it and so on. The rules are that you must move the disks from the left peg (#1) to the right peg (#3) and you may not put a larger disk on a smaller disk. So if you have 3 disks you must put disk 1 on peg 3 and disk 2 on peg 2 and disk 1 on peg 2 and disk 3 on peg 3 and disk 1 on peg 1 and disk 2 on peg 3 and disk 1 on peg 3. It takes 7 moves. You will find that it takes 2^n – 1 moves if there are n disks. The start and finish is shown in the illustration below.
1 2 3 1 2 3
| | | | | |
XXX | | | | XXX
XXXXX | | | | XXXXX
XXXXXXX | | | | XXXXXXX
---------------------------- ----------------------------
Start Finish
To make sure you understand it try working through it for the case of 4 pegs. You will notice that you have to make the right choice for the intermediate pegs to end up with the disks on the right peg at the end. It takes 2^4 – 1 or 15 moves to solve it for 4 disks.
Now try thinking about how this problem can be solved for the general case of n disks using a recursive method. Use any language you are familiar with. Think about it a while and try to work out a rough solution and then read on.
Recursion and the Towers of Hanoi
The recursive solution works like this. If there is just one disk the solution is trivial. Just move the one disk from the peg it is on to the peg it is to go to. But suppose we have n disks we want to move from peg 1 to peg 3. First we will call a routine which will move n-1 disks from peg 1 to peg 2 and then move disk n from peg 1 to peg 3 and then the n-1 disks from peg 2 to peg 3. Sounds easy but how does this magical routine that can move n-1 disks from peg 1 to peg 2 work. It just calls itself with (n-1)-1 to move from peg 1 to peg 3. This recursion goes on until the n-1 is actually 1. Moving the 1 disk is easy. Then the last called procedure can return to the previous calling one and so forth until all the necessary moves are done and we are back at the main procedure.
In the listing given below I have included a display procedure so you can see the disks moving around. The display takes a lot of processing time so it really slows the program down. You often seen the Towers of Hanoi used in benchmark runs to compare processors or languages. This program is not optimized in any way for speed. A limit of 15 disks is given because the display has about reached its limits but there is no other reason why a larger number of disks could not be used. Disable the display portion to explore larger number of disks or to do speed comparisons. Most languages support arrays and you would use a 2 dimensional array to keep track of the pegs and disks. Instead I defined a database in RAM1_ with 3 fields (peg1, peg2, peg3) and then add as many records to it as there are to be disks. Then I can keep track of the pegs and disks by looking at the fields and records of the database. It works quit well and it would be easy to halt the program and save the database and start it up again from the stopping point.
Enter the program by starting the ARCHIVE editor and then entering each procedure. Notice the ERROR command used when attempting to delete the RAM1_ database just in case it doesn’t exist. Save the program to disk and to run it make sure it is loaded in ARCHIVE and just type “tower<ENTER>” at the ARCHIVE prompt. The listing begins on the next page. The first procedure (a_tower) only exists to be a heading and does nothing.
This series only has 2 more installments. If you have any special requests about ARCHIVE programming be sure to let me know in time to cover it here. Until next time HAPPY ARCHIVING!
LISTING OF TOWER_PRG
proc a_tower
rem
rem tower_prg is an ARCHIVE program that displays and solves the Tower of Hanoi puzzle for
rem the specified number of disks. Originally appearing in UPDATE magazine but released to
rem the public domain. B Cable 1/96
rem
endproc
proc close_pegs
rem
rem close_pegs will close peg file logical name "p" if it is open
rem If it is not open an error will occur which should be trapped by
rem the calling proceudre
rem
close "p"
endproc
proc create_pegs;ndisks-
rem
rem create_pegs closes and deletes pegs_dbf if it exists then creates a
rem a new set of pegs with one record for each disk to be used. The peg_dbf
rem database is expected to be on device td$(target device) which should be
rem set to ram1_ in the tower procedure. The passed argument ndisks is the
rem number of disks to have on the first peg.
rem
error close_pegs
kill td$+"peg_dbf"
create td$+"peg_dbf" logical "p"
peg1
peg2
peg3
endcreate
let i=0: while i<ndisks
let peg1=ndisks-i: append : let i=i+1: endwhile
endproc
proc disk_show;peg,disk,level
rem
rem disk_show will display the specified disk on the specified peg.
rem Each disk is associated with a letter 1 - A, 2 - B, etc which is
rem is used in the display. A larger disk number means a larger disk.
rem The level tells the procedure where it should go on the peg. Each peg
rem has a different position on the screen (1 = 15, 2 = 40, 3 = 65)
rem The chr(64+disk) changes the disk number into a display letter.
rem
if peg=1: let peg=15
else : if peg=2: let peg=40
else : let peg=65: endif : endif
print at 20-level,peg-disk;chr(64+disk);rept(chr(64+disk),2*disk);
endproc
proc disk_unshow;peg,disk,level
rem
rem disk_unshow will remove a disk from a peg. Before a disk is put
rem on a new peg it must be removed from its old place on the screen.
rem
if peg=1: let peg=15
else : if peg=2: let peg=40
else : let peg=65: endif : endif
print at 20-level,peg-disk;" ";rept(" ",2*disk);
print at 20-level,peg;"|";
endproc
proc move;disk,p1,p2
rem
rem move is the recursive procedure which will move a set of disks
rem on peg p1 to peg p2. First the unused peg has to be figured out so
rem the disk-1 set of disks can be put on it. This procedure is recursive
rem unless disk=1 in which case the move can actually occur and the calls can
rem begin working their way back. The variable p3 must be local or the
rem next recursion will reset the value.
rem
local p3
if p1<>1 and p2<>1
let p3=1
else : if p1<>2 and p2<>2
let p3=2
else : let p3=3
endif : endif
if disk=1
take;p1,1
put;p2,1
else
move;disk-1,p1,p3
take;p1,disk
put;p2,disk
move;disk-1,p3,p2
endif
endproc
proc put;peg,disk
rem
rem put will put the specified disk on next free level of the specified
rem If there is no room on the peg the program stops. This will not occur
rem the program was typed in with an error. The disk is show on the screen
rem by the call to disk_show.
rem
first : while fieldv(peg-1) and not eof(): next : endwhile
if eof()
print "No room on peg ";peg;" for disk ";disk
stop
endif
if peg=1: let peg1=disk
else : if peg=2: let peg2=disk
else : let peg3=disk: endif : endif
update
disk_show;peg,disk,recnum()+1
let moves=moves+1
endproc
proc show_pegs
rem
rem show_pegs sets up the initial screen to show all the disks on
rem peg 1.
rem
print at 20,1;rept("-",78)
let i=1: first : while not eof()
disk_show;1,peg1,i
print tab 40;"|"; tab 65;"|"
let i=i+1: next : endwhile
print at 20-i,15;"|"; tab 40;"|"; tab 65;"|"
endproc
proc take;peg,disk
rem
rem take will remove the specified disk from the specified peg. If it is
rem not found then the program will stop. This will not occur unless there
rem was an error when the program was typed in. The disk is remove from
rem the screen by the call to disk_unshow.
rem
first : while fieldv(peg-1)<>disk and not eof(): next : endwhile
if eof()
print "Error disk ";disk;" not found on peg ";peg
stop
endif
if peg=1
let peg1=0
else : if peg=2
let peg2=0
else : let peg3=0: endif : endif
update
disk_unshow;peg,disk,recnum()+1
endproc
proc tower
rem
rem tower is the main procedure of the tower program. It sets the initial
rem conditions and prompts for how many disks to use. It displays the
rem pegs at the start and then starts the recursive calls to move.
rem
let td$="ram1_": let moves=0
mode 0
print " TOWER OF HANOI B Cable 1/96"
print
input " Start with how many disk [2-15] or 0 to stop? ";nd
let nd=int(nd)
if nd=0: mode 1: stop : endif
if nd<2 or nd>15:tower: endif
create_pegs;nd
show_pegs
move;nd-1,1,2
take;1,nd
put;3,nd
move;nd-1,2,3
close_pegs
print at 22,1;"Done after ";moves;" moves"; at 24,1;
endproc
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.