Towers of Hanoi

Date: 198x
Type: Program
Platform(s): TS 1000

Appears on

Assembled by Tim Ward from many sources. Contains programs 10252 – 10293.

Gallery

Towers of Hanoi

Source Code

  65 CLS 
  70 FOR I=0 TO 7
  75 PRINT AT I,7;"\@@";TAB 17;"\@@";TAB 27;"\@@"
  80 PRINT AT 8,3+I;"\@@";AT 8,31-I;"\@@";AT 8,11+I;"\@@";AT 8,23-I;"\@@"
  81 PRINT AT 9,3+I;"\@@";AT 9,31-I;"\@@";AT 9,11+I;"\@@";AT 9,23-I;"\@@"
  85 NEXT I
  86 PRINT AT 10,3;"\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~\~~"
  90 PRINT AT 8,7;1;TAB 17;2;TAB 27;3
  95 PRINT AT 9,6;" ** TOWERS OF HANOI ** "
 105 PRINT AT 11,0;"NUMBER OF DISCS =          "
 110 PRINT AT 11,18;"?"
 111 PRINT AT 11,18;"%?"
 115 LET C=CODE INKEY$
 116 IF C<29 OR C>36 THEN GOTO 110
 117 LET N=VAL (CHR$ C)
 118 PRINT AT 11,18;N
 119 LET NN=0
 120 PRINT AT 13,0;"NUMBER OF MOVES = ";NN
 121 PRINT AT 14,1;"MINIMUM NUMBER = ";2**N-1
 140 DIM N(3)
 145 DIM D(N,3)
 150 LET Y=1
 155 FOR I=1 TO N
 160 LET N(Y)=I
 165 LET D(N(Y),Y)=N+1-I
 170 GOSUB 400
 180 NEXT I
 185 GOSUB 600
 195 IF CC=62 THEN GOTO 700
 205 PRINT AT 11,0;"MOVE FROM STACK   TO STACK     "
 210 PRINT AT 11,16;"?"
 211 PRINT AT 11,16;"%?"
 214 LET C=CODE INKEY$
 215 IF C<29 OR C>31 THEN GOTO 210
 216 LET X=VAL (CHR$ C)
 220 IF N(X)>0 THEN GOTO 230
 225 PRINT AT 12,0;"STACK ";X;" IS EMPTY            "
 226 GOTO 210
 230 PRINT AT 11,16;X
 231 PRINT AT 12,0;"                                "
 235 PRINT AT 11,27;"?"
 236 PRINT AT 11,27;"%?"
 240 LET C=CODE INKEY$
 242 IF C<29 OR C>31 THEN GOTO 235
 245 LET Y=VAL (CHR$ C)
 250 IF Y=X THEN GOTO 235
 255 PRINT AT 11,27;Y
 257 IF N(Y)=0 THEN GOTO 271
 260 IF D(N(X),X)<D(N(Y),Y) THEN GOTO 271
 265 PRINT AT 12,0;"ILLEGAL MOVE - TRY AGAIN PLEASE"
 270 GOTO 200
 271 GOSUB 300
 275 IF N(3)<N THEN GOTO 200
 280 GOSUB 383
 285 GOTO 2
 305 GOSUB 500
 310 LET N(Y)=N(Y)+1
 315 LET D(N(Y),Y)=D(N(X),X)
 320 GOSUB 400
 325 LET D(N(X),X)=0
 330 LET N(X)=N(X)-1
 380 LET NN=NN+1
 381 PRINT AT 13,18;NN
 382 RETURN 
 385 PRINT AT 15,0;"TO START NEW GAME PRESS  ","""NEW-LINE"" KEY "
 390 LET C=CODE INKEY$
 395 IF C=118 THEN GOTO 397
 396 GOTO 390
 397 GOTO 2
 405 LET D=D(N(Y),Y)
 410 LET H=20*Y-6
 412 LET V=27+2*N(Y)
 420 FOR J=1 TO D
 425 PLOT H+1+J,V
 426 PLOT H-J,V
 440 NEXT J
 460 RETURN 
 505 LET D=D(N(X),X)
 510 LET H=20*X-6
 512 LET V=27+2*N(X)
 520 FOR J=D TO 1 STEP -1
 525 UNPLOT H+1+J,V
 526 UNPLOT H-J,V
 540 NEXT J
 580 RETURN 
 605 PRINT AT 15,0;"DO YOU WISH COMPUTER TO MAKE","THE MOVES?      "
 610 PRINT AT 16,11;"YES OR %NO"
 611 PRINT AT 16,11;"%YES OR NO"
 615 LET CC=CODE INKEY$
 620 IF CC=62 OR CC=51 THEN GOTO 630
 625 GOTO 610
 630 FOR V=15 TO 16
 635 FOR C=0 TO 31
 640 PRINT AT V,C;" "
 645 NEXT C
 650 NEXT V
 655 LET C=0
 695 RETURN 
 703 LET NN=0
 705 IF N=1 OR N=3 OR N=5 OR N=7 THEN GOTO 800
 715 LET P=1
 720 LET Q=2
 725 GOSUB 900
 730 LET P=1
 735 LET Q=3
 740 GOSUB 900
 745 LET P=2
 750 LET Q=3
 755 GOSUB 900
 760 IF N(3)=N THEN GOTO 385
 780 GOTO 715
 805 LET P=1
 810 LET Q=3
 815 GOSUB 900
 820 IF N(3)=N THEN GOTO 385
 825 LET P=1
 830 LET Q=2
 835 GOSUB 900
 840 LET P=3
 845 LET Q=2
 850 GOSUB 900
 855 GOTO 805
 905 LET X=P
 910 LET Y=Q
 915 IF N(P)=0 THEN LET X=Q
 916 IF N(P)=0 OR N(Q)=0 THEN GOTO 925
 920 IF D(N(P),P)>D(N(Q),Q) THEN LET X=Q
 925 IF X=Q THEN LET Y=P
 930 GOSUB 300
 935 RETURN 
 940 CLEAR 
 950 SAVE "1026%1"
 960 RUN 

People

No people associated with this content.

Scroll to Top