5 PRINT CHR$(26):WIDTH 80
6 DIM T(7,3)
10 PRINT TAB(33);"TOWERS":PRINT
20 PRINT TAB(15);"CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
30 PRINT:PRINT:PRINT
90 PRINT
100 REM*** INITIALIZE
110 FOR I1=0 TO 7:FOR I2=0 TO 3:T(I1,I2)=0:NEXT:NEXT
120 E=0
130 FOR D=1 TO 7
140 FOR N=1 TO 3
150 T(D,N)=0
160 NEXT N
170 NEXT D
180 PRINT "TOWERS OF HANOI PUZZLE.": PRINT
200 PRINT "YOU MUST TRANSFER THE DISKS FROM THE LEFT TO THE RIGHT"
205 PRINT "TOWER, ONE AT A TIME, NEVER PUTTING A LARGER DISK ON A"
210 PRINT "SMALLER DISK.": PRINT
215 INPUT "HOW MANY DISKS DO YOU WANT TO MOVE (7 IS MAX)";S
220 PRINT
230 M=0
240 FOR Q=1 TO 7
250 IF Q=S THEN 350
260 NEXT Q
270 E=E+1
280 IF E>2 THEN 310
290 PRINT "SORRY, BUT I CAN'T DO THAT JOB FOR YOU.": GOTO 215
310 PRINT "ALL RIGHT, WISE GUY, IF YOU CAN'T PLAY THE GAME RIGHT, I'LL"
320 PRINT "JUST TAKE MY PUZZLE AND GO HOME.  SO LONG.": RUN "MENU"
340 REM *** STORE DISKS FROM SMALLEST TO LARGEST
350 PRINT "IN THIS PROGRAM, WE SHALL REFER TO DISKS BY NUMERICAL CODE."
355 PRINT "3 WILL REPRESENT THE SMALLEST DISK, 5 THE NEXT SIZE,"
360 PRINT "7 THE NEXT, AND SO ON, UP TO 15.  IF YOU DO THE PUZZLE WITH"
365 PRINT "2 DISKS, THEIR CODE NAMES WOULD BE 13 AND 15.  WITH 3 DISKS"
370 PRINT "THE CODE NAMES WOULD BE 11, 13 AND 15, ETC.  THE NEEDLES"
375 PRINT "ARE NUMBERED FROM LEFT TO RIGHT, 1 TO 3.  WE WILL"
380 PRINT "START WITH THE DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM"
385 PRINT "TO NEEDLE 3."
390 PRINT: PRINT "GOOD LUCK!": PRINT
395 LINE INPUT "ENTER 'RETURN' TO START THE GAME. ";RESP$
400 Y=7: D=15
420 FOR X=S TO 1 STEP -1
430 T(Y,1)=D: D=D-2: Y=Y-1
460 NEXT X
470 GOSUB 1230
480 PRINT:PRINT:PRINT "WHICH DISK WOULD YOU LIKE TO MOVE";:E=0
500 INPUT D
510 IF (D-3)*(D-5)*(D-7)*(D-9)*(D-11)*(D-13)*(D-15)=0 THEN 580
520 PRINT "ILLEGAL ENTRY... YOU MAY ONLY TYPE 3,5,7,9,11,13, OR 15."
530 E=E+1: IF E>1 THEN 560
550 GOTO 500
560 PRINT "STOP WASTING MY TIME.  GO BOTHER SOMEONE ELSE.":RUN "MENU"
580 REM *** CHECK IF REQUESTED DISK IS BELOW ANOTHER
590 FOR R=1 TO 7
600 FOR C=1 TO 3
610 IF T(R,C)=D THEN 640
620 NEXT C: NEXT R
640 FOR Q=R TO 1 STEP -1
645 IF T(Q,C)=0 THEN 660
650 IF T(Q,C)<D THEN 680
660 NEXT Q
670 GOTO 700
680 PRINT "THAT DISK IS BELOW ANOTHER ONE.  MAKE ANOTHER CHOICE."
690 GOTO 480
700 E=0
705 INPUT "PLACE DISK ON WHICH NEEDLE";N
730 IF (N-1)*(N-2)*(N-3)=0 THEN 800
735 E=E+1
740 IF E>1 THEN 780
750 PRINT "I'LL ASSUME YOU HIT THE WRONG KEY THIS TIME.  BUT WATCH IT,"
760 PRINT "I ONLY ALLOW ONE MISTAKE.": GOTO 705
780 PRINT "I TRIED TO WARN YOU, BUT YOU WOULDN'T LISTEN."
790 PRINT "BYE BYE, BIG SHOT.":RUN "MENU"
800 FOR R=1 TO 7
810 IF T(R,N)<>0 THEN 840
820 NEXT R
830 GOTO 880
835 REM *** CHECK IF DISK TO BE PLACED ON A LARGER ONE
840 IF D<T(R,N) THEN 880
850 PRINT "YOU CAN'T PLACE A LARGER DISK ON TOP OF A SMALLER ONE,"
860 PRINT "IT MIGHT CRUSH IT!": PRINT "NOW THEN, ";:GOTO 480
875 REM *** MOVE RELOCATED DISK
880 FOR V=1 TO 7: FOR W=1 TO 3
900 IF T(V,W)=D THEN 930
910 NEXT W: NEXT V
925 REM *** LOCATE EMPTY SPACE ON NEEDLE N
930 FOR U=1 TO 7
940 IF T(U,N)<>0 THEN 970
950 NEXT U
960 U=7: GOTO 980
965 REM *** MOVE DISK AND SET OLD LOCATION TO 0
970 U=U-1
980 T(U,N)=T(V,W): T(V,W)=0
995 REM *** PRINT OUT CURRENT STATUS
1000 GOSUB 1230
1018 REM *** CHECK IF DONE
1020 M=M+1
1030 FOR R=1 TO 7: FOR C=1 TO 2
1050 IF T(R,C)<>0 THEN 1090
1060 NEXT C: NEXT R
1080 GOTO 1120
1090 IF M<=128 THEN 480
1100 PRINT "SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN"
1110 PRINT "128 MOVES.":RUN "MENU"
1120 IF M<>2^S-1 THEN 1140
1130 PRINT:PRINT "CONGRATULATIONS!!":PRINT
1140 PRINT "YOU HAVE PERFORMED THE TASK IN";M;"MOVES."
1150 PRINT: PRINT "TRY AGAIN (YES OR NO)";: INPUT A$
1160 IF LEFT$(A$,1)="N" THEN 1390
1170 IF LEFT$(A$,1)="Y" THEN PRINT CHR$(26):GOTO 90
1180 PRINT: PRINT "'YES' OR 'NO' PLEASE";: INPUT A$: GOTO 1160
1230 REM *** PRINT SUBROUTINE
1235 PRINT CHR$(26):FOR XX=1 TO 7:PRINT:NEXT
1240 FOR K=1 TO 7
1250 Z=10
1260 FOR J=1 TO 3
1270 IF T(K,J)=0 THEN 1330
1280 PRINT TAB(Z-INT(T(K,J)/2));
1290 FOR V=1 TO T(K,J)
1300 PRINT CHR$(22);"*";CHR$(22);
1310 NEXT V
1320 GOTO 1340
1330 PRINT TAB(Z);CHR$(22);"*";CHR$(22);
1340 Z=Z+21
1350 NEXT J
1360 PRINT
1370 NEXT K
1380 RETURN
1390 PRINT: PRINT "THANKS FOR THE GAME!": PRINT:RUN "MENU"
