The computer solves the Towers of Hanoi puzzle by itself. Press a key from 2 to 8 to choose the number of rings, which are stacked on the first of three pegs above a checkered base. The program then moves the whole stack to peg 3, one ring at a time. Each ring rises off its peg, slides across and drops onto its new place. When the stack is finished it prints “OK IN n MOVES”, and pressing p starts again.
The program carries no author credit; the only text on screen is the title “TOWERS OF HANOI”.
Program Analysis
Rings and Pegs
Lines 40–110 hold the eight rings as DATA strings built from top-half block graphics, with quarter blocks at the ends of the even-numbered rings. Each ring is therefore half a character wider than the one above it. The strings go into r$(1)–r$(8), and the unused r$(9) stays as nine spaces to erase a ring. Line 10 POKEs UDG b with alternating 85 and 170 to make a checkerboard, and line 240 prints the base from it with the peg numbers 1, 2 and 3 set into it.
The Solution
The solver at lines 340–410 is recursive, with GO SUB 340 calling itself. Sinclair BASIC has no local variables, so it keeps the ring count n and the three peg numbers p, q and r as globals. Each call swaps them before it recurses and swaps them back afterwards, and m counts the moves. Lines 640–680 find the top of a peg by stepping down the screen with SCREEN$ until a cell is not a space. Block graphics and UDGs read back as an empty string, so any ring or the base stops the search. The position is kept as a single number, row × 33 + column, and INT splits it back apart. Lines 540–620 animate the move by printing the ring and then printing r$(9) over it at each step, going up, across in two-column steps and down.
Notable Techniques
- Recursion through
GO SUB, with global variables swapped before and after each call. SCREEN$used to sense where graphics are, relying on its empty result for any character it cannot match.- Half-cell ring widths made from the quarter-block graphics.
Potential Issues
- With more than three rings the animation goes wrong. In testing, five rings were solved in the correct 31 moves, but pieces of rings were left behind on pegs 1 and 2. The top-of-peg search looks one column to the right of the peg’s centre, where the smallest ring has no graphics. The lift and slide loops then draw and erase relative to that row, so on some moves the erase appears to land on the wrong row.
- Lines 160–170 read
INKEY$three times. If the key is released after line 160 accepts it,VAL INKEY$at line 170 appears to fail on an empty string.
This program came from an Oliger (JLO SAFE) disk in the SNUG library set and was converted for tape. It has no disk commands, so no changes were needed. It was tested in ZEsarUX on a stock TS2068: three rings were solved cleanly in seven moves.
Source Code
10 CLS : RESTORE : FOR z=0 TO 6 STEP 2: POKE USR "b"+z,85: POKE USR "b"+z+1,170: NEXT z
20 PRINT "TOWERS OF HANOI"
30 DIM r$(9,9)
40 DATA " ''"
50 DATA " '''' "
60 DATA " ''''''"
70 DATA " '''''''' "
80 DATA " ''''''''''"
90 DATA " '''''''''''' "
100 DATA " ''''''''''''''"
110 DATA " '''''''''''''''' "
120 FOR z=1 TO 8: READ r$(z): NEXT z
130 LET m=0
140 LET d=170
150 PRINT "How many rings (2 to 8)?"
160 IF INKEY$<"2" OR INKEY$>"8" THEN GO TO 160
170 LET n=VAL INKEY$
180 LET n1=n
190 PRINT AT 1,0,,
200 PRINT AT 5,0;
210 FOR r=1 TO n
220 PRINT r$(r)
230 NEXT r
240 PRINT "bbbb1bbbbbbbb2bbbbbbbb3bbb"
250 LET p=1
260 LET q=2
270 LET r=3
280 GO SUB 340
290 PRINT AT 15,0;"OK IN ";m;" MOVES"
300 PRINT '"PRESS ""P"" TO GO AGAIN"
310 IF INKEY$<>"p" THEN GO TO 310
320 CLS
330 RUN
340 IF NOT n THEN RETURN
350 LET n=n-1: LET y=q: LET q=r: LET r=y
360 GO SUB 340
370 GO TO 420
380 LET m=m+1: LET y=p: LET p=r: LET r=q: LET q=y
390 GO SUB 340
400 LET y=p: LET p=q: LET q=y: LET n=n+1
410 RETURN
420 LET t=p
430 GO SUB 640
440 LET gf=tt
450 LET x=(t-tt)/33+5
460 LET t=q
470 GO SUB 640
480 LET gt=tt
490 LET y=(t-tt)/33+5
500 LET z=2*n1-x-y+15
510 LET y1=y
520 IF ABS (gf-gt)=18 AND z<y THEN LET y=z
530 IF y>x THEN LET y=x+1
540 FOR l=x TO y STEP -1
550 PRINT AT l-1,gf;r$(n+1);AT l-1,gf;r$(9)
560 NEXT l
570 FOR c=gf TO gt STEP 2*SGN (gt-gf)
580 PRINT AT l,c;r$(n+1);AT l,c;r$(9)
590 NEXT c
600 FOR l=l TO y1-1
610 PRINT AT l-1,gt;r$(9);AT l,gt;r$(n+1)
620 NEXT l
630 GO TO 380
640 LET t=9*t-9
650 LET tt=t
660 IF SCREEN$ (INT ((d+t)/33),d+t-(INT ((d+t)/33)*33))<>" " THEN RETURN
670 LET t=t+33
680 GO TO 660Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

