1
0
mirror of https://github.com/rfivet/BCG101.git synced 2024-12-18 08:06:27 -05:00
BCG101/dec/TOWER.BAS

121 lines
3.7 KiB
QBasic

90 PRINT
100 REM *** INITIALIZE
110 DIM T(7,3)
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 ON A"
210 PRINT "SMALLER DISK."\PRINT
215 PRINT "HOW MANY DISKS DO YOU WANT TO MOVE (7 IS MAX)";
220 INPUT S\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 "ALRIGHT, WISE GUY, IF YOU CAN'T PLAY THE GAME RIGHT, I'LL"
320 PRINT "JUST TAKE MY PUZZLE AND GO HOME. SO LONG."\STOP
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, 7 THE NEXT,"
360 PRINT "AND SO ON, UP TO 15. IF YOU DO THE PUZZLE WITH 2 DISKS, THEIR"
365 PRINT "CODE NAMES WOULD BE 13 AND 15. WITH THREE DISKS, THE CODE"
370 PRINT "NAMES WOULD BE 11, 13 AND 15, ETC. THE NEEDLES ARE"
375 PRINT "NUMBERED FROM LEFT TO RIGHT, 1 TO 3. WE WILL START WITH THE "
380 PRINT "DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM TO NEEDLE 3."
390 PRINT\PRINT "GOOD LUCK!!"\PRINT
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 "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\PRINT "STOP WASTING MY TIME. GO BOTHER SOMEONE ELSE."\STOP
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."\GOTO 480
700 E=0
705 PRINT "PLACE DISK ON WHICH NEEDLE";\INPUT 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."\STOP
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 *** RELOCATE MOVED 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 FIRST EMPTY SPACE ON NEEDLE N
930 FOR U=1 TO 7
940 IF T(U,N)<>0 THEN 970
950 NEXT U
960 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."\STOP
1120 IF M<>2^S-1 THEN 1140
1130 PRINT "CONGRATULATIONS!! ";
1140 PRINT "YOU HAVE PERFORMED THE TASK IN"M"MOVES."
1150 PRINT\PRINT "TRY ANOTHER ONE (YES OR NO)";\INPUT A$
1160 IF A$="NO" THEN 1390
1170 IF A$="YES" THEN 90
1180 PRINT\PRINT "'YES' OR 'NO' PLEASE";\INPUT A$\GOTO 1160
1230 REM *** PRINT SUBROUTINE
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 "*";
1310 NEXT V
1320 GOTO 1340
1330 PRINT TAB(Z);"*";
1340 Z=Z+21
1350 NEXT J
1360 PRINT
1370 NEXT K
1380 RETURN
1390 PRINT\PRINT "THANKS FOR THE GAME!"\PRINT\END