Dec 222017
 
BASIC source that shows solutions to the Towers of Hanoi problem (2-15). A very typical exercise given to CS101 students. .
File THANOI.ZIP from The Programmer’s Corner in
Category BASIC Language
BASIC source that shows solutions to the Towers of Hanoi problem (2-15). A very typical exercise given to CS101 students. .
File Name File Size Zip Size Zip Type
THANOI.BAS 2857 1423 deflated
THANOI.TXT 3179 1072 deflated
TPCREAD.ME 199 165 deflated

Download File THANOI.ZIP Here

Contents of the THANOI.TXT file


5 DIM A%(16),B%(16),C%(16)
10 REM TOWERS OF HANOI
20 REM PROGRAMMER: RON ANDERSON
30 REM 5/13/89
40 PRINT "TOWERS OF HANOI SIZES 2-15 (WARNING LPRINT ON 15 = 9533 SHEETS)"
50 PRINT "SHOWS HOW TO MOVE A TOWER (STACK OF NUMBERS) FROM ONE POST"
60 PRINT "TO ANOTHER USING THREE POSTS AND NEVER STACKING A LARGER"
70 PRINT "NUMBER ON A SMALLER NUMBER (NUMBERS ARE MOVED ONE AT A TIME)"
85 PRINT "WARNING USE CTRL-BREAK TO STOP IF YOU CHOOSE A LARGE NUMBER OR WAIT"
86 PRINT "ENTER A NUMBER FROM 2 TO 15 (SUGGESTION SMALLER NUMBERS)"
90 INPUT N%
100 IF N%>15 GOTO 86
110 IF N%<2 GOTO 86
120 FOR I%=1 TO N%
130 A%(I%)=N%+1-I%
140 B%(I%)=0
150 C%(I%)=0
160 NEXT I%
170 MOVES%=(2^(N%-1))
171 MOVES%=(MOVES%-1)+MOVES%
180 POLAR%=1
182 A%(0)=0
183 B%(0)=0
184 C%(0)=0
185 TOPA%=N%
186 TOPB%=0
187 TOPC%=0
188 SPOT%=1
190 REM CLEAR SCREEN
200 CLS
210 REM PRINT POSTS
220 FOR I%=1 TO N%
230 PRINT A%(N%+1-I%),B%(N%+1-I%),C%(N%+1-I%)
235 REM LPRINT A%(N%+1-I%),B%(N%+1-I%),C%(N%+1-I%)
240 NEXT I%
245 REM LPRINT
246 REM DELAY LOOP (REMOVE FOR LONGER RUNS TO TERMINATE QUICKER)
250 FOR I%=1 TO 2500
260 NEXT I%
265 IF MOVES%=0 GOTO 999
270 IF POLAR%=1 GOTO 700
280 REM EVEN MOVE
290 POLAR%=1
300 MOVES%=MOVES%-1
310 IF SPOT%=1 GOTO 460
320 IF SPOT%=2 GOTO 580
330 IF A%(TOPA%)>B%(TOPB%) GOTO 400
335 IF A%(TOPA%)=0 GOTO 385
340 TOPB%=TOPB%+1
350 B%(TOPB%)=A%(TOPA%)
360 A%(TOPA%)=0
370 TOPA%=TOPA%-1
380 GOTO 200
385 TOPA%=TOPA%+1
390 A%(TOPA%)=B%(TOPB%)
392 B%(TOPB%)=0
394 TOPB%=TOPB%-1
396 GOTO 200
400 IF B%(TOPB%)=0 GOTO 430
405 TOPA%=TOPA%+1
410 A%(TOPA%)=B%(TOPB%)
415 B%(TOPB%)=0
420 TOPB%=TOPB%-1
425 GOTO 200
430 TOPB%=TOPB%+1
435 B%(TOPB%)=A%(TOPA%)
440 A%(TOPA%)=0
450 TOPA%=TOPA%-1
455 GOTO 200
460 IF B%(TOPB%)>C%(TOPC%) GOTO 520
465 IF B%(TOPB%)=0 GOTO 495
470 TOPC%=TOPC%+1
475 C%(TOPC%)=B%(TOPB%)
480 B%(TOPB%)=0
485 TOPB%=TOPB%-1
490 GOTO 200
495 TOPB%=TOPB%+1
500 B%(TOPB%)=C%(TOPC%)
505 C%(TOPC%)=0
510 TOPC%=TOPC%-1
515 GOTO 200
520 IF C%(TOPC%)=0 GOTO 550
525 TOPB%=TOPB%+1
530 B%(TOPB%)=C%(TOPC%)
535 C%(TOPC%)=0
540 TOPC%=TOPC%-1
545 GOTO 200
550 TOPC%=TOPC%+1
555 C%(TOPC%)=B%(TOPB%)
560 B%(TOPB%)=0
565 TOPB%=TOPB%-1
570 GOTO 200
580 IF A%(TOPA%)>C%(TOPC%) GOTO 640
585 IF A%(TOPA%)=0 GOTO 615
590 TOPC%=TOPC%+1
595 C%(TOPC%)=A%(TOPA%)
600 A%(TOPA%)=0
605 TOPA%=TOPA%-1
610 GOTO 200
615 TOPA%=TOPA%+1
620 A%(TOPA%)=C%(TOPC%)
625 C%(TOPC%)=0
630 TOPC%=TOPC%-1
635 GOTO 200
640 IF C%(TOPC%)=0 GOTO 670
645 TOPA%=TOPA%+1
650 A%(TOPA%)=C%(TOPC%)
655 C%(TOPC%)=0
660 TOPC%=TOPC%-1
665 GOTO 200
670 TOPC%=TOPC%+1
675 C%(TOPC%)=A%(TOPA%)
680 A%(TOPA%)=0
685 TOPA%=TOPA%-1
690 GOTO 200
700 REM ODD MOVE
710 POLAR%=0
720 MOVES%=MOVES%-1
730 IF SPOT%=1 THEN 810
740 IF SPOT%=2 THEN 870
750 SPOT%=1
760 TOPA%=TOPA%+1
770 A%(TOPA%)=1
780 C%(TOPC%)=0
790 TOPC%=TOPC%-1
800 GOTO 200
810 SPOT%=2
820 TOPB%=TOPB%+1
830 B%(TOPB%)=1
840 A%(TOPA%)=0
850 TOPA%=TOPA%-1
860 GOTO 200
870 TOPC%=TOPC%+1
875 SPOT%=3
880 C%(TOPC%)=1
890 B%(TOPB%)=0
900 TOPB%=TOPB%-1
910 GOTO 200
999 PRINT "PRESS ENTER TO END PROGRAM"
1000 INPUT TRASH
1001 END


 December 22, 2017  Add comments

Leave a Reply