Hanoi - 2

Discuss construction, troubleshooting, and operation of the Altair 8800c computer

Hanoi - 2

Postby BillO » March 26th, 2022, 7:45 pm

I had a little spare time today so came up with this little 55 line program. This version of the program is a text based animation of a 5 disk Towers of Hanoi.

It requires a VT100/ANSI capable terminal or terminal emulator.

It uses the same recursion method to calculate the moves as the previous sample (begins at line 300). It utilizes two arrays to keep track of the disks. TWR() represent the towers which are just stacks and TP() contains pointers to the top item in each of the stacks. As written it takes about a minute to make the 31 moves required to move a stack of 5 disks. You can change the loop in line 550 to speed it up or slow it down. There is much tidying up that could be done and some enhancements, such as better code documentation .. for another day.

I hope it's okay to post my little retro-computing diversions. I'd also love to see what the rest of you are up too.

Code: Select all
10 ESC$=CHR$(27)+"["
20 PRINT ESC$+"2J"
30 PRINT ESC$+"H"
40 PRINT "     *********************"
50 PRINT "     *                   *"
60 PRINT "     *  TOWERS OF HANOI  *"
70 PRINT "     *                   *"
80 PRINT "     *********************"
90 PRINT
100 'SET THINGS UP
110 DISK$(0)="            "
120 DISK$(1)="     XX     "
130 DISK$(2)="    XXXX    "
140 DISK$(3)="   XXXXXX   "
150 DISK$(4)="  XXXXXXXX  "
160 DISK$(5)=" XXXXXXXXXX "
170 I=6
180 FOR X=1 TO 5
190 I=I-1
200 TWR(1,X)=I:TWR(2,X)=0:TWR(3,X)=0
210 NEXT X
220 TP(1)=5:TP(2)=0:TP(3)=0
230 K=0
240 J=5
250 S=1:B=2:D=3:N=J+1
260 GOSUB 480
270 GOSUB 300
280 PRINT:PRINT K;" MOVES WERE REQUIRED."
290 END
300 N=N-1
310 IF N=1 THEN GOSUB 360:GOTO 350
320 T=B:B=D:D=T:GOSUB 300:T=D:D=B:B=T:N=N+1
330 GOSUB 360
340 T=S:S=B:B=T:GOSUB 300:T=B:B=S:S=T:N=N+1
350 RETURN
360 'UPDATE THE STACK POINTERS AND THE TOWERS
370 TP(D)=TP(D)+1
380 TWR(D,TP(D)) = TWR(S,TP(S))
390 TWR(S,TP(S))=0
400 GOSUB 440
410 TP(S)=TP(S)-1
420 K=K+1
430 RETURN
440 'PRINT THE MOVE AND PRINT OUT THE TOWERS
450 PRINT ESC$+"10;1H"
460 PRINT "MOVE FROM";S;"TO";D
470 GOSUB 550
480 PRINT ESC$+"12;1H"
490 FOR Y=5 TO 1 STEP -1
500 PRINT DISK$(TWR(1,Y));DISK$(TWR(2,Y));DISK$(TWR(3,Y))
510 NEXT Y
520 PRINT"     1           2           3"
530 GOSUB 550
540 RETURN
550 FOR T=0 TO 400:NEXT T:RETURN
BillO
 
Posts: 136
Joined: November 11th, 2020, 6:29 am

Return to Altair 8800c

Who is online

Users browsing this forum: No registered users and 8 guests

cron