Towers of Hanoi (Syntax)

Developer(s): William Andrews
Date: 1984
Type: Program
Platform(s): ZX81, TS 1000
Tags: Game

The Towers of Hanoi on a 16K ZX81. Three platforms appear with five numbered blocks on the first. Press the number of the platform to move from, then the platform to move to; the top block moves across. Putting a larger block on a smaller one gets a warning to move it back, though you can ignore it. Move all five blocks to platform 3 to win; the program counts your moves, and the fewest possible is 31.

The article uses the game to show a three-dimensional string array that holds the block pictures for each position, and suggests improvements to try, such as stronger error traps. GOTO 820 saves it. A TS2068 version with color appeared in the same article.

Published in Syntax October 1984 (Towers of Hanoi).

Image Gallery

Source Code

  10 CLS
  20 FAST
  30 LET E$="          "
  40 DIM N$(10,23,9)
  50 DIM A$(5,9)
  60 DIM P(10,23)
  70 LET A$(1)="   ▐1▌   "
  80 LET A$(2)="    2    "
  90 LET A$(3)="    3    "
 100 LET A$(4)="    4    "
 110 LET A$(5)="    5    "
 120 LET S=0
 130 LET N=1
 140 LET R=11
 150 FOR C=1 TO 23 STEP 11
 160 PRINT AT R,C;"▐▒▒▒▒▒▒▒▌"
 170 PRINT AT R+1,C;"▐▒▒▒";CHR$ (N+156);"▒▒▒▌"
 180 PRINT AT R+2,C;"▐▓▓▓▓▓▓▓▌"
 190 LET N=N+1
 200 NEXT C
 210 LET C=1
 220 FOR R=6 TO 10
 230 LET P(R,C)=1
 240 NEXT R
 250 GOSUB 930
 260 FOR R=6 TO 10
 270 FOR C=1 TO 23 STEP 11
 280 IF P(R,C)=1 THEN LET N$(R,C)=A$(R-5)
 290 PRINT AT R,C;N$(R,C)
 300 NEXT C
 310 NEXT R
 320 SLOW
 330 PRINT AT 15,14;"MOVE"
 340 PRINT AT 17,8;"FROM- ";
 350 IF INKEY$="" THEN GOTO 350
 360 LET F$=INKEY$
 370 IF F$<>"1" AND F$<>"2" AND F$<>"3" THEN GOTO 350
 380 LET F=VAL F$
 390 PRINT F;"  TO- ";
 400 IF INKEY$="" THEN GOTO 400
 410 LET T$=INKEY$
 420 IF T$<>"1" AND T$<>"2" AND T$<>"3" THEN GOTO 400
 430 LET T=VAL T$
 440 IF T=F THEN GOTO 400
 450 PRINT T
 460 PRINT AT 15,9;"              "
 470 PRINT AT 15,14;"MOVE"
 480 LET S=S+1
 490 IF F=1 THEN LET C=1
 500 IF F=2 THEN LET C=12
 510 IF F=3 THEN LET C=23
 520 FOR R=6 TO 10
 530 IF P(R,C)=1 THEN PRINT AT R,C;E$
 540 IF P(R,C)=1 THEN LET Z$=N$(R,C)
 550 IF P(R,C)=0 THEN GOTO 580
 560 LET P(R,C)=0
 570 GOTO 590
 580 NEXT R
 590 IF T=1 THEN LET C=1
 600 IF T=2 THEN LET C=12
 610 IF T=3 THEN LET C=23
 620 FOR R=10 TO 6 STEP -1
 630 IF P(R,C)=1 THEN GOTO 710
 640 IF P(R,C)=0 THEN LET N$(R,C)=Z$
 650 PRINT AT R,C;N$(R,C)
 660 IF R>=10 THEN GOTO 680
 670 IF Z$(5)>N$(R+1,C,5) THEN GOSUB 970
 680 LET P(R,C)=1
 690 IF R=6 THEN GOTO 720
 700 GOTO 800
 710 NEXT R
 720 PRINT AT 15,14;"    ";AT 17,5;"YOU DID IT IN ";S;" MOVES"
 730 PRINT AT 21,4;"WANT TO TRY AGAIN? (Y/N)"
 740 IF INKEY$="" THEN GOTO 740
 750 IF INKEY$="Y" THEN RUN
 760 CLS
 770 PRINT AT 10,14;"BYE-"
 780 PAUSE 4E4
 790 STOP
 800 PRINT AT 17,8;"                   "
 810 GOTO 340
 820 SAVE "TOWERS"
 830 CLS
 840 GOSUB 930
 850 PRINT AT 3,0;"OBJECT OF THIS GAME IS TO MOVE"
 860 PRINT ,,"FIVE BLOCKS FROM TOWER 1 TO",,,"TOWER 3 IN THE SAME ORDER."
 870 PRINT ,,"A SMALLER BLOCK MAY BE PLACED ON",,"A LARGER BLOCK BUT NEVER A LARGE",,"ONE ATOP A SMALLER ONE.----THAT"
 880 PRINT ,,"COSTS EXTRA MOVES TO RESTORE THE",,"PROPER ORDER."
 890 PRINT ,,"NUMBER OF MOVES WILL BE SHOWN",,,"WHEN YOU FINISH THE GAME."
 900 PAUSE 4E4
 910 CLS
 920 RUN
 930 PRINT AT 0,8;"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄"
 940 PRINT AT 1,8;" TOWERS OF HANOI "
 950 PRINT AT 2,8;"▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀"
 960 RETURN
 970 FOR N=1 TO 16
 980 PRINT AT 15,14;"    "
 990 PRINT AT 17,7;"     * NO-NO *";AT 17,7;"     * NO NO * "
1000 NEXT N
1010 PAUSE 90
1020 PRINT AT 17,9;"THAT/S NOT LEGAL"
1030 PAUSE 150
1040 PRINT AT 15,9;"  MOVE IT BACK    "
1050 PRINT AT 17,9;"                   "
1060 RETURN

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.