-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path20_Towers_of_Hanoi.bc2
173 lines (172 loc) · 5.44 KB
/
20_Towers_of_Hanoi.bc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
1000 A=1000:GOTO20:REM TOWERS OF HANOI
1010 REM
1020 GOSUB100
1030 PRINT"THE TOWERS OF HANOI"
1040 PRINT"==================="
1050 PRINT:PRINT:PRINT"A BASICODE-2-PRODUCTION"
1060 PRINT:PRINT"BY THOMAS HOFMEISTER, GERMANY"
1070 PRINT:PRINT:PRINT"Do you want instructions (y/n)"
1080 GOSUB200
1090 IF(IN$="Y") OR(IN$="y") THENGOSUB2060:GOTO1120
1100 IF(IN$="N") OR(IN$="n") THEN1120
1110 GOTO1080
1120 GOSUB100:PRINT"How many discs shall I move?"
1130 PRINT:PRINT"(at least one for a 40 character screen,"
1140 PRINT:PRINT"to a maximum of 12 for graphic option)"
1150 PRINT:PRINT:INPUTA5:IFA5<1 THEN1120
1160 IFA5>255 THEN1120
1170 GOSUB100
1180 PRINT"Should I display the results"
1190 PRINT:PRINT"graphically ('G') or in words ('W')"
1200 GOSUB200
1210 IF(IN$="g") OR(IN$="G") THENG9=1:GOTO1240
1220 IF(IN$="w") OR(IN$="W") THENG9=0:GOTO1240
1230 GOTO1200
1240 REM PREPARE THREE PILES
1250 DIMH(3,A5),A1(3),SC$(A5)
1260 DIMVO(A5),NA(A5),AN(A5),AD(A5)
1270 IFG9=1 THENGOSUB1810
1280 IFG9=0 THENGOSUB100
1290 A1(1)=A5:A1(2)=0:A1(3)=0
1300 FORI=1 TOA5:H(1,I)=A5+1-I:NEXTI
1310 REM ****************************
1320 REM BEGIN OF RECURSIVE PROCEDURE
1330 TF=0
1340 TF=TF+1
1350 VO(TF)=1:NA(TF)=3:AN(TF)=A5
1360 AD(TF)=1
1370 IFAN(TF)<>1 THEN1390
1380 VO=VO(TF):NA=NA(TF):GOSUB1540:GOTO1480
1390 TF=TF+1:VO(TF)=VO(TF-1)
1400 NA(TF)=6-NA(TF-1)-VO(TF-1)
1410 AN(TF)=AN(TF-1)-1
1420 AD(TF)=2:GOTO1370
1430 VO=VO(TF):NA=NA(TF):GOSUB1540
1440 TF=TF+1:VO(TF)=6-NA(TF-1)-VO(TF-1)
1450 NA(TF)=NA(TF-1):AN(TF)=AN(TF-1)-1
1460 AD(TF)=3:GOTO1370
1470 GOTO1480
1480 REM PSEUDO-RETURN
1490 TF=TF-1:Z=AD(TF+1)
1500 ONZ GOTO2040,1430,1470
1510 STOP
1520 REM ******************************
1530 REM END OF RECURSIVE PROCEDURE
1540 IFG9=0 THENPRINT"from ";VO;" to ";NA:RETURN
1550 A1(NA)=A1(NA)+1
1560 Z=H(VO,A1(VO))
1570 H(NA,A1(NA))=Z
1580 A1(VO)=A1(VO)-1
1590 REM NOW THE GRAPHIC PART
1600 HO=(VO-1)*(A5+1)+1:VE=20-A1(VO)-1
1610 GOSUB110
1620 H$=LEFT$(A2$,A5):PRINTH$;
1630 REM MOVE UPWARDS
1640 FORI=VE TO20-A5-2 STEP-1:VE=I:GOSUB110:PRINTSC$(Z);
1650 GOSUB2050
1660 GOSUB110:PRINTH$;
1670 NEXTI
1680 REM SIDEWAYS MOVEMENT
1690 V1=20-A1(NA)
1700 H1=(NA-1)*(A5+1)+1
1710 FORI=HO TOH1 STEP(SGN(H1-HO)):HO=I
1720 GOSUB110:PRINTSC$(Z);
1730 GOSUB2050
1740 GOSUB110:PRINTH$;:NEXTI
1750 FORI=VE TOV1-1:VE=I
1760 GOSUB110:PRINTSC$(Z);
1770 GOSUB2050
1780 GOSUB110:PRINTH$;:NEXTI
1790 HO=H1:VE=V1:GOSUB110:PRINTSC$(Z);
1800 RETURN
1810 REM BUILDING BLOCKS
1820 GOSUB100
1830 VE=20:HO=0:GOSUB110
1840 FORK=1 TO3
1850 PRINT"i";:FORI=1 TOA5:PRINT"-";:NEXTI
1860 NEXTK:PRINT"i";
1870 FORI=1 TOA5:VE=20-I
1880 HO=0:FORL=1 TO4
1890 GOSUB110:PRINT"i";
1900 HO=HO+A5+1
1910 NEXTL:NEXTI
1920 REM STRING VARIABLES FOR
1930 REM DISPLAY IN WORD
1940 REM
1950 A1$="***********************"
1960 A2$=" "
1970 FORI=1 TOA5:SC$(I)=""
1980 IFI=A5 THEN2000
1990 SC$(I)=LEFT$(A2$,A5-I)
2000 SC$(I)=SC$(I)+LEFT$(A1$,I)
2010 NEXTI
2020 FORI=1 TOA5
2030 VE=20-I:HO=1:GOSUB110:PRINTSC$(A5+1-I);:NEXTI:RETURN
2040 GOTO2440
2050 RETURN
2060 GOSUB100
2070 PRINT"Towers of Hanoi is an ancient"
2080 PRINT:PRINT"problem which can be solved"
2090 PRINT:PRINT"easily by using recursion."
2100 PRINT:PRINT"You have three posts. On"
2110 PRINT:PRINT"the first is a pile of"
2120 PRINT:PRINT"discs, each one smaller"
2130 PRINT:PRINT"than the one below. The aim"
2140 PRINT:PRINT"is to move all discs "
2150 PRINT:PRINT"from post 1 to post 3, using"
2160 PRINT:PRINT"post 2 to help."
2170 GOSUB2410
2180 PRINT:PRINT"You can move ONE disc "
2190 PRINT:PRINT"at a time, but you CANNOT"
2200 PRINT:PRINT"put a disc on top of"
2210 PRINT:PRINT"another disc that is smaller."
2220 PRINT:PRINT"The RECURSIVE solution "
2230 PRINT:PRINT"says that in order to"
2240 PRINT:PRINT"solve the problem for N"
2250 PRINT:PRINT"discs, you will have to move"
2260 GOSUB2410
2270 PRINT:PRINT"N-1 discs from post 1 to post 2,"
2280 PRINT:PRINT"then move the last disc from post 1"
2290 PRINT:PRINT"to post 3, then move N-1 discs"
2300 PRINT:PRINT"from post 2 to post 3."
2310 GOSUB2410
2320 PRINT:PRINT"The author solved the "
2330 PRINT:PRINT"problem by using recursion"
2340 PRINT:PRINT"in BASIC without using "
2350 PRINT:PRINT"any GOSUB, which would "
2360 PRINT:PRINT"limit the number of the "
2370 PRINT:PRINT"discs, and it demonstrates"
2380 PRINT:PRINT"that anything can be done in BASIC!"
2390 GOSUB2410
2400 RETURN
2410 HO=0:VE=20:GOSUB110
2420 PRINT"Please press a key to continue";
2430 GOSUB210:GOSUB100:RETURN
2440 HO=15:VE=0:GOSUB110
2450 PRINT"Please press a key";
2460 GOSUB210
2470 GOSUB100
2480 PRINT"Do you want to restart? (Y/N)"
2490 GOSUB200
2500 IF(IN$="y") OR(IN$="Y") THENRUN
2510 IF(IN$="n") OR(IN$="N") THEN2530
2520 GOTO2490
2530 GOSUB100
2540 PRINT"******************************"
2550 PRINT"* *"
2560 PRINT"* THOMAS HOFMEISTER *"
2570 PRINT"* *"
2580 PRINT"* WITTENBERGSTR. 25 *"
2590 PRINT"* *"
2600 PRINT"* 4630 BOCHUM 7 *"
2610 PRINT"* *"
2620 PRINT"* WEST-GERMANY *"
2630 PRINT"* *"
2640 PRINT"******************************"
30000 REM PROGRAM WAS WRITTEN ON
30010 REM A COMMODORE-64 IN
30020 REM OCTOBER 83 AND ADAPTED FOR
30030 REM BASICODE-2 ON THE SAME DAY.
30040 REM
30050 REM TRANSLATED
30060 REM BY JONATHAN MARKS